views:

42

answers:

2

Hi guys im hoping someone can help,

im working on a site and created this experimental script whitch populates a category menu dynamically based on the database entry

it worked for a day and then suddenly stopped. i changed my includes for requires and it gave me this error message

Fatal error: Maximum execution time of 30 seconds exceeded in /home1/advertbo/public_html/dev_area/origocloud/include/views/blog/dbget.php on line 34

function getBlogMenu(){
$dbhost = 'localhost';
$dbuser = ' ';
$dbpass = ' ';

$con = mysql_connect($dbhost, $dbuser, $dbpass);
if (!$con)
  {
    die('Could not connect: ' . mysql_error());
  }

mysql_select_db("ado_ocblog", $con);

$htmlString = "";

$result = mysql_query(
    "SELECT *
    FROM subCat
    JOIN headCat ON subCat.headid = headCat.id
    ORDER BY headid ASC;");

$array = mysql_fetch_array($result);
mysql_close($con);

$pre = NULL;
$hc = 0;
$sc = 1;
while ($array) {
    if($pre == NULL){
        $pre = $row["headc"];
        $test[0][0]=$row["headc"];
        $test[0][1]=$row["subc"];

    }
    else
    {
        if($pre ==$row["headc"]){
            $sc++;
            $test[$hc][$sc] = $row["subc"];

        }
        else
        {
            $hc++;
            $sc = 1;
            $test[$hc][0]=$row["headc"];
            $test[$hc][$sc]=$row["subc"];
            $pre = $row["headc"];
        }
    }

}

foreach( $test as $arrays=>$cat)
{
        $first = TRUE;
        foreach($cat as $element)
        {
            if($first == TRUE)
            {
                $htmlString.= '<h3><a href="">'.$element.'</a></h3>
                        <div>
                            <ul>
                    ';
                $first = FALSE;
            }
            else
            {
                $htmlString.= '<li><a class="sub_menu" href="#">'.$element.'</a></li>';
            }

        }
        $htmlString.= '</ul> </div>';
}
return $htmlString;


}

im really stuck, the page just keeps timing out the point where i call the function

PLEASE HELP:)

+5  A: 

Try this:

while ($array = mysql_fetch_array($result)) {}

Take a look on PHP docs http://php.net/mysql_fetch_array

If does not work, your SQL Query returns too much values and craches the php execution

=]

André Gadonski
André is right. You are taking the assignment `$array = mysql_fetch_array($result)` out of the while() expression. You can't do that, otherwise $array will never change and you will never get out of the while loop.
Sebastián Grignoli
I think it should be while ($row = mysql_fetch_array($result)) { ... } - just a qick look; $array isn't used. maybe he is mixing some example scripts...?
handfix
i am using the $array in the loop, thanks for the advice guys, PHP isn't my strong point i will definitely check out your suggestions. Should I concentrate on the mysql_fetch_array in this? i have seen some advice on stackoverflow that suggests to use mysql_fetch_assoc in these cases is that worth checking out also?
Arturski
@Arturski: i always use mysql_fetch_object (but it doesn't matter). If error this persist, please tell me, how many results your sql command returns? Maybe the error is here.
André Gadonski
A: 

I think it's time to take a step back and look at what you're doing :) This function should do what you want (even if you fixed the infinite loop problem in the function you gave, I don't think it would act how you want it to.):

function getBlogMenu(){
  $dbhost = 'localhost';
  $dbuser = ' ';
  $dbpass = ' ';

  $con = mysql_connect($dbhost, $dbuser, $dbpass);
  if (!$con)
  {
    die('Could not connect: ' . mysql_error());
  }

  mysql_select_db("ado_ocblog", $con);

  $htmlString = "";
  $result = mysql_query(
      "SELECT *
      FROM subCat
      JOIN headCat ON subCat.headid = headCat.id
      ORDER BY headid ASC;");

  // arrays can have strings as keys as well as numbers,
  // and setting $some_array[] = 'value'; (note the empty brackets []) 
  // automatically appends 'value' to the end of $some_array,
  // so you don't have to keep track of or increment indexes
  while ($row = mysql_fetch_assoc($result))
  {
    $test[$row["headc"]][] = $row["subc"];
  }

  // don't close the connection until after we're done reading the rows
  mysql_close($con);

  // $test looks like: array('headc1' => array('subc1', 'subc2', 'sub3'), 'headc2' => array('subc4', 'subc5'), ...)
  // so we step through each headc, and within that loop, step through each headc's array of subc's.
  foreach($test as $headc => $subc_array)
  {
    $htmlString.= '<h3><a href="">'.$headc.'</a></h3><div><ul>';
    foreach($subc_array as $subc)
    {
      $htmlString.= '<li><a class="sub_menu" href="#">'.$subc.'</a></li>';
    }
    $htmlString.= '</ul></div>';
  }

  return $htmlString;
}
catgofire
i nitailly this was split in 2 php files i joined them together in the efforts of troubleshooting this problem to to have it all in one piece. the functionality did work in the end but as suggested earlier it started crashing out :)
Arturski
Right but what I'm saying is: There are some not-so-great programming practices in the example you gave. I was trying to help you out not just for this problem, but for the future :)
catgofire