0

I have a list of 6 products that i want to split in 2 lists of 3 products next to each other. The list are made within a foreach loop, the first list stops after the count == 2, so 3 items wil be displayed. The second list should start with the fourth item. How can i achieve this?

This is wat makes the first list of 3 items:

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

0

2 Answers 2

1

For simplicity you could repeat the foreach statement but doing the opposite and continue on the first three items.

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

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.

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.

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@n00bly Great! Btw there is a dedicated Magento stack exchange on magento.stackexchange.com that might give better/more Magento specific solutions for your future questions!
0

Something like that with modulo function for have new array each 3 items :

$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.

1 Comment

You should probably explain what it does, especially when using variable variables since that's a rather advanced concept (and in this case produces variables that have numeric names which have fallpits all on its own). It's considered bad practice to recreate functionality that is native to Magento, there are already ways to paginate/split into group sizes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.