Henk Z Henk Z - 7 months ago 5
PHP Question

Split foreach based on even/odd

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?

EDIT CODE:

<?php $i=0; foreach ($_additionalgroup as $a => $_additional){ $additionalgroup_oddeven[(++$i % 2)][$a] = $_additional; }; ?>
<?php foreach($additionalgroup_oddeven[0] as $_additional){ ?>
<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 } ?>
<?php endforeach; ?>
<?php endforeach; ?>


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;?>

Answer

So here is what I came up with. Could you try it and give us some Feedback:

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

                    <?php $oddDataContent = ""; $evenDataContent = ""; ?>
                    <?php $i=0; foreach ($_additionalgroup as $_additional): $i++; $oddEven =($i % 2) ? 'odd':'even';?>
                        <?php ob_start(); ?>
                        <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 $contentData = ob_get_clean(); if($oddEven == 'even'){$evenDataContent .= $contentData; }else{$oddDataContent .= $contentData;} ?>
                    <?php endforeach; ?>

                    <!-- HERE'S TO THE TRICK:-->            
                    <!-- MANUALLY CREATE THE DIV ODD & EVEN DIVS HERE -->
                    <div class="blocs-specs-odd">
                        <!-- INSERT YOUR ODD CONTENT DATA HERE:-->
                        <?php echo $oddDataContent;?>
                    </div>

                    <div class="blocs-specs-even">
                        <!-- INSERT YOUR EVEN CONTENT DATA HERE:-->
                        <?php echo $evenDataContent;?>
                    </div>

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