Jump to content

RSS feed display, limit number of columns


The_WJM

Recommended Posts

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();
$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>

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;

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.