Changing a recursive condition in a SQL/PHP website menu

I am modifying a site built in PHP with content drawn from a SQL database. The original developer had the navigation created with the following code. It creates the navigation from the CMS pages – it works fine for top level pages but I am trying to modify the way subpages are presented.

What I want it to do is when a subpage is present, it first opens a DIV containing formatting, then populates the subpagemenu by using the ‘while’ statement below. Finally, when the subpagemenu is finished, it closes the DIV.

I can’t seem to work out which condition ‘opens’ the subpage menu before it goes through the loop of filling out the subpage menu.

Any and all help appreciated – thanks!

$pagesrc = $_SERVER['SCRIPT_NAME'];
$getPID_sql = "SELECT * FROM tblPages WHERE parentID = 0";
$getPID_result = mysql_query($getPID_sql);
if(!$getPID_result){print mysql_error()."<br />";}else{
    while ($row = mysql_fetch_array($getPID_result, MYSQL_ASSOC)) {
  $parentID = $row["pageID"];
  $linkName = $row["pageTitle"];
  $linkID = $row["pageID"];
  print "<A href = '".$pagename."?id=".$linkID."'>".$linkName."</A> | ";
    $subpages_sql = "SELECT * FROM tblPages WHERE parentID = $parentID";
    $subpages_results = mysql_query($subpages_sql);
    if(!$subpages_results){print mysql_error();}else{
        $rowcount = mysql_num_rows($subpages_results);
            if($rowcount > 0){
            while ($row2 = mysql_fetch_array($subpages_results, MYSQL_ASSOC)) {
            $sublinkName = $row2["pageTitle"];
            $sublinkID = $row2["pageID"];
            $sublinkParentID = $row2["parentID"];
            if($sublinkParentID == $pageID || $sublinkParentID == $PID){            
print "<a href='".$pagename."?id=".$sublinkID."'>".$sublinkName."</a>";

Leave a Reply


Hire Me
Follow Me!
Most Popular Articles & Pages
Because your vote is Important
Sorry, there are no polls available at the moment.