Jump to content

[SOLVED] Pulling out Categories from a db... please help


alco19357

Recommended Posts

Hi, I have a database table with records for categories. The categories can go in depth quite a bit and can be stacked "multiple" up: meaning, Category 1 may have a parent of "root" - or 0 as a category_id. Category 3 may have parent as Category 1, Category 6 may have parent as Category 3, Category 20 may have parent as Category 6, and so on. With this example, our stack would look like the following for the Category 20: Root (or Id=0), 1, 3, 6

 

However, at the same time, Category 37's parent could be 3 as well as Category 6's. So we create a problem. How do we dynamically pull our categories and show their hierarchy? For this listed example, the hierarchy would look like:

 

0 -> 1 -> 3 -> 6 -> 20  [For Category 20]

0 -> 1 -> 3 -> 37          [For Category 37]

 

Actual Table:

table.jpg

 

We can use arrays or whatever means php 5 offers. But remember, just as Category 37 is a child, it could also turn out to be a parent in the future. To solve this, I've tried using eval and arrays. However, I get errors when doing this. My approach was to store the array pointers in an array and then loop through them and store them in a variable string format and then do an eval.

 

However, nothing has worked and I'm crunched for time on this project. Please, please help!! A point in the right direction will be useful as well!!

 

Thank you,

Alex

Link to comment
Share on other sites

quote modified from original context at: http://www.sitepoint.com/article/hierarchical-data-database/

 

for anyone who runs into this problem in the future, here is an example of hierarchy:

 

function display_children($parent, $level) {
   // retrieve all children of $parent
   $data = new mysql_database;
   $result = $data->select("select * from " . TABLE_BUSINESS_CATEGORIES . " where parent='".$parent."'");

   // display each child
   while ($row = $data->fetch_array($result)) {
       // indent and display the title of this child
       echo str_repeat('  ',$level).$row['category_name']." (parent = '".$row['parent']."' && id = '".$row['business_categories_id']."')"."\n"; // just doing a check on (parent= and id=) to ensure it's working

       // call this function again to display this
       // child's children
       display_children($row['business_categories_id'], $level+1);
   }
}

echo display_children('0', '0');

# this code solves miracles  thankx!

Link to comment
Share on other sites

The benefit to the way listed in the article I linked is it's not recursive and you're only doing one query to the database.

 

You may be right on that, however, when you're crunched for time (a week), the simplest option works the best ;) Thanks for the help though!!!

Alex

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.