0

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

2 Answers 2

1

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>
2
  • Thanks a lot! But that only display's the first odd and first even. Not all attributes are displayed then. And also the <h3> title is not displayed anymore.
    – JGeer
    Commented May 6, 2016 at 13:29
  • there are two fixed H3 tags for two block (odd and even), right ? Commented May 6, 2016 at 13:34
0

Please try to add below code after box-collateral class div end.

<script type="text/javascript">decorateGeneric($$('div.box-collateral'), ['odd','even','first','last'])</script>

I hope this will help you.

1
  • sorry, by mistake, I edited your answer Commented May 6, 2016 at 13:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.