Hello. There are two tables

Categories and subcategories.

How to display categories with the right subcategories. Relationship between tables by id categories.

I want to understand the algoritm, how do people implement such a menu through the base?

  • 2
    And why do you need 2 tables for this? A single table with the fields id, parent_id, name will suffice. You get these values ​​and write them into a multidimensional array, then you work with this array. - andreyqin
  • and what will happen in parent_id - duddeniska
  • A reference to the id of the parent category in this table. - andreyqin
  • Well, that means we need a second table - duddeniska
  • 3
    I wrote "in this table." Example: id | parent_id | name 1 | null | News 2 | 1 | Politics 3 | 1 | Economy - andreyqin

1 answer 1

And so! Hmm cool! And how to work with an array? I would just have an algorithm and not a code!

Well, look, if you use the structure suggested by @andreyqin , then you can take all the fields from the database and sort by parent_id. Thus, you will get these data

id | parent_id | name 1 | 1 | Новости 2 | 1 | ΠŸΠΎΠ»ΠΈΡ‚ΠΈΠΊΠ° 3 | 2 | Π­ΠΊΠΎΠ½ΠΎΠΌΠΈΠΊΠ° 

And you can run through this array and generate views for them.

Alternatively, it is possible to group the result from the database according to the necessary criteria and to get an output data associative array.