Hello. I can’t figure out how to create a list with a nested list. In fact, there is a table in the database with categories of goods and subcategories. It is necessary to display the list so that the subcategories are a nested list of the category to which they belong. Guys help please. The second day I sit with this menu. I just learn not to throw stones) It should turn out like this:
I did it like this:
<?php $query = "SELECT c.id AS categories_id, c.category AS categories_category, sc.id AS subcategories_id, sc.subcategory AS subcategories_subcategory, sc.category AS subcategories_category FROM subcategories sc RIGHT JOIN categories c ON c.id = sc.category"; $result = mysqli_query($link, $query) or die (mysqli_error($link)); for ($data = []; $row = mysqli_fetch_assoc($result); $data[] = $row); var_dump($data); $content = ''; foreach ($data as $elem) { $content .= "<li class=\"sub\"> <a href=\"\">$elem[categories_category]</a> </li>"; if ($elem['categories_id'] == $elem['subcategories_category']) { $content .= "<ul> <li class=\"down_item\"> <a href=\"\">$elem[subcategories_subcategory]</a> </li> </ul>"; } } ?>
parent_id
, if it isNULL
then this is a category. Accordingly, when you select all the records from the database, you need to reassemble the resulting array so that the subcategories are nested into categories (the SO tree menu was such a question). After this, there will be no problems with the output. - ArchDemon