Forum Moderators: coopster

Message Too Old, No Replies

Trying to reset $var (if $var = 0 reset $var and start over)

Can't get my RSS news script to work.

         

jaysin

9:46 pm on Nov 5, 2005 (gmt 0)



Hi, I'm trying to write an RSS news script for my site so that it will pull relevant news related to the section of the site... but the thing is some of the sections have no news... so I would like it to then pull news from the main topic from the site. I'm not sure how to get it to do this though. I've tried everything.

Right now I just have it displaying: echo "There is no news on $section."; if there is no news... Instead of displaying that I would like it to pull news using the variable $topic if it can't find any news from that section...

Here is the script:

<?php
$filename = "http://search.msn.com/news/results.aspx?q=$section&format=rss&FORM=RSNR";
$feed = implode('', file($filename));

preg_match_all('#<title>(.*?)</title>#', $feed, $title, PREG_SET_ORDER);
preg_match_all('#<link>(.*?)</link>#', $feed, $link, PREG_SET_ORDER);
preg_match_all('#<description>(.*?)</description>#', $feed, $description, PREG_SET_ORDER);

$nr = count($title);

if($nr == 1){

echo "There is no news on $section."; // Would I tell it to reset to $topic here?
}

elseif($nr > 1){

for ($counter = 1; $counter < 11; $counter++ ){

if(empty($title[$counter][1])){
echo "";
}
elseif(!empty($title[$counter][1])){

$title[$counter][1] = str_replace("&amp;", "&", $title[$counter][1]);
$title[$counter][1] = str_replace("&apos;", "'", $title[$counter][1]);

$description[$counter][1] = str_replace("&amp;", "&", $description[$counter][1]);
$description[$counter][1] = str_replace("&apos;", "'", $description[$counter][1]);

echo "<p>".$title[$counter][1]."</p>";
echo "<p>".$description[$counter][1]."</p>";
echo "<a href=\"".$link[$counter][1]."\">Read more</a><br>";
}
}

}

?>

Anyango

5:49 am on Nov 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I would make this code a Function which will take a paremeter to fetch news for and returns an array with its results, first call the function for $section and if no results are returned you can call it for $topic.

Pseudo will be like:

$RecordSet=fetchNews($section);
if($RecordSet['count']==0)
{
$RecordSet2=fetchNews($topic);
}

I am sorry if i misunderstood your question.

Kami