Forum Moderators: coopster
Story so far..
I have an xml file with some news items, like this
<?xml version="1.0"?>
<latestnews>
<item>item 1 blah blah</item>
<item>item 2 monkeys live on the moon</item>
<item>item 3 more stuff</item>
<item>item 4 swedish guy grows extra head</item>
<item>item 5 even more news stuff</item>
<item>item 6 table cloths are dangerous</item>
</latestnews>
Now, what i would like to do, is using that one file, to just echo a certain amount, say the 3 most recent entries (added in from the top). I've worked that bit out using this
// set name of XML file
$file = "news.xml";
// load file
$xml = simplexml_load_file($file) or die ("Unable to load XML file!");
// access each <item>
echo $xml->item[0] . "<br />";
echo $xml->item[1] . "<br />";
echo $xml->item[2] . "<br />";
So far, so good...
However, i would also like (on another page) to list all the items in that xml file, in order. I can do it if i encase the <item>'s with <news>, but then that messes up the first bit of code to read the first 3...i can get one or the other working, but not both..the code im using to read all of them is a foreach statement like so
foreach($news as $latestnews)
{
echo"<p>{$news->latestnews}</p>";
}
Any ideas or tips to make it work would be appreciated
:)
foreach($xml->item as $news)
{
echo "<p>$news</p>";
}
Try that :)
[edited by: eelixduppy at 8:21 pm (utc) on July 1, 2008]