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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

foreach ($rss->items as $item ) {

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




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


?>

 

I'm using magpierss for rss parsing, I'm having difficulty with breaking the foreach.

Link to comment
Share on other sites

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>";
}
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.