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 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; }   

The output should be something like 2, 15, 45, 15, 41, 32

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:

  • 1
    • 2
      • 3
      • 4
        • 5
        • 6
          • 7
            • 8 For Category 5, the value of descent
                / 1/2/4/5 /   

              For class 3 the value of descent will be

                / 1/2/3 /   

              For category 2 and all its subcategories If you want to query, then your code should look like this:

                $ this-> Db- & gt; Like ('descent', '/ 2 /'); // 'WHERE Dynasty' %% / 2 /% '  

              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.

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -