Show only selected/used categories in a drop down menu. If not selected/used, the category does not appear.

3 posts by 2 authors in: Forums > CMS Builder
Last Post: November 21, 2010   (RSS)

Re: [zick] Show only selected/used categories in a drop down menu. If not selected/used, the category does not appear.

By Jason - November 18, 2010

Hi Zick,

So, if I understand correctly, you have 9 list fields in your events section and only want options that have been selected to appear in your drop down. Is that right?

If so, we don't even need to select all the different category menus, as we can build an array based on the events records of all the category options selected.

First we use a foreachloop to create the array:
$eventCategory = array();
foreach($eventsRecords as $event){
$eventCategory[$event['city_categories']]=$event['city_categories'];
$eventCategory[$event['service_categories']]=$event['service_categories'];
$eventCategory[$event['club_categories']]=$event['club_categories'];
$eventCategory[$event['economic_categories']]=$event['economic_categories'];
$eventCategory[$event['environment_categories']]=$event['environment_categories'];
$eventCategory[$event['event_categories']]=$event['event_categories'];
$eventCategory[$event['park_rec_categories']]=$event['park_rec_categories'];
$eventCategory[$event['resident_categories']]=$event['resident_categories'];
$eventCategory[$event['visitor_categories ']]=$event['visitor_categories '];
}


After that, we filter out any blank entries and sort it alphabetically:

$eventCategory=array_filter($eventCategory);
asort($eventCategory);


The variable $eventCategory now holds an array of all the categories that had been used. Now we just output that array:
<form method="post" action="event_list.php" >
<select name="club_categories,city_categories,service_categories,economic_categories,environment_categories,event_categories,park_rec_categories,resident_categories,visitor_categories_query" class="buttonCom">
<option value="">categories</option>
<?php foreach($eventCategory as $category):?>
<option value="<?php echo $category;?>"><?php echo $category;?></option>
<?php endforeach?>
</select>
</form>


Give this a try and let me know if this works out for you.

Hope this helps
---------------------------------------------------
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] Show only selected/used categories in a drop down menu. If not selected/used, the category does not appear.

By Mikey - November 21, 2010

Jason... many many thanks!!!
Zick