mysql - Trouble retrieving products under a complex category structure in PHP -
I hope you guys can help me with this one. Therefore, I have a product table and a category table. The structure for the table of categories is listed bellow. With a series of three or four levels, there would be about 20 categories, a recurring event was written to get the class tree and it is working fine.
My new job is to show products for a given category. As an example, I will show the data in the beta. Therefore, if I add a product with category_id = 7, then I have to show that product when the user selects Category 7, but when the user also chooses Category 4 or Category 1. In addition, if a user selects the category with ID = 1, the products for Category = 1, 4, 5, 6 are to be displayed.
My categories table this looks as follows:
Create tab 'categories' ( `Sreni_aidi`) (5) unsigned NOT NULL Autisiaartiment,` Kategri_nam` considered (256) character set Utif 8 Collet Utif 8_inikod_si not, `Sreni_slog` considered (256) character set UTF-8 Collet Utif 8_inikod_si not,` Sreni_parnt` small (5) unsigned nOT nULL default '0', `category_description_ro` text reader sET utf8 COLLATE utf8_unicode_ci tap , `Range_description_en Si` text reader option is an example of data in utf8 COLLATE utf8_unicode_ci tap, PRIMARY KEY ( `Sreni_aidi`)) engine = Maisam AUTO_INCREMENT = 8 DEFAULT CHARSET = latin1
Bellow table:
category id | Category name | Category_parent 1 categoria 1 0 2 categoria 2 0 3 categoria 3 0 4 categoria 1.1 1 5 categoria 1.2 1 6 1.3 Category 1 Category 7 1.1.2 4 "Post-text" Aitmprop = "text"> < P> In similar cases, I have created a recursive function which will allow me to get all the children in a category, and also the category itself. SQL
then you can simply say
in WHERE category (cat1, cat2, .. update.) If you find a function that you want children, which adds to the yield, 1 results [1,4,5,6,7]
Then your query
WHERE category (1,4,5,6,7)
is a short way to write
WHERE category = 1 or category = 4 or category = 5 ...
So this is actually a statement that will be able to get the products, though The same category ID is attached
Comments
Post a Comment