Reputation: 4465
I am trying to create a nested for loop in PHP but the code is producing THOUSANDS of returns.
What I am trying to do is read rss feeds, (the two in the array) and then comparing the posts in the feed what's in my database. If the new title of a post is not the same as one in my database, it will add it to my database.
I don't mean to just drop a bunch of my code but I've never created a nested for loop in PHP and i'm not even sure if this is possible.
$url_feeds = array('0' => array('user_id' => '1','feed' => 'http://feeds.abcnews.com/abcnews/topstories'),
'1' => array('user_id' => '2','feed' => 'http://feeds.reuters.com/reuters/MostRead'));
for($i=0;$i<sizeof($url_feeds);$i++){
$user_id = $url_feeds[$i]['user_id'];
// echo $user_id;
$feed = $url_feeds[$i]['feed'];
//echo $feed;
$abc =$this->get_rss_feeds($feed); //This returns an array of all RSS feed posts.
//print_r($abc);
$post = new post_model();
//print_r($res);
for($i=0;$i<sizeof($abc);$i++)
{
$link = $abc[$i]['link'];
$title = $abc[$i]['title'];
// echo $title;
$date_published = $abc[$i]['pubDate'];
$res = $post->get_new_user_posts($user_id); // This returns an array of posts in my database (It's sent user_id meaning, the id of the feed see top above array)
foreach ($res as $key => $value)
{
$new_title = $value->title;
// echo $new_title;
//echo $title;
$new_link = $value->link;
if ($title != $new_title)
{
// echo 'NOT A MATCH'.$i;
$update_new_user_posts = $post->post_to_new_user_posts($user_id,$link,$title,$date_published);
}
}
}
}
Upvotes: 0
Views: 390
Reputation: 2820
You are using the same variable $i in both loops. Usually this is incorrect and will result in uncontrolled behavior.
Upvotes: 1