I am attempting to modify a joomla module and I've got a small problem (which isn't joomla related, thus no code necessary).

I've got a foreach loop with a block of code inside it which shows articles. It repeats itself as numerous occasions while you arrange it within the admin panel. I wish to add the feature which makes this module display products on a lot more than 1 column. All I want may be the perk, I believe I've anything else covered.

Essentially how do you modify an easy foreach loop to ensure that it shows articles on several column?

Rather than this

a

b

c

d

e

I would like this

b ........ d

b ........ e

c

You will get the count of results and work in the middle if you are adhering with tables

$half_count = floor(count($entries) / 2);
for($i=0;$i<$half_count;$i++)
{
    echo '<tr>';
    echo '<td>' . $entries[$i] . '</td>';
    echo '<td>' . (isset($entries[$half_count + $i]) ? $entries[$half_count + $i]: '') . '</td>';
    echo '</tr>';
}

Here an easy method of doing it :

php > $arr = array(1,2, 3, 4, 5, 6, 7, 8, 9, 10);
php > for ($i=0; $i<count($arr); $i+=2) { print $arr[$i] . "\t" . $arr[$i+1] . "\n"; }
1       2
3       4
5       6
7       8
9       10

So far as I understand, you will not have the ability to get it done having a foreach statement however with a for.

For instance:

$iterations = (count($array_of_items) % 2) ? (count($array_of_items) / 2) + 1 : count($array_of_items) / 2;
for ($i = 0; $i <= $iterations; $i++) {
if (isset($array_of_items[$i+3]))
   echo $array_of_items[$i].'........'.$array_of_items[$i+3];
else
   echo $array_of_items[$i];
}

Rather easy code without so very little info but tend to result in the trick!

Rather than outputting within the for loop, I'd create two arrays from the articles within the for loop. Then loop through individuals arrays to produce your posts outdoors from the primary loop.

You've little info within the question, but here's something you could utilize

$arr = array("a","b","c","d","e","f");

for ($i = 0; $i<count($arr); $i++){
    echo $arr[$i]."    ". $arr[$i+3] ."\n";
    if($i == 2){ break;} //Modify 2 as more alphabets are added
}

Results

a    d
b    e
c    f

For any generic solution, presuming a packed array....

function show_table($data, $columns)
{
    $items=count($data);
    $iters=$items/$columns + ($items % $columns) ? 1 : 0;
    for ($y=0; $y<$iters; $y++) {
      for ($x=0; $x<$columns; $x++) {
         $offset=$y*$columns + $x;
         if ($offset<$items) print $data[$offset] . ' ';
      }
      print "\n";
    }
}