forzatio Posted July 5, 2007 Share Posted July 5, 2007 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. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 5, 2007 Share Posted July 5, 2007 Im not 100% sure from your description, but i think this is what you are after: http://www.phpfreaks.com/forums/index.php/topic,95426.0.html Quote Link to comment Share on other sites More sharing options...
forzatio Posted July 5, 2007 Author Share Posted July 5, 2007 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. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 5, 2007 Share Posted July 5, 2007 That is exacty what that link shows you how to do. If you show us your existing code, we might be able to help you modify it. Quote Link to comment Share on other sites More sharing options...
forzatio Posted July 5, 2007 Author Share Posted July 5, 2007 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. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 5, 2007 Share Posted July 5, 2007 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>"; } ?> Quote Link to comment Share on other sites More sharing options...
forzatio Posted July 5, 2007 Author Share Posted July 5, 2007 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.