1

In MySQL my table appears like this:

In MySQL my table appears like this:

http://sqlfiddle.com/#!2/76717.

CREATE TABLE IF NOT EXISTS `tbl_category` (
  `id` int(6) NOT NULL auto_increment,
  `parent_id` int(4) NOT NULL default '0',
  `name` varchar(255) NOT NULL,
  PRIMARY KEY  (`id`),
  KEY `parent_id` (`parent_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8;


INSERT INTO `tbl_category` (`id`, `parent_id`, `name`) VALUES
(1, 0, 'Category 1'),
(2, 0, 'Category 2'),
(3, 0, 'Category 3'),
(4, 1, 'Category 1. 1'),
(5, 1, 'Category 1. 2'),
(6, 1, 'Category 1. 3'),
(7, 4, 'Category 1. 1. 1'),
(8, 4, 'Category 1. 1. 2'),
(9, 4, 'Category 1. 1. 3');

My array

My array

$arrCategories = array(
    array("id" => 1, "parent_id" => 0, "name" => "Category 1", "children" => array()
                array("id" => 4, "parent_id" => 1, "name" => "Category 1. 1", "children" => array()
                         array("id" => 7, "parent_id" => 4, "name" => "Category 1. 1. 1", "children" => array()),
                         array("id" => 8, "parent_id" => 4, "name" => "Category 1. 1. 2", "children" => array()),
                         array("id" => 9, "parent_id" => 4, "name" => "Category 1. 1. 3", "children" => array())
                                ),
                array("id" => 5, "parent_id" => 1, "name" => "Category 1. 2", "children" => array()),
                array("id" => 6, "parent_id" => 1, "name" => "Category 1. 3", "children" => array())        
        ),
    array("id" => 2, "parent_id" => 0, "name" => "Category 2", "children" => array()),
    array("id" => 3, "parent_id" => 0, "name" => "Category 3", "children" => array())
);

The array output

The array output

Array
(
    [id] => 1
    [parent_id] => 0
    [name] => Category 1
    [children] => Array
        (
            [4] => Array
                (
                    [id] => 4
                    [parent_id] => 1
                    [name] => Category 1. 1
                    [children] => Array
                        (
                            [7] => Array
                                (
                                    [id] => 7
                                    [parent_id] => 4
                                    [name] => Category 1. 1. 1
                                    [children] => Array
                                        (
                                        )

                                )

                            [8] => Array
                                (
                                    [id] => 8
                                    [parent_id] => 4
                                    [name] => Category 1. 1. 2
                                    [children] => Array
                                        (
                                        )

                                )

                            [9] => Array
                                (
                                    [id] => 9
                                    [parent_id] => 4
                                    [name] => Category 1. 1. 3
                                    [children] => Array
                                        (
                                        )

                                )

                        )

                )

            [5] => Array
                (
                    [id] => 5
                    [parent_id] => 1
                    [name] => Category 1. 2
                    [children] => Array
                        (
                        )

                )

            [6] => Array
                (
                    [id] => 6
                    [parent_id] => 1
                    [name] => Category 1. 3
                    [children] => Array
                        (
                        )

                )

        )

)
Array
(
    [id] => 2
    [parent_id] => 0
    [name] => Category 2
    [children] => Array
        (
        )

)
Array
(
    [id] => 3
    [parent_id] => 0
    [name] => Category 3
    [children] => Array
        (
        )

)

How do I make it width PHP ?

How do I make it width PHP ?

<ul>
 <li>Category 1
  <ul>
   <li>Category 1. 1
    <ul>
     <li>Category 1. 1. 1</li>
     <li>Category 1. 1. 2</li>
     <li>Category 1. 1. 3</li>
    </ul>
   </li>
   <li>Category 1. 2</li>
   <li>Category 1. 3</li>
  </ul>
 </li>
 <li>Category 2</li>
 <li>Category 3</li>
</ul>

This is does not work correctly

This is does not work correctly

<?php
echo "    <ul>\n";
foreach ($categories as $category) {
echo "     <li>".$category['ad'];

if ($category['children']) {
echo "      <ul>\n";
foreach ($category['children'] as $child) {
echo "       <li>".$child['ad']."</li>\n";
}
echo "      </ul>\n";
}
echo "     </li>\n";
}
echo "    </ul>\n";
?>

Thanks works now for me thats functions... http://ideone.com/u2GNV

But i think not valid ul and li tags...

http://ideone.com/u2GNV results for me

<ul>
 <li>Category 1
  <ul>
   <li>Category 1. 1
    <ul>
     <li>Category 1. 1. 1</li>
    </ul>
    <ul>
     <li>Category 1. 1. 2</li>
    </ul>
    <ul>
     <li>Category 1. 1. 3</li>
    </ul>
   </li>
  </ul>

  <ul>
   <li>Category 1. 2</li>
  </ul>

  <ul>
   <li>Category  1. 3</li>
  </ul>
 </li>
</ul>

<ul>
 <li>Category 2</li>
</ul>

<ul>
 <li>Category 3</li>
</ul>

i think valid w3 code

<ul>
 <li>Category 1
  <ul>
   <li>Category 1. 1
    <ul>
     <li>Category 1. 1. 1</li>
     <li>Category 1. 1. 2</li>
     <li>Category 1. 1. 3</li>
    </ul>
   </li>
   <li>Category 1. 2</li>
   <li>Category 1. 3</li>
  </ul>
 </li>
 <li>Category 2</li>
 <li>Category 3</li>
</ul>
4

1 回答 1

5

您可以创建一个函数来将所有数组解析为一个menu like块并使用递归:

对于该数组:

function printMenu($array){
    foreach($array as $item){
        if(is_array($item) && isset($item['name'])){
            echo "<ul>\n";

                if(is_array($item['children'])){
                    echo "<li>".$item['name'];
                    printMenu($item);
                    echo "</li>\n";
                } else {
                    echo "<li>".$item['name']."</li>\n";
                }

            echo "</ul>\n\n";
        }
    }
}

来自 mysql 资源(基于您的表):

$db = new PDO('mysql:host=localhost;dbname=test', 'root', '', array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));


function printMenu($array){
    foreach($array as $item){
        if(is_array($item) && isset($item['name'])){
            echo "<ul>\n";
                if(getChildren($item['id'], false)){
                    echo "<li>".$item['name'];
                    printMenu(getChildren($item['id'], true));
                    echo "</li>\n";
                } else {
                    echo "<li>".$item['name']."</li>\n";
                }

            echo "</ul>\n\n";
        }
    }
}

function getChildren($id, $return = false){
    global $db;
    $stmt = $db->prepare("SELECT * FROM `tbl_category` WHERE `parent_id` = :parent_id");
    $stmt->execute(array(':parent_id' => $id));
    $num = $stmt->rowCount();
    if($num){
        if($return){
            $row = $stmt->fetchAll(PDO::FETCH_ASSOC);
            return $row;
        }
        return true;
    }
    return false;
}


$query = "SELECT * FROM `tbl_category` WHERE `parent_id` = 0";
$stmt = $db->query($query);
$row = $stmt->fetchAll(PDO::FETCH_ASSOC);
printMenu($row);

结果:

  • 第一类
    • 类别 1. 1
      • 类别 1. 1. 1
      • 类别 1. 1. 2
      • 类别 1. 1. 3
    • 类别 1. 2
    • 类别 1. 3
  • 第 2 类
  • 第 3 类
于 2012-09-15T22:28:22.023 回答