farzad
farzad

Reputation: 654

How create menu automaticaly by php <ul> <li> tag with nested set model

I see nested set model method for create hierarchy category in database with MYSQL in this page :

mikehillyer.com/articles/managing-hierarchical-data-in-mysql/

But i didn't understand how can i make nested list for site category menu with these mysql result.

For example if , my table is like this :

+-------------+----------------------+-----+-----+
| category_id | name                 | lft | rgt |
+-------------+----------------------+-----+-----+
|           1 | ELECTRONICS          |   1 |  20 |
|           2 | TELEVISIONS          |   2 |   9 |
|           3 | TUBE                 |   3 |   4 |
|           4 | LCD                  |   5 |   6 |
|           5 | PLASMA               |   7 |   8 |
|           6 | PORTABLE ELECTRONICS |  10 |  19 |
|           7 | MP3 PLAYERS          |  11 |  14 |
|           8 | FLASH                |  12 |  13 |
|           9 | CD PLAYERS           |  15 |  16 |
|          10 | 2 WAY RADIOS         |  17 |  18 |
+-------------+----------------------+-----+-----+

I want make category menu with php and <ul> <li> automaticaly.

Upvotes: 1

Views: 277

Answers (1)

Marcogomesr
Marcogomesr

Reputation: 2672

just get all the categories on mysql with a query like:

"SELECT name from category"

don't know if you are using PDO or any other any driver like mysqli, I assume the table's name is called category. set the content to a variable and loop through it.

<ul>
    <?php  foreach ($categories as $category): ?>
        <li><?=$category ?></li>
    <?php endforeach; ?>;
</ul>

Upvotes: -1

Related Questions