dongqu4443 2012-09-14 21:29
浏览 47
已采纳

使用php和MySQLi的递归菜单

I have the following code:

function menu($parent, $level){
global $dbc;

$result = $dbc->prepare('SELECT linktext, visible, sort FROM content WHERE parent =? ORDER BY sort');
$result->bind_param('s', $parent);
$result->execute();
$result->bind_result($menu_linktext, $menu_visible, $menu_sort);  
$total_records = $result->num_rows;

if($level > 0 && $total_records > 0){
echo '<ul>';
}
while($row = $result->fetch()){
echo "<li>";
echo '<a href="?page=' . $menu_linktext . '">' . $menu_linktext . '</a>'.$id;
//display this level's children
menu($id, $level+1);
echo "</li>
";
}
if($level > 0 &&  $total_records > 0){
echo '</ul>';
 }
}
echo '<ul>' . menu(0,0) . '</ul>'

It works for one link (Home) then throws out a Call to a member function bind_param() on a non-object error.

The basics of the table is:

page | linktext | visable | parent | sort
  1       Home       1         0      1
  2      Gallery     1         0      3
  3     About Us     1         0      2
  4    Contact Us    1         0      5
  5     Services     1         0      4
  6     Diving       0         5      1
  7     Angling      0         5      2
  8     Charters     0         5      3

Here is the HTML structure:

 <ul class="sf-menu" id="nav">
          <li><a href="index.html">Home</a></li>
          <li><a href="about.html">Examples</a></li>
          <li><a href="gallery.html">A Page</a></li>
          <li><a href="#">Services</a>
            <ul>
              <li><a href="#">Diving</a></li>
              <li><a href="#">Angling</a>
              <li><a href="#">Charter</a></li>
            </ul>
          </li>
          <li><a href="contact.html">Contact Us</a></li>
        </ul>

I want to get the basic menu working then work on the sort order of the links.If anyone can help it would be very much appreciated.

  • 写回答

1条回答 默认 最新

  • dongyi8383 2012-09-27 00:53
    关注

    Problem solved! I will post how it was done and someone may be able to use it.

    function menu()
      {
        global $dbc;
    
      $result = $dbc->prepare('SELECT page, linktext, visable, parent FROM content WHERE visable > 0 ORDER BY parent,sort ASC');
      $result->execute();
      $result->bind_result($menu_page, $menu_linktext, $menu_visible, $menu_parent);
    
      while($result->fetch())
        {
            if($menu_parent == 0) $menu[$menu_page]=$menu_linktext;
            elseif(!empty($menu[$menu_parent])) $sub[$menu_parent][]=$menu_linktext;
        }
    
      $result->close();
    
      if(!empty($menu))
        {
            echo '<ul class="sf-menu" id="nav">';
            foreach($menu as $page=>$link)
              {
                  echo "<li><a href='$link'>$link</a>";
                  if(!empty($sub[$page]))
                    {
                        echo '<ul>';
                        foreach($sub[$page] as $lnk) echo "<li><a href='$lnk'>$lnk</a></li>";
                        echo '</ul>';
                    }
                  echo '</li>';
              }
            echo '</ul>';
          }
    

    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB代码补全插值
  • ¥15 Typegoose 中如何使用 arrayFilters 筛选并更新深度嵌套的子文档数组信息
  • ¥15 前后端分离的学习疑问?
  • ¥15 stata实证代码答疑
  • ¥50 husky+jaco2实现在gazebo与rviz中联合仿真
  • ¥15 dpabi预处理报错:Error using y_ExtractROISignal (line 251)
  • ¥15 在虚拟机中配置flume,无法将slave1节点的文件采集到master节点中
  • ¥15 husky+kinova jaco2 仿真
  • ¥15 zigbee终端设备入网失败
  • ¥15 金融监管系统怎么对7+4机构进行监管的