Multiple dynamic category levels

5 posts by 3 authors in: Forums > CMS Builder
Last Post: April 1, 2010   (RSS)

By keith_s - March 29, 2010

I need to be able to create a set of categories which can be accessed in a dropdown menu when the CMS user adds items (products in this case), to their database. I know how to do this, but I also need to be able to create a set of subcategories as well, which are also accessible in a dropdown list. I wouldn't mind if they were all on the same list as long as they were grouped by their parent category.

Ideally, it would be a dynamic javascript driven list which would populate itself based on their selection of categories.

How would I accomplish this? To see where I'm going with this, take a look at http://www.sharberoil.com/products_services.php. The product subcategories, for instance, are things like BF Goodrich, Cooper Tires, etc.

When I get through, this text will link to a page showing a list of products that are all related. The user will then be able to click onto a product to see detailed information, including a photo, description, etc.

How would I go about this? Any insight would be greatly appreciated.

Re: [webgfx] Multiple dynamic category levels

By Jason - March 30, 2010

Hi,

You can create a dropdown list that organizes categories by parent category. Here is the code that you can use to do that, you will need to change some of the names used depending on how you named your tables and fields:

<select>
<?php

list($categoryRecords, $categoryMetaData) = getRecords(array(
'tableName' => 'category',
));

foreach($categoryRecords as $record){
echo "<optgroup label='".$record['title']."'>";

list($subcategoryRecords, $subcategoryMetaData) = getRecords(array(
'tableName' => 'subcategory',
'where' =>'parent=\''.$record['title']."'",
'debugSql' =>'true'
));

foreach($subcategoryRecords as $subrecord){
echo "<option value=".$subrecord['num'].">".$subrecord['title']."</option>";
}
echo "</optgroup>";
}
?>
</select>


In this example, there are 2 tables: 1 for the parent category (called category) and 1 for the sub category (called subcategory). There is a field in the subcategory section called "parent". This is the "title" of the record in "category".

This will output a dropdown box with subcategories sorted by parent category.

Let me know if this works for you.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Multiple dynamic category levels

By Perchpole - April 1, 2010

Hi Jason -

I'm very interested in your response to this question. I'm particularly intruiged by your use of the line:

'debugSql' =>'true'

I don't think I've seen this used before. Am I right in thinking it's to prevent any errors appearing if the script cannot find any sub-categories?

:0/

Perchpole

Re: [Perchpole] Multiple dynamic category levels

By Jason - April 1, 2010

Hi,

Sorry about that. That line actually just prints out the MySQL query string, which can be used to debug problems.

Since you don't what to output that, just remove that line.

Thanks.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/