简体   繁体   English

如何制作子菜单

[英]How can I make a submenu

I have the next database: 我有下一个数据库:

在此处输入图片说明

I have the below function: 我有以下功能:

<?php 

$connection = mysql_connect("localhost", "root", "password") or die(mysql_error());
mysql_select_db("database1", $connection) or die(mysql_error());

function loop_array($array = array(), $parent_id = 0)

{
  if (!empty($array[$parent_id])) {
      echo '<ul>';
      foreach ($array[$parent_id] as $items) {
          echo '<li>';
          echo '<a href="?page='.$items['id'].'">'.$items['title'].'</a>';
          loop_array($array, $items['id']);
          echo '</li>';
      }
  }
}

function displays_menus_revised()

{
  $sql = "SELECT * FROM pages";
  $query = mysql_query($sql) or die(mysql_error());
  $array = array();
  if (mysql_num_rows($query)) {
    while ($rows = mysql_fetch_array($query)) {
      $array[$rows['parent_id']][] = $rows;
    }
    loop_array($array);
  }
}

 ?>

My problem is the funtion show the records like in the left picture. 我的问题是功能类似于左图所示显示记录。 I want the function to show the records like in the right picture. 我希望该功能显示记录如右图所示。 Can you tell me where is the problem? 你能告诉我问题出在哪里吗? What i must change that the function show records like in the right picture? 功能如右图所示记录我必须更改什么? Thanks. 谢谢。

在此处输入图片说明

So nice code, with such a small slip. 这么小的代码,代码真好。 Only the closing 'ul' is missing. 仅缺少结尾的“ ul”。

function loop_array($array = array(), $parent_id = 0)
{
  if (!empty($array[$parent_id])) {
      echo '<ul>';
      foreach ($array[$parent_id] as $items) {
          echo '<li>';
          echo '<a href="?page='.$items['id'].'">'.$items['title'].'</a>';
          loop_array($array, $items['id']);
          echo '</li>';
      }
      echo '</ul>';
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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