基于偶数/奇数

时间:2016-05-06 12:43:44

标签: php html magento

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

我目前的代码显示如下。

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

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

但在div中只应显示偶数或奇数的内容。

所以我得到两个主要的div,左/右两个显示。

我如何实现这一点,以便分割foreach代码?

编辑代码:

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

当前代码:

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

4 个答案:

答案 0 :(得分:2)

'xhtml'   => sprintf( '<body id="skrollr-body" class="%s">', implode( ' ', get_body_class() ) ),

答案 1 :(得分:1)

所以这就是我想出来的。 你可以尝试一下,给我们一些反馈意见:

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

答案 2 :(得分:0)

这样你可以将结果分成多维数组

foreach ($_additionalgroup as $a => $_additional){ $additionalgroup_oddeven[(++$i % 2)][$a] = $_additional; }

然后您可以foreach覆盖特定群组,例如

foreach($additionalgroup_oddeven[0] as $_additional){ /* first div */ } foreach($additionalgroup_oddeven[1] as $_additional){ /* second div */ }

答案 3 :(得分:0)

您可以在回显任何HTML之前添加以下代码,将$_additionalgroup数组拆分为包含赔率和平均值的两个数组。 请注意,isOdd不是PHP函数,您需要自己实现它,我只是为了简单起见而添加它。

$oddArr = array();
$evenArr = array();

for($i=0; $i<count($_additionalgroup); $i++)
{
     $value = $_additionalGroup[$i];
     if(isOdd($i)
     {
         $oddArr[] = $value;
     }
     else
     {
         $evenArr[] = $value;
     }
 }