Multiple dynamic category levels

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

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/

Re: [Jason] Multiple dynamic category levels

By Perchpole - April 1, 2010

Ah!

Well, either way, I've learned something new!

:0)

Perchpole