Jump to content

continue loop


mraza

Recommended Posts

hi i am stuck in a loop please can someone check what is miss. I wants to quit from loop and go to next title if that tile is found but cant figure out how should i go. my code:

$feed_url = "http://www.myfeed.com/feed/";
$xml = simplexml_load_file($feed_url);
foreach ($xml->channel->item as $item) { 

$ns_content = $item->children('http://purl.org/rss/1.0/modules/content/');
$title = $item->title;
// opening file which contain titles already
$content = file('file.txt');
foreach ($content as $aline) {
    if (strstr($title, $aline)) {
        $found =  true;
    } 
}
// i dont wants to print that $title which is already in file.txt so loop continue.
	if($found) {
	continue;
	}

echo $title . "<br>" ;

}

 

Problem is with that continue statement if a title is matched from file.txt it will display me nothing, i only wants to skip that title and echo other titles. thanks for any help

 

Link to comment
https://forums.phpfreaks.com/topic/218736-continue-loop/
Share on other sites

There are better ways to do this, if the titles are actually identical then it will be even easier, but your problem is that once $found is set to true it is always true.  You need to set it to false at the beginning of the loop somewhere.

Link to comment
https://forums.phpfreaks.com/topic/218736-continue-loop/#findComment-1134474
Share on other sites

thanks but it is not showing anything either

$found =  false;
foreach ($xml->channel->item as $item) { 
$ns_content = $item->children('http://purl.org/rss/1.0/modules/content/');
$title = $item->title;
$content = file('file.txt');
foreach ($content as $aline) {
    if (strstr($title, $aline)) {
        $found =  true;
    } 
}
	if($found){ 
	continue;
	}

	echo $title . "<br>" ;


}

it display only a blank page. if i remove continue; it shows me all titles from feed without problem. in my file.txt all titles are on a new line so are identical.

Link to comment
https://forums.phpfreaks.com/topic/218736-continue-loop/#findComment-1134483
Share on other sites

foreach ($xml->channel->item as $item) {
   $found =  false;

 

But like I said if the titles are identical then this is easier and faster:

 

$feed_url = "http://www.myfeed.com/feed/";
$xml = simplexml_load_file($feed_url);
$content = file('file.txt');

foreach ($xml->channel->item as $item) { 
  $title = $item->title;

  if(!in_array($title, $content)) {
    echo $title;
  }
}

Link to comment
https://forums.phpfreaks.com/topic/218736-continue-loop/#findComment-1134489
Share on other sites

hm thanks again sir but for some reason !in_array is also not working properly, if there is title in file it still display title with above code.

in my other script what i do is run the script and save that title of feed in a file here is code

$ourFileName = "file.txt";
$ourFileHandle = fopen($ourFileName, 'a') or die("can't open file");
fwrite($ourFileHandle, "$title\n");
fclose($ourFileHandle);

so it save that title in file.txt so next time when i run above script it should not read that feed again which has same title in file.txt, is there are any other thing i can try? thanks again

Link to comment
https://forums.phpfreaks.com/topic/218736-continue-loop/#findComment-1134566
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.