Jump to content

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>
Edited by The_WJM

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;

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.