创建动态PHP / HTML类别树

时间:2015-05-10 14:57:04

标签: php jquery html css

我已经弄清楚如何使用ul>获取每个父/子。 li结构,但现在我被卡住了,子类别需要一个班级'下拉'但无法解决这个问题。

我的目标是使用Foundation导航标记创建一个无限的动态ul菜单。

PHP

function fetchCategoryTreeList($parent = 0, $user_tree_array = '') {

$isParent = '';

if (!is_array($user_tree_array))
$user_tree_array = array();

$sql = "SELECT id,name,parent FROM `categories` WHERE 1 AND parent=$parent ORDER BY id ASC" or die(mysql_error);

if (database_querySelect($sql,$rows)) {

    $user_tree_array[] = "<ul>";

        foreach ($rows as $row) {

            if ($row["parent"] == "0") { $isParent = "has-dropdown"; } 

            $user_tree_array[] = "<li class='".$isParent."'>". $row["name"]. "</li>";

            $user_tree_array = fetchCategoryTreeList($row["id"], $user_tree_array);

        }

    $user_tree_array[] = "</ul>";   

}

  return $user_tree_array;

显示猫的代码

 <?php
  $res = fetchCategoryTreeList();
    foreach ($res as $r) {
      echo $r;
    }
  ?>

输出

<ul>
  <li class='has-dropdown'>Golf Equipment</li>

    <ul>
        <li class=''>Manual Golf Trolleys</li>
        <li class=''>Electric Golf Trolleys</li>
    </ul>

  <li class='has-dropdown'>Weight Training</li>

    <ul>
        <li class=''>Weight Benches</li>
        <li class=''>Weights</li>
    </ul>
  </li>
</ul>

2 个答案:

答案 0 :(得分:0)

我的帮助..我不知道(需要一个班级'下拉')什么...你可以给主要的一个类

$('#sel_kat').change(function() {
    $('.sel_div').hide(); // hide all
    $("#" + this.value).show(); // show selected
});

然后你可以在css和jquery中使用selector

$user_tree_array[] = "<ul  class='main_ul'>";

答案 1 :(得分:0)

功能:

<?php

    function fetchCategoryTreeList( $parent = 0, $user_tree_array = "" ) {

        if ( ! is_array($user_tree_array))
            $user_tree_array = array();


        $sql  = "SELECT id,name,parent ";
        $sql .= "FROM `categories` ";
        $sql .= "WHERE 1 AND parent = {$parent} ";
        $sql .= "ORDER BY id ASC";

        if ( database_querySelect( $sql, $rows) )
        {

            foreach ( $rows as $row ) 
            {

                $user_tree_array[] = "<li class='' >{$row["name"]}</li>";

                if ( $parent != $row["id"] )
                {

                    $user_tree_array[] = "<ul>";

                    $user_tree_array = fetchCategoryTreeList( $connection, $row["id"], $user_tree_array );

                    $user_tree_array[] = "</ul>";

                }
                else
                { 

                    $user_tree_array = fetchCategoryTreeList( $row["id"], $user_tree_array );               

                }

            }

        }

        return $user_tree_array;

    }

?>

致电:

<?php

    $arrays = fetchCategoryTreeList();

    foreach ( $arrays as $list) 
    {
        echo $list;
    }

?>