WeirdMystery Posted May 20, 2010 Share Posted May 20, 2010 Somehow, my two loops which parse information from an RSS file don't seem to be looping correctly... For the inner loop, it only counted totally 348 loops, where it is supposed to be 360 loops. However, when I printed $i, it counted 18. When I printed the outer loop, it counted 20. So 18 * 20 = 360. I have no idea how this could have possibly happened. for($i=0;$i<count($arrFeeds);$i++) { $l++; $title_players = explode(" - ", $arrFeeds[$i]['title']); $description_image = $arrFeeds[$i]['desc']; $description_image = str_replace(array ("<p>","</p>"), "", $description_image); $description_image = explode("</a>", $description_image); $title = $title_players[0]; $players_wrong = explode(" ", $title_players[1]); $players = $players_wrong[0]; $date = $arrFeeds[$i]['date']; $link = $arrFeeds[$i]['link']; $gameid = explode("=", $link); $gameid = $gameid[1]; $description = $description_image[1]; $image = $description_image[0] . "</a>\n"; $image_url = explode("<img src=\"", $image); $image_url = explode("\" width", $image_url[1]); $image_url = $image_url[0]; $q = mysql_query("SELECT * FROM `games` WHERE `gameid` = '" . $gameid . "'") or die(mysql_error()); $r = mysql_num_rows($q); $mysql_safe_description = mysql_real_escape_string($description); $mysql_safe_title = mysql_real_escape_string($title); if ($r == 0) { $q = mysql_query("INSERT INTO `games` (id, gameid, players, gamename, gamelink, lastupdated, description, imageurl) VALUES ('NULL', '$gameid', '$players', '$mysql_safe_title', '$link', '$date', '$mysql_safe_description', '$image_url')") or die(mysql_error()); $q = mysql_query("INSERT INTO `players` (id, timestamp, gameid, players) VALUES ('NULL', NOW(), '$gameid', '$players')") or die(mysql_error()); } else { $q = mysql_query("UPDATE `games` SET lastupdated = '$date', description = '$mysql_safe_description', players = '$players' WHERE `gameid` = '$gameid'") or die(mysql_error()); $q = mysql_query("INSERT INTO `players` (id, timestamp, gameid, players) VALUES ('NULL', NOW(), '$gameid', '$players')") or die(mysql_error()); } } This is my code: <?php #!/usr/bin/php //Config File Contents //define("pages_to_parse" 20); require_once("main.php"); $l = 0; for ($j=0; $j < pages_to_parse; $j++) { $doc = new DOMDocument(); $doc->load('http://www.roblox.com/games.aspx?g=all&p=' . $j . '&feed=rss'); $arrFeeds = array(); foreach ($doc->getElementsByTagName('item') as $node) { $itemRSS = array ( 'title' => $node->getElementsByTagName('title')->item(0)->nodeValue, 'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue, 'link' => $node->getElementsByTagName('link')->item(0)->nodeValue, 'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue ); array_push($arrFeeds, $itemRSS); } for($i=0;$i<count($arrFeeds);$i++) { $l++; $title_players = explode(" - ", $arrFeeds[$i]['title']); $description_image = $arrFeeds[$i]['desc']; $description_image = str_replace(array ("<p>","</p>"), "", $description_image); $description_image = explode("</a>", $description_image); $title = $title_players[0]; $players_wrong = explode(" ", $title_players[1]); $players = $players_wrong[0]; $date = $arrFeeds[$i]['date']; $link = $arrFeeds[$i]['link']; $gameid = explode("=", $link); $gameid = $gameid[1]; $description = $description_image[1]; $image = $description_image[0] . "</a>\n"; $image_url = explode("<img src=\"", $image); $image_url = explode("\" width", $image_url[1]); $image_url = $image_url[0]; $q = mysql_query("SELECT * FROM `games` WHERE `gameid` = '" . $gameid . "'") or die(mysql_error()); $r = mysql_num_rows($q); $mysql_safe_description = mysql_real_escape_string($description); $mysql_safe_title = mysql_real_escape_string($title); if ($r == 0) { $q = mysql_query("INSERT INTO `games` (id, gameid, players, gamename, gamelink, lastupdated, description, imageurl) VALUES ('NULL', '$gameid', '$players', '$mysql_safe_title', '$link', '$date', '$mysql_safe_description', '$image_url')") or die(mysql_error()); $q = mysql_query("INSERT INTO `players` (id, timestamp, gameid, players) VALUES ('NULL', NOW(), '$gameid', '$players')") or die(mysql_error()); } else { $q = mysql_query("UPDATE `games` SET lastupdated = '$date', description = '$mysql_safe_description', players = '$players' WHERE `gameid` = '$gameid'") or die(mysql_error()); $q = mysql_query("INSERT INTO `players` (id, timestamp, gameid, players) VALUES ('NULL', NOW(), '$gameid', '$players')") or die(mysql_error()); } } } echo $j; echo " "; echo $l; ?> Quote Link to comment https://forums.phpfreaks.com/topic/202335-loop-problem/ Share on other sites More sharing options...
sasa Posted May 20, 2010 Share Posted May 20, 2010 change for ($j=0; $j < pages_to_parse; $j++) to for ($j=1; $j <= pages_to_parse; $j++) Quote Link to comment https://forums.phpfreaks.com/topic/202335-loop-problem/#findComment-1060966 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.