displaying rss
Moderator: General Moderators
displaying rss
hey guys
i hope you can help.
I've created 5 separate divs, and want to display data from an xml file in them.
In the first div i want to display just the data from the first item in the xml file.
then in the second div i want to display the second item from the xml file and so forth.
any ideas?
thanks
i hope you can help.
I've created 5 separate divs, and want to display data from an xml file in them.
In the first div i want to display just the data from the first item in the xml file.
then in the second div i want to display the second item from the xml file and so forth.
any ideas?
thanks
Re: displaying rss
Are all the divs the same? Sounds like a simple loop would work fine. Do you have some sample code we can look at?
Re: displaying rss
im new to all this really so bare with me haha
i basically have 5 divs like so, and want to display 1 of the 5 items in my xml file, in each div so
<div id="feed1">
<!--DISPLAY ITEM 1 HERE-->
</div>
<div id="feed2">
<!--DISPLAY ITEM 2 HERE-->
</div>
<div id="feed3">
<!--DISPLAY ITEM 3 HERE-->
</div>
<div id="feed4">
<!--DISPLAY ITEM 4 HERE-->
</div>
<div id="feed5">
<!--DISPLAY ITEM 5 HERE-->
</div>
the only code i managed to find was this.
which works but displays all 5 items in every div, whereas i want 1 in each in order
<?php
$xml = "rss.xml";
$xmlDoc = new DOMDocument();
$xmlDoc->load($xml);
// get the item tag
$items = $xmlDoc->getElementsByTagName("item");
// get the items count
$itemsCount = $items->length;
// start to iterate through the item node
for ($i=0; $i<$itemsCount; $i++) {
$itemTitle = $items->item($i)->getElementsByTagName("title")->item(0)->childNodes->item(0)->nodeValue;
$itemURL = $items->item($i)->getElementsByTagName("link")->item(0)->childNodes->item(0)->nodeValue;
$item_desc = $items->item($i)->getElementsByTagName("description")->item(0)->childNodes->item(0)->nodeValue;
echo $itemTitle;
}
?>
i basically have 5 divs like so, and want to display 1 of the 5 items in my xml file, in each div so
<div id="feed1">
<!--DISPLAY ITEM 1 HERE-->
</div>
<div id="feed2">
<!--DISPLAY ITEM 2 HERE-->
</div>
<div id="feed3">
<!--DISPLAY ITEM 3 HERE-->
</div>
<div id="feed4">
<!--DISPLAY ITEM 4 HERE-->
</div>
<div id="feed5">
<!--DISPLAY ITEM 5 HERE-->
</div>
the only code i managed to find was this.
which works but displays all 5 items in every div, whereas i want 1 in each in order
<?php
$xml = "rss.xml";
$xmlDoc = new DOMDocument();
$xmlDoc->load($xml);
// get the item tag
$items = $xmlDoc->getElementsByTagName("item");
// get the items count
$itemsCount = $items->length;
// start to iterate through the item node
for ($i=0; $i<$itemsCount; $i++) {
$itemTitle = $items->item($i)->getElementsByTagName("title")->item(0)->childNodes->item(0)->nodeValue;
$itemURL = $items->item($i)->getElementsByTagName("link")->item(0)->childNodes->item(0)->nodeValue;
$item_desc = $items->item($i)->getElementsByTagName("description")->item(0)->childNodes->item(0)->nodeValue;
echo $itemTitle;
}
?>
Re: displaying rss
What if you moved the divs into the loop as well?
Code: Select all
// start to iterate through the item node
for ($i=0; $i<$itemsCount; $i++) {
echo '<div id="feed' . $i . '">;
$itemTitle = $items->item($i)->getElementsByTagName("title")->item(0)->childNodes->item(0)->nodeValue;
$itemURL = $items->item($i)->getElementsByTagName("link")->item(0)->childNodes->item(0)->nodeValue;
$item_desc = $items->item($i)->getElementsByTagName("description")->item(0)->childNodes->item(0)->nodeValue;
echo $itemTitle;
echo '</div>';
}Re: displaying rss
i thought about that but i think it might mess up the way it looks.
http://www.zombiedesigns.co.uk/my_anthems_v2/index.php
if you look at my site, you will see each of the 5 boxes i wish to display data in.
I'm guessing there's no easy as to say "pick first item from file, and echo headline" in one div, then a separate bit of code in the next div to pick the 2 item?
http://www.zombiedesigns.co.uk/my_anthems_v2/index.php
if you look at my site, you will see each of the 5 boxes i wish to display data in.
I'm guessing there's no easy as to say "pick first item from file, and echo headline" in one div, then a separate bit of code in the next div to pick the 2 item?
Re: displaying rss
actually thinking about it what u suggested may work.
i will try it and let you know!
i will try it and let you know!
Re: displaying rss
i tried it but i couldn't get it to work
did you mean:
for ($i=0; $i<$itemsCount; $i++) {
$itemTitle = $items->item($i)->getElementsByTagName("title")->item(0)->childNodes->item(0)->nodeValue;
$itemURL = $items->item($i)->getElementsByTagName("link")->item(0)->childNodes->item(0)->nodeValue;
$item_desc = $items->item($i)->getElementsByTagName("description")->item(0)->childNodes->item(0)->nodeValue;
echo "<div id=\"t1\">".$itemTitle."</div>";
echo "<div id=\"t2\">".$itemTitle."</div>";
echo "<div id=\"t3\">".$itemTitle."</div>";
echo "<div id=\"t4\">".$itemTitle."</div>";
echo "<div id=\"t5\">".$itemTitle."</div>"; }
?>
did you mean:
for ($i=0; $i<$itemsCount; $i++) {
$itemTitle = $items->item($i)->getElementsByTagName("title")->item(0)->childNodes->item(0)->nodeValue;
$itemURL = $items->item($i)->getElementsByTagName("link")->item(0)->childNodes->item(0)->nodeValue;
$item_desc = $items->item($i)->getElementsByTagName("description")->item(0)->childNodes->item(0)->nodeValue;
echo "<div id=\"t1\">".$itemTitle."</div>";
echo "<div id=\"t2\">".$itemTitle."</div>";
echo "<div id=\"t3\">".$itemTitle."</div>";
echo "<div id=\"t4\">".$itemTitle."</div>";
echo "<div id=\"t5\">".$itemTitle."</div>"; }
?>
Re: displaying rss
No, that's not what I meant.lawler_09 wrote:i tried it but i couldn't get it to work
did you mean:Code: Select all
for ($i=0; $i<$itemsCount; $i++) { $itemTitle = $items->item($i)->getElementsByTagName("title")->item(0)->childNodes->item(0)->nodeValue; $itemURL = $items->item($i)->getElementsByTagName("link")->item(0)->childNodes->item(0)->nodeValue; $item_desc = $items->item($i)->getElementsByTagName("description")->item(0)->childNodes->item(0)->nodeValue; echo "<div id=\"t1\">".$itemTitle."</div>"; echo "<div id=\"t2\">".$itemTitle."</div>"; echo "<div id=\"t3\">".$itemTitle."</div>"; echo "<div id=\"t4\">".$itemTitle."</div>"; echo "<div id=\"t5\">".$itemTitle."</div>"; } ?>
You just need one instance of <div> inside the loop. Since the loop is going to run $itemsCount times, $itemsCount divs will be created.
Code: Select all
for ($i=1; $i<=$itemsCount; $i++) {
$itemTitle = $items->item($i)->getElementsByTagName("title")->item(0)->childNodes->item(0)->nodeValue;
$itemURL = $items->item($i)->getElementsByTagName("link")->item(0)->childNodes->item(0)->nodeValue;
$item_desc = $items->item($i)->getElementsByTagName("description")->item(0)->childNodes->item(0)->nodeValue;
echo "<div id=\"t" . $i . "\">".$itemTitle."</div>";
}
Re: displaying rss
ahhh i see!!
i'm nearly there!! i get the error:
fatal error: call to a member fucntion getelementsbytagname() on a non-object
BUT it kinda works. it shows 2 where 1 should be, 3 where 2 should be etc etc but misses out the 1st one
i'm nearly there!! i get the error:
fatal error: call to a member fucntion getelementsbytagname() on a non-object
BUT it kinda works. it shows 2 where 1 should be, 3 where 2 should be etc etc but misses out the 1st one
Re: displaying rss
Code: Select all
for ($i = 1; $i <= $itemsCount; $i++); // Starts at 1, Goes to $itemsCount
for ($i = 0; $i < $itemsCount; $i++); // Starts at 0, Goes to $itemsCount - 1Re: displaying rss
Ah, yes. Boneheaded mistake on my part.lawler_09 wrote:BUT it kinda works. it shows 2 where 1 should be, 3 where 2 should be etc etc but misses out the 1st one
Code: Select all
for ($i=0; $i<$itemsCount; $i++) {
$itemTitle = $items->item($i)->getElementsByTagName("title")->item(0)->childNodes->item(0)->nodeValue;
$itemURL = $items->item($i)->getElementsByTagName("link")->item(0)->childNodes->item(0)->nodeValue;
$item_desc = $items->item($i)->getElementsByTagName("description")->item(0)->childNodes->item(0)->nodeValue;
echo "<div id=\"t" . ($i + 1) . "\">".$itemTitle."</div>";
}Re: displaying rss
absolute legend!!
thank you so much!!
I'll be sure to thank you in my final project report!!
much appreciate!!
P.S Thanks to McInfo as well
thank you so much!!
I'll be sure to thank you in my final project report!!
much appreciate!!
P.S Thanks to McInfo as well