Dynamic Drop down Menu Coding Issue

Messages
440
Reaction score
0
Points
16
I am trying to make a drop down menu that is fully dynamic for my website. I have it making drop downs and making a full tree menu. Only problem is i can't put external links in as it wont check if the row['ext'] == 1, i've tried everything i can think of so far. I have given you the full working script with the IF statements but without the check for ['ext'] == 1. I basically need the array to check if ['ext']==1 and if it does to output
Code:
$html .= "<li>\n  <a href='/".$menu['items'][$itemId]['link']."'>".$menu['items'][$itemId]['label']."</a>\n</li> \n";

and if ['ext'] == 0 to output
Code:
$html .= "<li>\n  <a href='pages/".$menu['items'][$itemId]['link']."'>".$menu['items'][$itemId]['label']."</a>\n</li> \n";

Anybody able to shed some light please.

Code:
//---------------------------------------------------------------------------------------------------------------------------------------------------------------
// Build Main Navigation menu and gather page data here -----------------------------------------------------------------------------

$sql = "SELECT id, label, link, parent, page_order, ext FROM content_pages WHERE active='1' ORDER BY page_order ASC ";
$query = mysqli_query($dbc, $sql) or die (mysqli_error($dbc)); 



// Create a multidimensional array to conatin a list of items and parents
$menu = array(
    'items' => array(),
    'parents' => array(),
);
// Builds the array lists with data from the menu table


while ($items = mysqli_fetch_assoc($query))
{

    // Creates entry into items array with current menu item id ie. $menu['items'][1]
    $menu['items'][$items['id']] = $items;
    // Creates entry into parents array. Parents array contains a list of all items with children
    $menu['parents'][$items['parent']][] = $items['id'];
    $
}

// Menu builder function, parentId 0 is the root

function buildMenu($parent, $menu, $external)
{

    $html = "<ul class='menu sf-js-enabled'>\n";
   if  ( isset($menu['parents'][$parent]) )
   {

      $html .= "";

       foreach ($menu['parents'][$parent] as $itemId)
     
       {
    if(!isset($menu['parents'][$itemId]))
    {
        $html .= "<li>\n  <a href='../pages/".$c.$menu['items'][$itemId]['link']."'>".$menu['items'][$itemId]['label']."</a>\n</li> \n";
    }

              else
              {
             $html .= "<li>\n  <a href='/".$menu['items'][$itemId]['link']."'>".$menu['items'][$itemId]['label']."</a>\n</li> \n";
              }

          if(isset($menu['parents'][$itemId]))
          {
             $html .= "
             <li>\n  <a href='../pages/".$menu['items'][$itemId]['link']."'>".$menu['items'][$itemId]['label']."</a> \n";
             $html .= buildMenu($itemId, $menu);
             $html .= "</li> \n";
          }      

}
       $html .= " </ul>\n";
   }
   $html .= "";
return $html;

}
$nav .= "<div class='bg-2'><div class='container_12'><article class='grid_12'><nav>";
$navend .= "</nav></article></div></div></header>";

mysqli_free_result($query);
 

gomarc

Member
Messages
516
Reaction score
18
Points
18
Hello imported_disturbedart,

You may want to check if the array $menu is giving you the expected information:

PHP:
print ( '<pre>' )  ;
print_r($menu);
print ( '</pre>' ) ;

From what I can see, the value of ['ext'] is in {$menu['items'][$key]['ext']}

PHP:
foreach ($menu['items'] as $key => $value){
  echo '<li>$menu[\'items\'][' . $key . '][\'ext\']';
  echo ' value is =' . $menu['items'][$key]['ext'];
  echo '</li>';
}

And you may be able to use $itemId (instead of $key) if all is working ok:

Code:
{$menu['items'][$itemId]['ext']}

A sample sql dump from the content_pages table would be useful to test the code.
 

disturbedart

Member
Messages
474
Reaction score
1
Points
18
Code:
$menu['items'][1]['ext'] value is =0
$menu['items'][2]['ext'] value is =1
$menu['items'][99]['ext'] value is =0
$menu['items'][92]['ext'] value is =0
$menu['items'][96]['ext'] value is =0
$menu['items'][100]['ext'] value is =0
$menu['items'][93]['ext'] value is =0
$menu['items'][89]['ext'] value is =0
$menu['items'][101]['ext'] value is =0
$menu['items'][6]['ext'] value is =1
$menu['items'][1]['ext'] value is =0

Is what is being outputted for the foreach. So its outputting the correct information now i just need that to take that data and change the anchors as needed.
 

disturbedart

Member
Messages
474
Reaction score
1
Points
18
Thank you for that code its fixed it, i can't believe i didnt try that i tried it with parents but wasn't work but itemid worked perfectly.

Working code if anyone wants to reference from it:

Code:
$sql = "SELECT id, label, link, parent, page_order, ext FROM content_pages WHERE active='1' ORDER BY page_order ASC ";
$query = mysqli_query($dbc, $sql) or die (mysqli_error($dbc)); 



// Create a multidimensional array to conatin a list of items and parents
$menu = array(
    'items' => array(),
    'parents' => array(),
);
// Builds the array lists with data from the menu table


while ($items = mysqli_fetch_assoc($query))
{

    // Creates entry into items array with current menu item id ie. $menu['items'][1]
    $menu['items'][$items['id']] = $items;
    // Creates entry into parents array. Parents array contains a list of all items with children
    $menu['parents'][$items['parent']][] = $items['id'];
}

// Menu builder function, parentId 0 is the root

function buildMenu($parent, $menu)
{

    $html = "<ul>\n";
   if  ( isset($menu['parents'][$parent]) )
   {

      $html .= "";

       foreach ($menu['parents'][$parent] as $itemId)
     
       {
    if(!isset($menu['parents'][$itemId]) && $menu['items'][$itemId]['ext'] == 0)
    {
        $html .= "<li>\n  <a href='../pages/".$menu['items'][$itemId]['link']."'>".$menu['items'][$itemId]['label']."</a>\n</li> \n";
    }
    elseif(!isset($menu['parents'][$itemId]) && $menu['items'][$itemId]['ext'] == 1)
    {
        $html .= "<li>\n  <a href='../".$menu['items'][$itemId]['link']."'>".$menu['items'][$itemId]['label']."</a>\n</li> \n";
    }


if(isset($menu['parents'][$itemId]) && $menu['items'][$itemId]['ext'] == 0)
          {
             $html .= "
             <li>\n  <a href='../pages/".$menu['items'][$itemId]['link']."'>".$menu['items'][$itemId]['label']."<span class='arrow-down'></span></a> \n";
             $html .= buildMenu($itemId, $menu);
             $html .= "</li> \n";
          } 
elseif(isset($menu['parents'][$itemId]) && $menu['items'][$itemId]['ext'] == 1)
    {
        $html .= "<li>\n  <a href='../".$menu['items'][$itemId]['link']."'>".$menu['items'][$itemId]['label']."</a>\n</li> \n";
    }


}
       $html .= " </ul>\n";
   }
   $html .= "";
return $html;

}
 

Skizzerz

Contributors
Staff member
Contributors
Messages
2,929
Reaction score
118
Points
63
  1. Your code indentation is horrid. You should feel ashamed of it and fix it.
  2. If the menu contains items that are user input (and if not why are you getting them from the db instead of hardcoding?), then your code is vulnerable to HTML injection (and thus by extension XSS), meaning someone can insert arbitrary HTML/JavaScript into your page which is then viewed/run by everyone else visiting the site. You should properly escape all output with appropriate functions depending on the context of where you are inserting the variable.
 
Top