Php 表中的Foreach…限制为7,然后创建新行

Php 表中的Foreach…限制为7,然后创建新行,php,magento,foreach,Php,Magento,Foreach,我用这段代码打印了一份畅销产品清单的输出。问题是,它横向延伸而不断裂,并且在7个产品之后离开屏幕的一侧 我想做的是,让第8到第14个条目开始新的一行,但由于我的尝试一直没有成功,我完全受阻了。为了实现这一点,我需要如何修改代码 <?php $products = $this->getCollection(); ?> <?php if ($products && $products->count() > 0) { ?> <div c

我用这段代码打印了一份畅销产品清单的输出。问题是,它横向延伸而不断裂,并且在7个产品之后离开屏幕的一侧

我想做的是,让第8到第14个条目开始新的一行,但由于我的尝试一直没有成功,我完全受阻了。为了实现这一点,我需要如何修改代码

<?php $products = $this->getCollection(); ?>
<?php if ($products && $products->count() > 0) { ?>

<div class="block block-list block-viewed">
    <div class="block-title">
        <strong><span><?php echo $this->__($this->getHeader()) ?></span></strong>
    </div>
    <div class="block-content">

    <table class="amsorting-table">
        <tr>
        <?php foreach ($products as $p) { ?>
            <td style="padding: 15px 15px 0px 15px;">
                <a href="<?php echo $p->getProductUrl() ?>" title="<?php echo $this->htmlEscape($p->getName()) ?>" class="product-image"><img 
src="<?php echo $this->helper('catalog/image')->init($p, 'small_image')->resize(125) ?>" width="125" height="125" alt="<?php echo 
$this->htmlEscape($p->getName()) ?>" /></a>
                <h3 class="product-name"><a href="<?php echo $p->getProductUrl() ?>" title="<?php echo $this->htmlEscape($p->getName()) 
?>"><?php echo $this->htmlEscape($p->getName()) ?></a></h3>
                <?php echo $this->getPriceHtml($p, true) ?>

            </td>
        <?php } ?>
        </tr>
        <tr>
            <?php foreach ($products as $p) { ?>
               <td style="padding: 0px 15px 15px;">
                <?php if($p->isSaleable()): ?>
                        <button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart"
onclick="setLocation('<?php echo $this->getAddToCartUrl($p) ?>')"><span><span><?php echo $this->__('Add to Cart')
?></span></span></button>
                    <?php else: ?>
                        <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
                    <?php endif; ?>

            <?php } ?>
              </td>
        </tr>
    </table>

    </div>
</div>

<?php } ?>


根据当前产品索引是否可被7整除,有条件地标记

<table class="amsorting-table">
   <?php 
   foreach ($products as $i => $p) { 

     if ($i % 7 == 0) { // start a new row on product 0, 7, 14, etc

        if ($i > 0) { // if this is not the 1st product, close the previous row
           ?></tr><?php
        }

        ?><tr><?php
     }

     ... your code the display the table cell for the current product

   }

   // after the loop, we need to close the last row
   ?></tr><?php
   ...


记下您创建了多少个
td
s。对于每一个新的,得到你的计数模7的值。如果模为0,插入tr。带结束符的代码会是什么样子?@nero实际上,我编辑了我的答案,因为我在我的示例中意识到,您将始终需要在循环后添加最后的