2016-04-24 40 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; ?>">