需要帮助创建动态菜单 [英] Need help in creating dynamic menu

查看:113
本文介绍了需要帮助创建动态菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PHP开发一个网站,这是我第一次尝试动态菜单。



我在做什么是我创建了一个表的页面



表格结构如下:

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

这里 is_sub 表示它是否是一个下拉列表一些主菜单的菜单。而 parent_page 是子菜单的主菜单。



现在,我想要的就像



我已经创建了两个父页面:



关于我们测试



和2子菜单:测试aboutus和testsub,



测试aboutus是关于我们的子项,testsub是测试的子级。



查询和循环实际应该如何,使菜单完美呈现。 / p>

提前感谢



这是我的菜单结构:

 < ul> 
< li class ='has-sub'>< a href ='#'>< span>关于我们< / span>< / a>
< ul>
< li>< a href ='corporateprofile'>< span>公司简介< / span>< / a>< / li>
< li class ='last'>< a href ='visionandmission'>< span> Vision& amp; amp;使命和LT; /跨度>< / A>< /锂>
< / ul>
< / li>
< li class ='has-sub'>< a href ='#'>< span> Business Services< / span>< / a>
< ul>
< li>< a href ='recruitment'>< span>招募< / span>< / a>< / li>
< li>< a href ='training'>< span>培训< / span>< / a>< / li> / b< / a>< / li>
< li>< a href ='payroll'>< span>工资单< / span>< / a>< / li>
< li class ='last'>< a href ='backgroundverification'>< span>后台验证< / span>< / a>< / li>
< / ul>
< / li>
< li class ='has-sub'>< a href ='#'>< span>雇主< / span>< / a>
< ul>
< li>< a href ='inquiry'>< span>查询< / span>< / a>< / li>
< li>< a href ='jobdescription'>< span>作业说明< / span>< / a>< / li> / b< / a>< / li>< /
< li class ='last'>< a href ='feedback'>< span>反馈< / span>< / a>< / li>
< / ul>
< / li>
< li class ='has-sub'>< a href ='javascript:;'>< span>求职者< / span>< / a>
< ul>
< li>< a href ='applyforjob'>< span>应用于作业/注册< / span>< / a>< / li>
< li>< a href ='careertips'>< span>职业提示< / span>< / a>< / li> / b< / a>< / li>
< li>< a href ='interviewprocess'>< span>采访过程< / span>< / a>< / li>
< li class ='last'>< a href ='corporatedress'>< span> Corporate Dress< / span>< / a>< / li>
< / ul>
< / li>
< li class ='has-sub'>< a href ='#'>< span>特许经营< / span>< / a>
< ul>
< li class ='last'>< a href ='#'>< span>特许经营查询表单< / span>< / a>< / li>
< / ul>
< / li>
< li class ='last'>< a href ='#'>< span>与我们联系< / 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()已被弃用,但您使用的是mysql扩展名,这就是为什么我在这里显示
return true; // exit function
} // else,继续使用函数
echo'< ul>';
while($ row = mysql_fetch_assoc($ result)){//为简单起见,弃用mysql php函数
echo'< li>< a href ='$ result ['url'] ''>'。 $ result ['menu_title']。 < / A>; // no closing< / li>但
displayMenu($ row ['page_id']); //这是递归部分
echo'< / li>'; //关闭< li>从递归前
}
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);

}

解决方案

我认为你最简单的代码就是这样的递归函数。所有您需要做的是将其放在您的页面上,并确保您的基本菜单项有 parent_page_id = 0 。 (我使用 parent_page_id 而不是 parent_page ,因为我认为这是你的意思,更清楚的是



调用参数为0的函数应该给你完整的菜单树。

  $ 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()已被弃用,但您使用的是mysql扩展名,这就是为什么我在这里显示
$ trace [] =Page_id $ parent_page_id没有子
返回true; // exit function
} // else,继续使用函数
echo'< ul>';
while($ row = mysql_fetch_assoc($ result)){//为简单起见,弃用mysql php函数
$ trace [] =输入page_id = $ parent_page_id的while循环; // not a error,just tracking
echo'< li>< a href ='。$ row ['url']。'>'。 $ row ['menu_title']。 < / A>; // no closing< / li>但
displayMenu($ row ['page_id']); //这是递归部分
echo'< / li>'; //关闭< li>从递归前
}
echo'< / ul>';
}

displayMenu(0); //基本级别的调用函数

var_dump($ trace);

更多信息:



之前的主题,并有一个很好的链接到mysql.com上的一个页面,深入探索层次关系数据库结构。但是,当我检查,链接被打破了



但是,我想我发现在另一个网站,在这里:



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


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 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/

这篇关于需要帮助创建动态菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆