Jump to content

PHP Warning: Invalid argument supplied for foreach()


ebchost

Recommended Posts

[27-Jul-2011 08:49:35] PHP Warning:  Invalid argument supplied for foreach() in /home/..../blog.php on line 115

[27-Jul-2011 08:49:35] PHP Warning:  simplexml_load_file() expects parameter 2 to be a class name derived from SimpleXMLElement, '0' given in /home/...blog.php on line 107

 

 

 

$rss_link 		= simplexml_load_file($rss_izvor, LIBXML_ERR_NONE );
			//var_dump($rss_izvor);

			if($rss_izvor)
			{		

				$items = $rss_link->channel->item;

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

						$link 			= $item->link;

						$published_on 		= $item->pubDate;							

						$description 		= $item->description;
						$descritpion_strip	= strip_tags($description);
						$description_trim 	= trim($descritpion_strip);					


						$image_object = preg_match_all('/src="(.*?)"/', $description, $matches);
						$image = str_replace( '"','',$matches[0][0]);
						$image2 = str_replace( 'src=','',$image);


								$descritpion_trim1 	= mysql_real_escape_string($description_trim);
								$title1 		= mysql_real_escape_string($title);
								$link1 			= mysql_real_escape_string($link);
								$datum			= date('Y-m-d H:m:s');
								$datum1			= mysql_real_escape_string($datum);								

								$provera 	= mysql_query("SELECT * FROM `rss_vesti` WHERE `IZVOR` = '$link1'") or die(mysql_error());
								$poklapanja 	= mysql_num_rows($provera);
								//echo "Poklapanja: $poklapanja";

								if( $poklapanja == 0)
									{
										$sql = "INSERT INTO `rss_vesti` VALUES('', '$rss_izvor_id', '$title1', '$image2', '$descritpion_trim1', '$link1', '$datum1', '$rss_kategorija_id', '','')";
                           								mysql_query($sql)or die(mysql_error());
                           							}
                           							
                           			}
           }

 

 

NOTE: First line in the code is line 107, and in line is "foreach($items as $item)" 115 line.

 

How to solve this ?

Thanks.

Link to comment
Share on other sites

An the end of the post is code of the function for reading RSS (source_add_all)

 

with out this, i write som record, with this, i write 0 record in database

 

 

this problem show up after when i add "LIBXML_ERR_NONE"

in line

$rss_link = simplexml_load_file($rss_izvor,  LIBXML_ERR_NONE)

 

before that, i have brake of function with out error 1 time of 15-20 times

 

function source_add_all($table, $id)
{
	$table = mysql_real_escape_string($table); 
	$id    = (int)$id;		

	$query = mysql_query("SELECT * FROM `rss_sajtovi`") or die(mysql_error());

	while($row= mysql_fetch_assoc($query))
		{
			$rss_izvor_id 		= $row['ID_SAJTA'];
			$rss_izvor_naziv 	= $row['NAZIV'];
			$rss_izvor_logo 	= $row['LOGO'];
			$url_sajta 		= $row['URL'];
			$rss_izvor 		= $row['RSS'];
			$rss_kategorija 	= $row['kategorija'];
			$rss_kategorija_id 	= $row['kategorija_id'];
			$rss_odobren 		= $row['odobren'];
			$korisnik_id 		= $row['korisnik_id'];
			$kontrola 		= $row['kontrola'];		



			$rss_link 		= simplexml_load_file($rss_izvor,  LIBXML_ERR_NONE);
			//var_dump($rss_izvor);

			if($rss_izvor)
			{		

				$items = $rss_link->channel->item;
				if(is_array($items))
					{
  							echo "that variable is an array ";
						foreach($items as $item)
							{
								$title 			= $item->title;
								$link 			= $item->link;
								$published_on 		= $item->pubDate;							
								$description 		= $item->description;
								$descritpion_strip	= strip_tags($description);
								$description_trim 	= trim($descritpion_strip);					

								$image_object = preg_match_all('/src="(.*?)"/', $description, $matches);
								$image = str_replace( '"','',$matches[0][0]);
								$image2 = str_replace( 'src=','',$image);

								$descritpion_trim1 	= mysql_real_escape_string($description_trim);
								$title1 		= mysql_real_escape_string($title);
								$link1 			= mysql_real_escape_string($link);
								$datum			= date('Y-m-d H:m:s');
								$datum1			= mysql_real_escape_string($datum);								

								$provera 	= mysql_query("SELECT * FROM `rss_vesti` WHERE `IZVOR` = '$link1'") or die(mysql_error());
								$poklapanja 	= mysql_num_rows($provera);
								echo "Poklapanja: $poklapanja";

								if( $poklapanja == 0)
									{
										$sql = "INSERT INTO `rss_vesti` VALUES('', '$rss_izvor_id', '$title1', '$image2', '$descritpion_trim1', '$link1', '$datum1', '$rss_kategorija_id', '','')";
                           								mysql_query($sql)or die(mysql_error());
                           							}
                           							
                           					}
                           			}
                           			else{
  							echo "that variable isn't an array !";
					    }
			}
		}
	return $rss_izvor;
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.