dongyilai4214
dongyilai4214
2015-02-16 20:30

如何使用php制作树状菜单

已采纳

I am trying to make a menu tree. My table in my database looks like this:

| id        | int(10) unsigned    | NO   | PRI | NULL    |       |
| title     | varchar(255)        | NO   |     | NULL    |       |
| parentId  | int(10) unsigned    | YES  |     | NULL    |       |
| level     | tinyint(3) unsigned | NO   |     | NULL    |       |
| sortOrder | int(10) unsigned    | YES  |     | NULL    |       |

The connection to my database is ok. I have made a loop like this:

$test = connexion_DB($db);
$strSQL = "SELECT id, parentId, title, level FROM my_table ORDER BY title ASC";
$sql = requete_SQL($strSQL);
$categories = array();

while($row = mysql_fetch_array($sql)) {
    $categories[] = array(
      'parentId' => $row['parentId'],
      'level' => $row['level'],
      'title' => $row['title']
    );
} 

…but I don't know how I can do a tree menu ? Like this:

<ul>
<li>Canins
    <ul>
    <li>Chiens</li>
    <li>Loups</li>
    </ul>
</li>
<li>Félins
    <ul>
    <li>Grands Félins
        <ul>
        <li>Lions</li>
        <li>Panthères</li>
        <li>Tigres</li>
        </ul>
    </li>
    <li>Petits Félins</li>
    </ul>
</li>
<li>Poissons
    <ul>
    <li>Requins</li>
    <li>Saumons</li>
    </ul>
</li>
</ul>

Anybody have an idea?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dsomm80482 dsomm80482 6年前

    A quick search of google found this solution here. You will need to do a recursive look through your array to get to your final solution.

    点赞 评论 复制链接分享

为你推荐