Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Looping Problems

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Looping Problems

Postby kheadley » Thu Mar 28, 2013 4:45 pm

Hey All,

I am having problems with a foreach loop within a while loop, the while loop only runs once through even there are more than 10 rows in the DB that match the criteria. Can anyone see where I am going wrong?

Code: Select all
$query = mysql_query("SELECT pages.id, pages.page, pages.site_id, site.url FROM pages
LEFT JOIN site ON site.id = pages.site_id
WHERE pages.processed = '0' LIMIT 10");
while ($row = mysql_fetch_array($query))
{
   $siteid = $row['site_id'];
   $pid = $row['id'];
   ///
   $baseurl = $row['url'];
   ///
   if(file_get_html($row['page']))
   {
      $html = file_get_html($row['page']);
      foreach($html->find('a') as $element)
      {
         
         if(stristr($element->href,$row['page']))
         {
            array_push($pages,$element->href);
   
         }
         elseif(substr($element->href, 0, 1) == "/")
         {
            array_push($pages,$baseurl.$element->href);
         }
         elseif(substr($element->href, 0, 7) != "http://" && substr($element->href, 0, 8) != "https://" && substr($element->href, 0, 11) != "javascript:" && $element->href != "" && $element->href != " " && substr($element->href, 0, 1) != "#" && substr($element->href, 0, 4) != "tel:" && substr($element->href, 0, 7) != "mailto:")
         {
            array_push($pages,$baseurl."/".$element->href);
         }
      }
   }
   
   foreach($pages as $p)
   {
      $p = str_replace("http://", "",$p);
      $p = str_replace("//", "/",$p);
      $p = "http://".$p;
      $p = explode("#",$p);
      $p = $p[0];
      
      $query = mysql_query("SELECT id, mentions FROM pages WHERE site_id = '$siteid' AND page = '$p'");
      $count = mysql_num_rows($query);
      if($count > 0)
      {
         while ($row = mysql_fetch_array($query))
         {
            $mentions = $row['mentions'] + 1;
            $pageid = $row['id'];
         }
         mysql_query("UPDATE pages SET mentions = '$mentions' WHERE id = '$pageid'", $con);
      }else{
         mysql_query("INSERT INTO pages (site_id, page) VALUES ('$siteid', '$p')", $con);
      }
   }
$pages = "";
mysql_query("UPDATE pages SET processed = '1' WHERE id = '$pid'", $con);
}      


I hope someone can help. Thanks Guys and Gals!
kheadley
New php-forum User
New php-forum User
 
Posts: 14
Joined: Sat Jan 07, 2012 3:03 pm

Re: Looping Problems

Postby seandisanti » Thu Mar 28, 2013 10:23 pm

1) don't use mysql_ functions. mysqli_ are faster and more secure, and PDO is the accepted best practice, and is faster and more secure than both. probably 2 or 3 times a day i post a link to a video that explains pdo usage on jream.com and johnj has posted another link explaining PDO several times.

2) how many pages for your foreach are really being loaded from the single record your while loop is based upon?

3) have you run your query through mysql or phpMyAdmin to verify that its returning the number of records you expect? limiting results to a greater number than are being returned doesn't create extra records.

4) your update query at the bottom is never going to update the way you are expecting it to. unless the id of a record is the literal string '$pid'. Inline variables are not interpreted as such between single quotes. they're treated as literal strings.to quote string values properly, you should be using PDO::quote() or concatenating in the variable names like:
Code: Select all

$sql 
="UPDATE pages SET processed = '1' WHERE id = " mysqli_real_escape_string($pid);
 

5) your insert statement is similarly not inserting the data you think it is, for the same reason. So even if it's looping through every valid record with the while loop, it's not changing them to processed, or inserting the values as you want, so it probably looks to you like it's not doing what it's supposed to.

6) seriously re-read number one, then spend 12 minutes watching http://jream.com/learning/videos/php-oo ... o-examples it can change your life - or at least improve your code and reduce your headaches
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm

Re: Looping Problems

Postby seandisanti » Thu Mar 28, 2013 10:25 pm

and sorry for answering your question last after having you check a bunch of stuff that didn't really matter in this particular situation. i was just writing notes as i read through it and didn't realize what was going on until the end.
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm


Return to PHP coding => General

Who is online

Users browsing this forum: Google [Bot] and 6 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron