Jump to content

[SOLVED] ForEach limited for two items per column


forzatio

Recommended Posts

Hello I have a foreach which outputs all items next to eachother.

I want to limit the output $description to only 2 items per column.

 

How can I echo this in a way that only 2 items are shown per column/row ?

 

This

echo "<td>$description</td>";

    is not limited for 2 items, as you can see.

hello, it is not exactly what I mean.

 

$description > displays an image everytime in a new <td> because it's using foreach.

Now I want only two images next to eachother and then two images again on a row below the previous.

 

so like:      <tr><td>image1</td><td>image2</td></tr>   

 

                <tr><td>image3</td><td>image4</td></tr>

 

but then the php should echo it, but because it's a foreach I can't say image1,image2 etc.

Well it should just be a case of applying the techniques used in the link i showed you.

 

<?php
$i = 0;
$max_columns = 2;
foreach ($rss->items as $item ) {

$description = $item[description];
	$url =		$item[link];
$title =	$item[title];

if($i == 0)
   echo "<tr>";		
echo "<td>$description</td>";
if(++$i == $max_columns) 
       {
           echo "</tr>";
           $i=0;
       }  // end if 
}
if($i < $max_columns)
{
    for($j=$i; $j<$max_columns;$j++)
        echo "<td> </td>";
}
?>

Well it should just be a case of applying the techniques used in the link i showed you.

 

<?php
$i = 0;
$max_columns = 2;
foreach ($rss->items as $item ) {

$description = $item[description];
	$url =		$item[link];
$title =	$item[title];

if($i == 0)
   echo "<tr>";		
echo "<td>$description</td>";
if(++$i == $max_columns) 
       {
           echo "</tr>";
           $i=0;
       }  // end if 
}
if($i < $max_columns)
{
    for($j=$i; $j<$max_columns;$j++)
        echo "<td> </td>";
}
?>

hello, the other thread confused me because of a lot of "counter" code, which in that way lays a bit far away from me.

that solution works though, many thanks.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.