ebchost Posted July 27, 2011 Share Posted July 27, 2011 [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. Quote Link to comment https://forums.phpfreaks.com/topic/242936-php-warning-invalid-argument-supplied-for-foreach/ Share on other sites More sharing options...
WebStyles Posted July 27, 2011 Share Posted July 27, 2011 that probably means that $items is not an array. Quote Link to comment https://forums.phpfreaks.com/topic/242936-php-warning-invalid-argument-supplied-for-foreach/#findComment-1247801 Share on other sites More sharing options...
dreamwest Posted July 27, 2011 Share Posted July 27, 2011 You can also make $items an array before the script if $rss_link has no value $items = array(); Quote Link to comment https://forums.phpfreaks.com/topic/242936-php-warning-invalid-argument-supplied-for-foreach/#findComment-1247805 Share on other sites More sharing options...
ebchost Posted July 27, 2011 Author Share Posted July 27, 2011 that probably means that $items is not an array. how to skip this item? becose is imposible that all data is not array? Quote Link to comment https://forums.phpfreaks.com/topic/242936-php-warning-invalid-argument-supplied-for-foreach/#findComment-1247806 Share on other sites More sharing options...
WebStyles Posted July 27, 2011 Share Posted July 27, 2011 you can check it with something like this: if(is_array($items)){ echo '<br>$items is an array<br>'; }else{ echo '<br>Oppss. $items is NOT an array<br>'; } Quote Link to comment https://forums.phpfreaks.com/topic/242936-php-warning-invalid-argument-supplied-for-foreach/#findComment-1247810 Share on other sites More sharing options...
ebchost Posted July 27, 2011 Author Share Posted July 27, 2011 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; } Quote Link to comment https://forums.phpfreaks.com/topic/242936-php-warning-invalid-argument-supplied-for-foreach/#findComment-1247865 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.