• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 254
  • Last Modified:

Duplicate Data showing in while loop from RSS Feed

Hi All,

I have a while loop running to pick up my rss feed and display this information in the code below on my page. However this is working but it only shows the latest item in the rss and repeats it, so I see the same thing twice.

What I want to happen is to show the latest TWO articles only. Not the FIRST latest article repeated twice.

Any help on this would be great.

Thanks In Advance,

<?php
											
											$need_url="http://www.myfeedurl.com";
											$provider = "Feed Provider";
											$i=0;
											$doc = new DOMDocument();
											$doc->load($need_url);
											
											if(!$doc->load($need_url)) {
							    				echo '<div class="newsRight">';
								    				echo "<h4>Oops, Sorry!</h4>";
								    				echo "<p>We are unable to show you any news at this time but we will back online shortly.</p>";
							    				echo '</div>';
							    			}
											
											$employees = $doc->getElementsByTagName("item");
											foreach( $employees as $employee ) {
												while ($i<'2'){
													$i++;
													$names = $employee->getElementsByTagName("title");
													$name = $names->item(2)->nodeValue;
													$dates = $employee->getElementsByTagName("pubDate");
													$date = $dates->item(2)->nodeValue;
													$links= $employee->getElementsByTagName("link");
													$link= $links->item(2)->nodeValue;
													$descriptions= $employee->getElementsByTagName("description");
													$description= $descriptions->item(2)->nodeValue;
													
													$date = date('d / m / y');
													
													echo "<p><u>" . $name . "</u><br />";
													echo '<span class="date">' . $date . '</span><br />';
													$descriptionSummary = substr($description, 0, 160);
													echo nl2br($descriptionSummary); echo "…<br />";
													echo '<a href="' . $link . '" title="Read More" target="_self" class="readMore">Read more</a></p>';
												}
											}
										?>

Open in new window

0
piixeldesigns
Asked:
piixeldesigns
  • 6
  • 4
  • 3
1 Solution
 
COBOLdinosaurCommented:
the whilw loop executes twice for the first employee.  It does not get to the second one until until it processes the first one twice.

If I understand it while ($I<1) might give it to you or you might want to try:

for ($i-0; $i<2; $i++) and forget the while.  You should be able to keep the code intact this way:
for($i=0; $i<2; $i++) ) 
{
   $employee=$employees[$i];
   $names = $employee->getElementsByTagName("title");
   $name = $names->item(2)->nodeValue;
   $dates = $employee->getElementsByTagName("pubDate");
   $date = $dates->item(2)->nodeValue;
   $links= $employee->getElementsByTagName("link");
   $link= $links->item(2)->nodeValue;
   $descriptions= $employee->getElementsByTagName("description");
   $description= $descriptions->item(2)->nodeValue;
   $date = date('d / m / y');
   echo "<p><u>" . $name . "</u><br />";
   echo '<span class="date">' . $date . '</span><br />';
   $descriptionSummary = substr($description, 0, 160);
   echo nl2br($descriptionSummary); echo "…<br />";
   echo '<a href="' . $link . '" title="Read More" target="_self" class="readMore">Read more</a></p>';
}

Open in new window


BTW, whatever you used to post with formats terribly.  Please use a text editor if you are going to cut and paste.
0
 
piixeldesignsAuthor Commented:
Hey Thanks for this - I am giving it ago now.

Just one question do I have to still keep the foreach or can I remove that as well.

Thanks,

P.S. also is the second ) an error?
0
 
COBOLdinosaurCommented:
No you don't need the foreach and yes the extra ")" is an error from a fat fingered dinosaur. :^(
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
piixeldesignsAuthor Commented:
Thanks here is the error it is throwing now:

Fatal error: Call to a member function getElementsByTagName() on a non-object in.....on line 142 which is the first line of code straight after the "{"
0
 
COBOLdinosaurCommented:
Did you add the line:

$employee=$employees[$i];

After the "{" the way I have it in the code I posted?

If not try it with that line.  If you did it probably means that everywhere in the loop that you have $employee it will have to be changed to $employees[$i] so we reference the row directly.
0
 
piixeldesignsAuthor Commented:
I tried both but I am still getting the same error message....any more ideas on this?
0
 
COBOLdinosaurCommented:
Okay that means going back to the foreach without the while.

It will process the whole array, but you don't have to do anything with the data after the first two.  Probably should have tried that first, but some days I look for complicated insted of simple solutions.
0
 
Ray PaseurCommented:
Just a thought... Why not give us some test data?  I'll be glad to show you how I process RSS feeds.  But I can't do it if I am looking at http://www.myfeedurl.com, y'know?  Thanks, ~Ray
0
 
piixeldesignsAuthor Commented:
It's coming from a standard wordpress feed....for example....http://www.piixeldesigns.com/blog/wp-rss2.php

It's exactly like that.

Thanks Ray
0
 
piixeldesignsAuthor Commented:
Yes that's correct COBO but where I am stuck is how to stop it after the first 2 articles as I want to save on load time as well if possible.

Ray - I only want to display the latest TWO articles at any one time.

Thanks Guys,
0
 
Ray PaseurCommented:
You can copy this and install it on your server, or test it on my server, here:
http://www.laprbass.com/RAY_temp_pixeldesigns.php

Best regards, ~Ray
<?php // RAY_temp_pixeldesigns.php
error_reporting(E_ALL);
echo "<pre>";

// GET THE XML INTO AN OBJECT
$url = 'http://www.piixeldesigns.com/blog/wp-rss2.php';
$xml = file_get_contents($url);
$obj = SimpleXML_Load_String($xml, 'SimpleXMLElement', LIBXML_NOCDATA);

// ACTIVATE THIS TO SEE THE ENTIRE OBJECT
// print_r($obj);

// SHOW SOME INFORMATION FROM THE OBJECT
$c = (string)$obj->channel->title;
echo PHP_EOL . $c;

// SHOW SOME INFORMATION FROM THE FIRST TWO ITEMS
$k = 2;
foreach ($obj->channel->item as $item)
{
    $t = (string)$item->title;
    $u = (string)$item->link;
    $a = '<a href="' . $u . '">' . $t . '</a>';
    echo PHP_EOL . $a;
    $k--;
    if (!$k) break;
}

Open in new window

0
 
piixeldesignsAuthor Commented:
Thanks Ray - that's perfect works a charm.

My only question is what is: PHP_EOL??

Thanks,
0
 
Ray PaseurCommented:
Thanks for the points.  

PHP_EOL is a predefined constant that is OS-context aware.  It is the end-of-line character.  It's different in Mac and Windows and Unix - there are variants that include \r or \n or both.  If you use PHP_EOL instead of a specific set of \x characters you will always get the right results from PHP, and you will not have to detect the OS and change your code based on the OS.  PHP already knows the right choice for you!

Best regards, ~Ray
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 6
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now