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>

1 Answer 1

5

You can create a function to parse all that array into a menu like block and use recursion:

for that array:

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";
        }
    }
}

from a mysql resource (based on your table):

$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);

and result:

  • Category 1
    • Category 1. 1
      • Category 1. 1. 1
      • Category 1. 1. 2
      • Category 1. 1. 3
    • Category 1. 2
    • Category 1. 3
  • Category 2
  • Category 3
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you very much. How to write $arrCategories array function that this result?
What do you mean how to write? You just wrote it? How can you pull it out of the database?
yes @Mihai Iorga database to array $arrCategories I'm sorry my english is very bad i think my array output function not functional and it is very long
look at please my functions and your function result http://ideone.com/MguhQ
I cannot add your functions because you are not using recursion from database, this script works fine, and mysql_* function are deprecated, try to change to PDO
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.