PHP – Split Foreach Based on Even/Odd

htmlPHP

I currently use a code to display some content in my Magento shop.
But now I want to split the loaded content based on even/odd into two different divs.

My current code is displayed below.

How can I split the code based on even/odd so that I get to <div class="block-specs">.

I want two div's <div class="block-specs odd"> and <div class="block-specs even">

But inside that div only the content of the even or odd should be displayed.

So that I get two main divs, two display left/right.

How can I achieve that, so that I split the foreach code?

Current code:

<?php if($_additionalgroup = $this->getAdditionalData()): ?>
<section id="additional">
<div class="box-collateral box-additional">
    <h2><?php echo $this->__('Additional Information') ?></h2>

    <?php $i=0; foreach ($_additionalgroup as $_additional): $i++; $oddEven =($i % 2) ? 'odd':'even';?>
    <div class="block-specs-<?php echo $i?>">
        <h3 class="specs-<?php echo $i?>"><?php echo $this->__( $_additional['title'] )?></h3>
        <table class="data-table specs-<?php echo $i?>" id="product-attribute-specs-table-<?php echo $i?>">
            <col width="25%" />
            <col />
            <tbody>
            <?php foreach ($_additional['items'] as $_data): ?>
             <?php $_attribute = $_product->getResource()->getAttribute($_data['code']);
    if (!is_null($_product->getData($_attribute->getAttributeCode())) && ((string)$_attribute->getFrontend()->getValue($_product) != '')) { ?>
                <tr>
                    <th class="label"><?php echo $this->htmlEscape($this->__($_data['label'])) ?></th>
                    <td class="data"><?php echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?></td>
                </tr>
            <?php } ?>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
        <script type="text/javascript">decorateTable('product-attribute-specs-table-<?php echo $i?>')</script>
    <?php endforeach; ?>

</div>
</section>
<?php endif;?>

Best Answer

Replace :

<?php $i=0; foreach ($_additionalgroup as $_additional): $i++; $oddEven =($i % 2) ? 'odd':'even';?>
<div class="block-specs-<?php echo $i?>">
    <h3 class="specs-<?php echo $i?>"><?php echo $this->__( $_additional['title'] )?></h3>
    <table class="data-table specs-<?php echo $i?>" id="product-attribute-specs-table-<?php echo $i?>">
        <col width="25%" />
        <col />
        <tbody>
        <?php foreach ($_additional['items'] as $_data): ?>
         <?php $_attribute = $_product->getResource()->getAttribute($_data['code']);
if (!is_null($_product->getData($_attribute->getAttributeCode())) && ((string)$_attribute->getFrontend()->getValue($_product) != '')) { ?>
            <tr>
                <th class="label"><?php echo $this->htmlEscape($this->__($_data['label'])) ?></th>
                <td class="data"><?php echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?></td>
            </tr>
        <?php } ?>
        <?php endforeach; ?>
        </tbody>
    </table>
</div>
    <script type="text/javascript">decorateTable('product-attribute-specs-table-<?php echo $i?>')</script>
<?php endforeach; ?>

WITH :

<?php
$oddEvenData = array();

$i=0; 
foreach ($_additionalgroup as $_additional): 
    $i++; 
    $oddEven =($i % 2) ? 'odd':'even';
    $oddEvenData[$oddEven] = array();

    foreach ($_additional['items'] as $_data): 
        $_attribute = $_product->getResource()->getAttribute($_data['code']);
        if (!is_null($_product->getData($_attribute->getAttributeCode())) && ((string)$_attribute->getFrontend()->getValue($_product) != '')) {
            $oddEvenData[$oddEven][] = '
            <tr>
            <th class="label">'.$this->htmlEscape($this->__($_data['label'])).'</th>
            <td class="data">'.$_helper->productAttribute($_product, $_data['value'], $_data['code']).'</td>
            </tr>
            ';
        } 
    endforeach; 
endforeach; ?>

<?php //PRINTING LEFT AND RIGHT DIVs ?>
<div class="block-specs odd">
    <h3 class="specs-odd">TITLE ODD</h3>
    <table class="data-table specs-left" id="product-attribute-specs-table-left">
        <col width="25%" />
        <col />
        <tbody>
            <?php echo implode("",$oddEvenData["odd"]); ?>
        </tbody>
    </table>
</div>
<script type="text/javascript">decorateTable('product-attribute-specs-table-left')</script>

<div class="block-specs even">
    <h3 class="specs-odd">TITLE EVEN</h3>
    <table class="data-table specs-right" id="product-attribute-specs-table-right">
        <col width="25%" />
        <col />
        <tbody>
            <?php echo implode("",$oddEvenData["even"]); ?>
        </tbody>
    </table>
</div>
<script type="text/javascript">decorateTable('product-attribute-specs-table-right')</script>
Related Topic