基于偶数/奇数分割foreach代码

时间:2016-04-24 17:46:27

标签: html magento foreach split

我目前使用代码在Magento商店中显示一些内容。 但是现在我想把基于偶数/奇数的加载内容分成两个不同的div。

我目前的代码显示如下。

如何基于偶数/奇数拆分代码,以便我到达<div class="block-specs">

我想要一个div <div class="block-specs odd"><div class="block-specs even">

我怎样才能做到这一点?

当前代码:

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

1 个答案:

答案 0 :(得分:2)

检查索引是否可以按&2%&#39;%&#39;这会在分割后返回余数(如果偶数则为0)。

<?php foreach ($_additionalgroup as $i => $_additional): 
  // if evenly divisable by 2, it is even
  $oddEven =($i % 2) ? 'odd':'even';
?>

<div class="block-specs-<?php echo $oddEven; ?>">