Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I would add a Parent_ID to the table which links to the same table’s ID field, but is NULL-able. Then, when you run through each category on the PHP side, look up sub-categories where the Parent_ID = $current_id. In order to get the root categories (uppermost), look up those where the Parent_ID = NULL.
This way you can make a recursive loop on the PHP side.
Here’s a rough idea on how you can make a recursive function.
NOTE: The Parent_ID field must be NULL-able because otherwise you cannot indicate a top-most category, since the Parent_ID must exist as an ID (assuming you properly link your tables).
Hope it helps!