2016-04-24 35 views
0

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

我的当前代码显示在下面。

如何根据偶数/奇数拆分代码以便我得到<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;?> 

回答

2

检查如果索引是2以 '%' 均匀divisable此返回分割后的余数(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; ?>">