2

I have products which they will be shown horizontally in Grid page,however I want them to be sorted vertically and I have 4 coulmns
if we had a simple array the out put is

1  2 3 4
5  6 7 8
9 10 11 12

I want the output to be

1 4 7 10
2 5 8 11
3 6 9 12

I wrote a test page without magento a simple array, I want to do this on array of products but I really don't know how to do it? because I have array of products that will be loaded and I should change the order of them. do you have any other idea?

thanks in advance

<?php
$array1=array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25);
$column=4;
$count=count($array1);
$arr=array();
$k=intval($count/4);
$r=($count%4);
$num=0;
for($m=0; $m<4;$m++)
    for($n=0;$n<$k;$n++)
    {
        $arr[$m][$n]=$array1[$num];
    $num++;
    }
    /* 
     * build the remainders
     */
    for($i=0;$i<$r;$i++)
    {
        $arr[4][$i]=$array1[$num];  
        $num++;
    }
        //print the result
echo "<table>";
for($j=0;$j<$k;$j++)
{
echo "<tr>";

    for($i=0;$i<=4;$i++)
    {

    if($i==4 && $j<$r)
        {
            echo "<td>".$arr[$i][$j]."</td>";
        }
        else if($i!=4)
        echo "<td>".$arr[$i][$j]."</td>";   
    }
        echo "</tr>";
}
echo "</table>";
echo "arraysize is ".$count."<br>";
echo "division answer is".$k."<br>";;
echo "remainder is".$r;
?>

here is the list.phtml part

$_productCollection=$this->getLoadedProductCollection();
       <ol class="products-list" id="products-list">
        <?php foreach ($_productCollection as $_product): 

so the $_product array should be displayed vertically

                <?php // Product Image ?> 
                <a href="<?php echo $_product->getProductUrl() ?>"
//some other code
5

It's not so difficult to do, actually. It helps to think about this outside of the context of Magento as you did above; but your method could be simplified1, 2:

<?php

$cols       = 5; //set this to the # columns you want
$count      = 55;//number of total records
$list       = range(1,$count);
$rowLength  = ceil($count/$cols);
$array      = array();


$output = array_chunk($list, $rowLength);

for($i=0;$i<$rowLength;$i++){
    @array_walk($output,function($val) use ($i){
        echo $val[$i] . ' ';
    });
    echo PHP_EOL;
}

Which, when calculated for 5 columns and 32 count you get:

1 8 15 22 29
2 9 16 23 30
3 10 17 24 31
4 11 18 25 32
5 12 19 26
6 13 20 27
7 14 21 28

All that is happening here is that the $list array (a stand-in for your Magento collection) is getting chunked into multiple arrays sized by $rowLength. After that all we're doing is iterating through all the columns and outputting the row index at that column value! That's pretty cool.

enter image description here

Do this in Magento

Load up the collection and cast to an array:

$collection = Mage::getModel('catalog/product')->getCollection()->setPageSize(20);
$array = $collection->toArray();

Then we plug it into our method that uses array_chunk3:

$collection = Mage::getModel('catalog/product')->getCollection()->setPageSize(20)->load();
$collArray = $collection->toArray();

$cols       = 5; //set this to the # columns you want
$count      = count($collArray);//number of total records
$rowLength  = ceil($count/$cols);
$array      = array();

$output = array_chunk($collArray, $rowLength);

for($i=0;$i<$rowLength;$i++){
    @array_walk($output,function($val) use ($i){
        echo $val[$i]['sku'] . ' '; //echoing sku in the array 
    });
    echo PHP_EOL;
}

In this case I'm echoing only the sku out, but instead of issuing an echo you could instead populate an output array or even a Varien_Data_Collection so you can make use of this later.

I hope that helps! Best of luck.


  1. Errors are being suppressed because we are trying to access a nonexistent key (in the case of blank cells)
  2. The use of array_walk in these examples is trivial and could be replaced with a loop. However, if you were doing something more in-depth or you had a block method that already handled how to compile rows from column values, this would be an appropriate solution.
  3. Again, errors are suppressed here. You could instead of casting to an array attempt to retrieve it via collection ID. Or, better yet, guard the condition by testing if it isset before echoing.
  • 1
    +1 because you added an image. I like answers with images. – Marius Mar 11 '14 at 14:07
  • @marius yeah the content is questionable, but the image really sells it. – philwinkle Mar 11 '14 at 16:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.