php - Simplifying looping queries down to one query -
I have a list of categories of different levels. I have columns, category_id , category_title , category_level and category_parent_id the following code is a part of my search script For the category given in the search, I want to get results not only on the results related to the selected categories, but in this category all child categories (and those children) D). I have surrounded it by looking into the children of the class and then children's children. Is it possible to do a query to simplify it down? I want to get a string of all hair categories individually by a comma, so that I can use it in the The output should be something like I use an active record query I am doing btw (in codeigner), understanding syntax, confusing you. I will be placed in the table at a maximum of 50 categories and at a maximum of 3 levels. If speed is not an issue, nested loop should work fine for you. But if you want to avoid recursion or nested loops to get categories and subcategories, you can create a new column, which maps the genealogy of the category. Example: For class 3 the value of descent will be For category 2 and all its subcategories If you want to query, then your code should look like this: with a similarity, it may be a pain to include a new category because you need a second code to generate genealogy. is. I do not recommend this solution if the user can reassign the parent of a class because you need to rebuild the offspring for all affected children. WHERE IN condition.
$ categories = $ this- & gt; Input-> Yes ('category_id'); $ Subquery = $ this- & gt; Db- & gt; Where ('parent_id', $ ranges) - & gt; Obtain ('Categories'); $ Subcats = $ subquery- & gt; result (); Forex Currency ($ subkits $ cat) {$ categories} = ','. $ Cat- & gt; Category_ID; $ Subsubquery = $ this- & gt; Db- & gt; Where ('parent_id', $ cat- & gt; Category_ID) - & gt; Obtain ('Categories'); Forex Currency ($ subsubquery- & gt; Results () as the $ cat) {$ categories. = ','. $ Cat- & gt; Category_ID; }
2, 15, 45, 15, 41, 32
/ 1/2/4/5 /
/ 1/2/3 /
$ this-> Db- & gt; Like ('descent', '/ 2 /'); // 'WHERE Dynasty' %% / 2 /% '
Comments
Post a Comment