Help - Search - Members - Calendar
Full Version: can someone help me clean up this code?
Invision Power Services > Community Forums > Community Web Design and Coding
Trel
I'm just starting with using php to interface with mysql and I thought a good idea might be to make a simple threaded message board to teach myself how to do this.

Now, I got this to work, but I'm sure I'm being redundant in it, but I don't know where. Could someone take a look at this code and point out any obvious redundancies?

CODE
<?
require_once './config.php';
require_once './classes/class_mysql_nest.php';
$mysql = new mysql;

function dash($num)
{
    $i = 0;
    while ($i < $num)
    {
 echo "-";
 $i++;
    }
}

echo "Starting General Nested Test<br>\n";
$connection = $mysql->db_connect($host,$user,$pass);
$mysql->db_select($db,$connection);

$query = 'SELECT * FROM `samp_nested` WHERE `parent` = 0';
$parents = $mysql->db_query($query);

while ($parent = mysql_fetch_assoc($parents))
    {
 $nestnum = 0;  
 printf ("<b>%s</b>",$parent["name"]);
 print "<br>";
 
 $query = 'SELECT * FROM `samp_nested` WHERE `parent` = '.$parent["tid"];
 $children = $mysql->db_query($query);
 if (mysql_num_rows($children) != 0)
 {
     $nestnum++;
     while ($child = mysql_fetch_assoc($children))
     {
   $curchi = $child;
   dash($nestnum);
   printf ("<b>%s</b>",$child["name"]);
   print "<br>";
   
   while (mysql_num_rows($children) != 0)
   {
       $nestnum++;
       $query = 'SELECT * FROM `samp_nested` WHERE `parent` = '.$curchi["tid"];
       $children = $mysql->db_query($query);
       while ($child = mysql_fetch_assoc($children))
     {
         $curchi = $child;
         dash($nestnum);
         printf ("<b>%s</b>",$child["name"]);
         print "<br>";
     }
     
   }
     }
 }
 print "<br>";
    }



$mysql->db_disconnect($connection);

?>


I'm attaching the output from the script
Click to view attachment
Rikki
I don't have time to rewrite it for you, but you should very rarely, if ever, run a query inside a loop, and especially not a loop within a loop like you have. It works, but what would happen if you had 500,000 topics to display, each with 10 children? That's upwards of 5 million queries to run. It may not happen in this case but it's good practice to prepare for it and get into the habit original.gif

A better way is to select all of the rows in the nested table with a single query, then use PHP to sort them into an array where the key is the parent ID. You can then loop through the array to print out the children.
Trel
won't that fail if two entrees have the same parent because of a duplicate key?
Rikki
Not if you add them like this:

CODE
$array[ $parent_id ][] = $row_info;


You then loop through each row in $array, then for each parent loop through $array[ $parent_id ] to get the children.
Trel
hmmm, I'm confused, I'm not familliar with that syntax, is it explained on php.net anywhere?
Archbob
That is a horrible way to do things. Never, ever run queries within a loop. Look into table join and the mysql_data_seek() function instead.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.