displaying rss

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lawler_09
Forum Newbie
Posts: 8
Joined: Wed Nov 10, 2010 10:06 am

displaying rss

Post by lawler_09 »

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
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: displaying rss

Post by Celauran »

Are all the divs the same? Sounds like a simple loop would work fine. Do you have some sample code we can look at?
lawler_09
Forum Newbie
Posts: 8
Joined: Wed Nov 10, 2010 10:06 am

Re: displaying rss

Post by lawler_09 »

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;
}
?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: displaying rss

Post by Celauran »

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>';
}
lawler_09
Forum Newbie
Posts: 8
Joined: Wed Nov 10, 2010 10:06 am

Re: displaying rss

Post by lawler_09 »

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?
lawler_09
Forum Newbie
Posts: 8
Joined: Wed Nov 10, 2010 10:06 am

Re: displaying rss

Post by lawler_09 »

actually thinking about it what u suggested may work.

i will try it and let you know!
lawler_09
Forum Newbie
Posts: 8
Joined: Wed Nov 10, 2010 10:06 am

Re: displaying rss

Post by lawler_09 »

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>"; }
?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: displaying rss

Post by Celauran »

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>"; }
?>
No, that's not what I meant.

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>";
}
lawler_09
Forum Newbie
Posts: 8
Joined: Wed Nov 10, 2010 10:06 am

Re: displaying rss

Post by lawler_09 »

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
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: displaying rss

Post by McInfo »

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 - 1
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: displaying rss

Post by Celauran »

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
Ah, yes. Boneheaded mistake on my part.

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>";
}
lawler_09
Forum Newbie
Posts: 8
Joined: Wed Nov 10, 2010 10:06 am

Re: displaying rss

Post by lawler_09 »

absolute legend!!

thank you so much!!

I'll be sure to thank you in my final project report!! :D

much appreciate!!

P.S Thanks to McInfo as well :D
Post Reply