Populate subcategories from parent categories

6 posts by 2 authors in: Forums > CMS Builder
Last Post: September 23, 2014   (RSS)

By Jesus - September 17, 2014 - edited: September 17, 2014

Hi,

I've been the forums and I'm kind of lost trying to get what I need. I'm looking to create a conditional list from another list for my categories/subcategories. Will try to explain this the best possible way below.

On my products table, I need to use 3 categories and each category will have their own subcategories.

I don't want to create just a single list of categories/subcategories all together. I want to create 2 lists fields.

The 1st field (Category), will have the 3 main categories and the 2nd field, will have their proper subcategories for each particular category previously selected. 

Is there a way that I can use to have those 2 list fields related to each other so if I choose CATEGORY 1, I'll be able to select just the subcategories related to the parent CATEGORY 1 and so on?

So far... I created a categories table, where I'm using 3 fields (Num, Category, Parent), here i'm able to just add categories and select which category will act as a parent for every category added. So far so good, but...

On my products table, while trying to add a record, I need to display the PARENT categories, on my category list (I can do that) but then, I don't have a clue how can I select just the subcategories related to the selected PARENT CATEGORY on a 2nd list.

Any ideas?, I'm lost here... thanks in advance,

Jesus

By gregThomas - September 19, 2014

Hi Jesus,

I think I understand what you're trying to achieve. So when a user selects a family from the first drop down, the second drown will only display categories that are linked to that family?

I think you need to use an advanced MySQL drop down, and then use a ESCAPED_FILTER_VALUE to filter the drop down. Here are a couple of posts that go over how to implement the system:

http://www.interactivetools.com/forum/forum-posts.php?postNum=2230106#post2230106

http://www.interactivetools.com/forum/forum-posts.php?postNum=2232494#post2232494

Let me know if you have any questions on how to implement this feature.

Cheers,

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By Jesus - September 19, 2014

AWESOME Greg!!!

Both threads helped me!!

Thank you so much!

Jesus

By Jesus - September 22, 2014

Hi Greg,

I made this possible but now, I've another question. I did it previously on other CMSBuilder project but on this one, its the 1st time I'm using this ESCAPED_FILTER_VALUE method.

I'm creating a filer by category on my front end page, so I can display all products for all categories or just a category specific products. I'm assuming that the category value remains on the database once I save the record.

Here's the code I'm using:

<a href="#" class="btn btn-default" data-filter="*">Todos</a>

<?php foreach (getListOptions("productos", "categoria") as $value => $label):?>
   <a href="#" class="btn btn-default" data-filter=".<?php echo $value;?>"><span class="hidden-xs"><?php echo $label;?></span></a>
<?php endforeach ?>

I'm not getting any category (categoria) value here. Am I missing something?

Thanks for the head-ups,

Jesus

By gregThomas - September 23, 2014

Hi Jesus,

I suspect the issue is that you don't have a value set for ESCAPED_FILTER_VALUE on the front end of your site, and so no values are being returned. You could update your MySQL string for your list field so that if no value is set for ESCAPED_FILTER_VALUE, all of the records are returned:

<?php if(@$ESCAPED_FILTER_VALUE): ?> 
 Current MySQL call here
<?php else: ?>
 MySQL call to return all data instead.
<?php endif; ?>

Another way to do this would be to use getRecords to return all of your categories on the front end page, and cycle through those instead:

<?php

  // load records from 'members'
  list($categorias, $categoriaMeta) = getRecords(array(
    'tableName'   => 'categoria',
    'loadUploads' => true,
    'allowSearch' => false,
  ));


?>
<a href="#" class="btn btn-default" data-filter="*">Todos</a>

<?php foreach ($categorias as $categoria):?>
   <a href="#" class="btn btn-default" data-filter=".<?php echo $categoria['num'];?>"><span class="hidden-xs"><?php echo $categoria['title'];?></span></a>
<?php endforeach ?> 

So the above code will return all categories from the categoria table, then cycle through them and display the appropriate num and title. This is just example code, so you'll probably have to make a few changes to get it working with your system.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com