简体   繁体   中英

Need help in creating dynamic menu

I am developing a website using PHP and this is the 1st time I am trying a dynamic menu.

What I am doing is I have created a table for pages.

The table structure is as follows:

____________________________________________________________________________
|page_id|title|url|content|menu_title|show_on_navuigation|is_sub|parent_page|
|_______|_____|___|_______|__________|___________________|______|___________|

Here is_sub indicates whether it is a dropdown menu of some main menu. and parent_page is the main menu of the sub menu.

Now, what I want is, like

I have created 2 parent pages:

About Us and Test

and 2 sub menu's: Test aboutus and testsub,

Test aboutus is sub of About Us and testsub is sub of test.

How actually should the query and the loop, so that the menu renders perfectly.

Thanks in advance.

This is my menu structure:

<ul>
    <li class='has-sub'><a href='#'><span>About Us</span></a>
      <ul>
         <li><a href='corporateprofile'><span>Corporate Profile</span></a></li>
         <li class='last'><a href='visionandmission'><span>Vision &amp; Mission</span></a></li>
      </ul>
   </li>
   <li class='has-sub'><a href='#'><span>Business Services</span></a>
      <ul>
         <li><a href='recruitment'><span>Recruitment</span></a></li>
         <li><a href='training'><span>Training</span></a></li>
         <li><a href='executivesearch'><span>Executive Search</span></a></li>
         <li><a href='payroll'><span>Payroll</span></a></li>
         <li class='last'><a href='backgroundverification'><span>Background Verification</span></a></li>
      </ul>
   </li>
   <li class='has-sub'><a href='#'><span>Employers</span></a>
      <ul>
         <li><a href='enquiry'><span>Enquiry</span></a></li>
         <li><a href='jobdescription'><span>Job Description</span></a></li>
         <li><a href='employercontract'><span>Employer Contract</span></a></li>
         <li class='last'><a href='feedback'><span>Feedback</span></a></li>
      </ul>
   </li>
   <li class='has-sub'><a href='javascript:;'><span>Job Seeker</span></a>
      <ul>
         <li><a href='applyforjob'><span>Apply For Job/Register</span></a></li>
         <li><a href='careertips'><span>Career Tips</span></a></li>
         <li><a href='interview Questions'><span>Interview Questions</span></a></li>
         <li><a href='interviewprocess'><span>Interview Process</span></a></li>
         <li class='last'><a href='corporatedress'><span>Corporate Dress</span></a></li>
      </ul>
   </li>
   <li class='has-sub'><a href='#'><span>Franchise</span></a>
      <ul>
         <li class='last'><a href='#'><span>Franchise Enquiry Form</span></a></li>
      </ul>
   </li>
   <li class='last'><a href='#'><span>Contact us</span></a></li> 

</ul>

<?php
function displayMenu($parent_page_id) {
      $sql = "SELECT * FROM `pages` WHERE `parent_page` = '$parent_page_id'"; // sql
      $result = mysql_query($sql);
      if( mysql_num_rows($result) === 0 ) { // mysql_num_rows() is deprecated, but you are using mysql extension so that's why I show it here
         return true; // exit function
      } // else, continue to rest of function
      echo '<ul>';
      while($row = mysql_fetch_assoc($result)) { // deprecated mysql php function here for simplicity
         echo '<li><a href="' . $result['url'] . '">' . $result['menu_title'] . '</a>'; // no closing </li> yet
         displayMenu($row['page_id']); // this is the recursive part
         echo '</li>'; // close the <li> from before the recursion
      }
      echo '</ul>';
}
$get_base_menu = mysql_query("SELECT * FROM pages WHERE parent_page = 0");
while($fetch_base_menu = mysql_fetch_array($get_base_menu))
{
    $parent_page_id = $fetch_base_menu['page_id'];
    displayMenu($parent_page_id);

}

?>

I highly suggest trying to get away from using an Adjacency List model and move toward a much easier to manage solution, such as a nested set. Using an MPTT type solution should help you manage your hierarchical data much easier. Using an Adjacency List model you are limited at a certain point.

I'd suggest looking into using something along the lines of Zebra_MPTT , or some other form of MPTT library. Please checkout this article on Managing Hierarchical data in MySQL .

I think the simplest thing for you to code will be something like this recursive function. All you have to do is put it on your page and make sure you have parent_page_id = 0 for the base level menu items. (I'm using parent_page_id instead of parent_page because I assume that is what you mean and it is more clear for the answer.)

Calling the function with an argument of "0" should give you the full menu tree.

$trace = array();

function displayMenu($parent_page_id) {
  $sql = "SELECT * FROM `tbl_menu` WHERE `parent_page_id` = $parent_page_id"; // sql
  $result = mysql_query($sql);
  if( mysql_num_rows($result) === 0 ) { // mysql_num_rows() is deprecated, but you are using mysql extension so that's why I show it here
     $trace[] = "Page_id $parent_page_id has no children";
     return true; // exit function
  } // else, continue to rest of function
  echo '<ul>';
  while($row = mysql_fetch_assoc($result)) { // deprecated mysql php function here for simplicity
     $trace[] = "Entered while loop for page_id = $parent_page_id"; // not an error, just tracking
     echo '<li><a href="' . $row['url'] . '">' . $row['menu_title'] . '</a>'; // no closing </li> yet
     displayMenu($row['page_id']); // this is the recursive part
     echo '</li>'; // close the <li> from before the recursion
  }
  echo '</ul>';
}

displayMenu(0); // call function for base level

var_dump($trace);

More info:

I have researched the topic before and had a nice link to a page on mysql.com with an in-depth exploration of hierarchical relational database structures. But when I checked, the link was broken!

But, I think I found it on another site, here:

http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM