简体   繁体   English

PHP foreach 循环跳过前 3 项

[英]PHP foreach loop skip first 3 items

I have a list of 6 products that i want to split in 2 lists of 3 products next to each other.我有一个包含 6 个产品的列表,我想将它们拆分为 2 个彼此相邻的 3 个产品列表。 The list are made within a foreach loop, the first list stops after the count == 2, so 3 items wil be displayed.该列表是在 foreach 循环中创建的,第一个列表在 count == 2 之后停止,因此将显示 3 个项目。 The second list should start with the fourth item.第二个列表应该从第四个项目开始。 How can i achieve this?我怎样才能做到这一点?

This is wat makes the first list of 3 items:这是 wat 列出的 3 个项目的第一个列表:

 <?php $_categoryId = explode(' ', $category['id']); $count = 0; $_productCollection = Mage::getModel('catalog/category')->load($_categoryId) ->getProductCollection() ->addAttributeToSelect('*') ->setOrder('date_added', 'DESC'); ?> <?php foreach ($_productCollection as $_product): ?> <li class="category-row-list-item"> <a class="product-name" href="<?php echo $_product->getProductUrl() ?>"> <?php echo $this->htmlEscape($_product->getName()) ?> </a> </li> <?php if($count == 2) break; // Stop after 3 items $count++; ?> <?php endforeach ?>

Best regards, Robert最好的问候,罗伯特

For simplicity you could repeat the foreach statement but doing the opposite and continue on the first three items.为简单起见,您可以重复foreach语句,但做相反的事情并continue前三个项目。

<?php foreach ($_productCollection as $_product): ?>
<?php
    $count++; // Note that first iteration is $count = 1 not 0 here.
    if($count <= 3) continue; // Skip the iteration unless 4th or above.
?>
<li class="category-row-list-item">
    <a class="product-name" href="<?php echo $_product->getProductUrl() ?>">
        <?php echo $this->htmlEscape($_product->getName()) ?>
    </a>
</li>
<?php endforeach ?>

The keyword continue is used in loops to skip the current iteration without exiting the loop, in this case it makes PHP go directly back to the first line of the foreach -statement, thus increasing counter to 4 (since 4th, 5th and 6th is what we're after) before passing the if statement.关键字continue在循环中用于跳过当前迭代而不退出循环,在这种情况下,它使 PHP 直接回到foreach语句的第一行,从而将计数器增加到 4(因为第 4、第 5 和第 6 是什么我们在之后)在传递if语句之前。

Commentary on the approach对方法的评论

I kept it coherent with your existing solution but a more clean way in this case would probably be to use the built in Collection Pagination.我使它与您现有的解决方案保持一致,但在这种情况下,更简洁的方法可能是使用内置的 Collection Pagination。

If you use ->setPageSize(3) you can simply iterate the collection to get the first three products and then use ->setCurPage(2) to get the second page of three items.如果您使用->setPageSize(3)您可以简单地迭代集合以获取前三个产品,然后使用->setCurPage(2)获取三个项目的第二页。

I'm linking this blog post on the topic here just to give you an example of how it's used but since I don't know your comfort level in working with collections I retain my first answer based on your existing code.我在此处链接 此主题的博客文章,只是为了给您提供一个如何使用它的示例,但由于我不知道您在使用集合时的舒适程度,所以我根据您现有的代码保留了我的第一个答案。

Something like that with modulo function for have new array each 3 items :类似于模函数的东西,每 3 个项目都有一个新数组:

$count = 1;
$count_change = 1;
$key = 0;
$yourList = array(
    "1",
    "2",
    "3",
    "4",
    "5",
    "6",
);
foreach ($yourList as $item) {
    if (!isset($$new_list)) {
        $new_list = "list" . $count_change . "";
        $$new_list = array();
    }
    if ($count % 3 == 0) {
        $key = 0;
        $count_change++;
        $new_list = "list" . $count_change . "";
        $$new_list = array();
    }
    $$new_list[$key] = $item;
    $count++;
    $key++;
}

Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM