The_WJM Posted April 18, 2014 Share Posted April 18, 2014 I want to loop a RSS feed into a table and with a maximum of 3 columns. Example: Feed retrieves 12 items, insert into table of 3 columns X 4 rows. 24 items retrieved, 3 columns X 8 rows, etc. I've figured out how to specify maximum number of items to retrieve and display in a table format, but I don't know how to limit the number of columns to begin a new row. Here's what I have: <table border="1"> <tr> <?php $rss = new DOMDocument(); $rss->load('http://pipes.yahoo.com/pipes/pipe.run?_id=e9ba5a52dfb7a537fa537445c6031349&_render=rss'); $feed = array(); foreach ($rss->getElementsByTagName('item') as $node) { $item = array ( 'title' => $node->getElementsByTagName('title')->item(0)->nodeValue, 'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue, 'link' => $node->getElementsByTagName('link')->item(0)->nodeValue, ); array_push($feed, $item); } $limit = 12; for($x=0;$x<$limit;$x++) { $title = str_replace(' & ', ' & ', $feed[$x]['title']); $link = $feed[$x]['link']; $description = $feed[$x]['desc']; echo '<td><a href="'.$link.'" title="'.$title.'">'.$title.'</a><br />'.$description.'</td>'; } ?> </tr> </table> Link to comment https://forums.phpfreaks.com/topic/287869-rss-feed-display-limit-number-of-columns/ Share on other sites More sharing options...
taquitosensei Posted April 18, 2014 Share Posted April 18, 2014 This is an example and you'll have to make it work with your code but something like this would work. $items=array(1,2,3,4,5,6,7,; $html="<table>"; $c=0; foreach($items as $item) { if($c==0) { $html.="<tr>"; } if($c%3==0) { $html.="</tr>"; $c=0; } $html.="<td>".$item."</td>"; $c++; } $html.="</table>"; echo $html; Link to comment https://forums.phpfreaks.com/topic/287869-rss-feed-display-limit-number-of-columns/#findComment-1476614 Share on other sites More sharing options...
The_WJM Posted April 18, 2014 Author Share Posted April 18, 2014 Oh so close. It outputs in 3 columns, however, it just repeats the first item in the feed. Link to comment https://forums.phpfreaks.com/topic/287869-rss-feed-display-limit-number-of-columns/#findComment-1476630 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.