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)

By Mikey - November 17, 2010 - edited: November 17, 2010

I've got an Events page that I need to pull categories records from Nine existing categories associated with other site pages. So, I've created nine separate category options available to choose from by setting each of the nine separate categories up as a field - list within the Events section editor. Each of the nine categories is set as a list > pulldown > get options from database (advanced) value: name, value: name.

The nine separate categories created within the Events section editor are:
city_categories
service_categories
club_categories
economic_categories
environment_categories
event_categories
park_rec_categories
resident_categories
visitor_categories

Each of these categories represent a Category Menu created to manage data related to a single page they are directly associated with. Since the nine categories already exist, I'm trying to pull the categories and their subcategory records into the Events so I can associate an event posting with a category that the event is related to. The code below works, but in the drop down menu it shows every single parent category and subcategory regardless of whether a category has been selected within the Event posted or not. It shows all categories... even if no event is associated with the category option.

Is there a way to make the categories appear only if a given category has been selected for an event? Otherwise if the category is not used for any events, it does not appear in the drop down menu. Also, is there a way to sort the categories alphabetically within the drop down menu?

I've been racking my brains over this for days now and I just can't figure it out.
Thanks Zick

<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php


// load viewer library
$libraryPath = 'abc/lib/viewer_functions.php';
$dirsToCheck = array('/home/content/a/b/c/abcwebsite/html/','','../','../../','../../../');
foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }

// load master page records
list($eventsRecords, $eventsMetaData) = getRecords(array(
'tableName' => 'events',
));

// load category records for drop down menu
list($club_categoriesRecords, $selectedCategory) = getCategories(array(
'tableName' => 'club_categories',
));

list($city_categoriesRecords, $selectedCategory) = getCategories(array(
'tableName' => 'city_categories',
));

list($service_categoriesRecords, $selectedCategory) = getCategories(array(
'tableName' => 'service_categories',
));

list($economic_categoriesRecords, $selectedCategory) = getCategories(array(
'tableName' => 'economic_categories',
));

list($event_categoriesRecords, $selectedCategory) = getCategories(array(
'tableName' => 'event_categories',
));

list($environment_categoriesRecords, $selectedCategory) = getCategories(array(
'tableName' => 'environment_categories',
));

list($park_rec_categoriesRecords, $selectedCategory) = getCategories(array(
'tableName' => 'park_rec_categories',
));

list($resident_categoriesRecords, $selectedCategory) = getCategories(array(
'tableName' => 'resident_categories',
));

list($visitor_categoriesRecords, $selectedCategory) = getCategories(array(
'tableName' => 'visitor_categories',
));


?>
<?PHP
function maxWords($html, $maxWords) {
$html = str_replace("<li>","<p>",$html);
$html = str_replace("</li>","</p>",$html);
$html = str_replace("<p>","*P*",$html);
$html = str_replace("</p>","*/P*",$html);
$html = str_replace("<"," <",$html);
$text = strip_tags($html);
$words = preg_split("/\s+/", $text, $maxWords+1);
if (count($words) > $maxWords) { unset($words[$maxWords]); }
$output = join(' ', $words);
$output=str_replace("*P*","<p>",$output);
$output=str_replace("*/P*","</p>",$output);
$output.="</p>";

return $output;
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>

<body>
<!-- search filter -->

<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>

<!-- first category -->
<?php foreach ($club_categoriesRecords as $categoryRecord): ?>
<option value="<?php echo $categoryRecord['name'] ?>"><?php echo maxWords($categoryRecord['name'], 3); ?></option>
<?php endforeach ?>
<!-- second category -->
<?php foreach ($city_categoriesRecords as $categoryRecord): ?>
<option value="<?php echo $categoryRecord['name'] ?>"><?php echo maxWords($categoryRecord['name'], 3); ?></option>
<?php endforeach ?>
<!-- third category -->
<?php foreach ($service_categoriesRecords as $categoryRecord): ?>
<option value="<?php echo $categoryRecord['name'] ?>"><?php echo maxWords($categoryRecord['name'], 3); ?></option>
<?php endforeach ?>
<!-- fourth category -->
<?php foreach ($economic_categoriesRecords as $categoryRecord): ?>
<option value="<?php echo $categoryRecord['name'] ?>"><?php echo maxWords($categoryRecord['name'], 3); ?></option>
<?php endforeach ?>
<!-- fifth category -->
<?php foreach ($event_categoriesRecords as $categoryRecord): ?>
<option value="<?php echo $categoryRecord['name'] ?>"><?php echo maxWords($categoryRecord['name'], 3); ?></option>
<?php endforeach ?>
<!-- sixth category -->
<?php foreach ($environment_categoriesRecords as $categoryRecord): ?>
<option value="<?php echo $categoryRecord['name'] ?>"><?php echo maxWords($categoryRecord['name'], 3); ?></option>
<?php endforeach ?>
<!-- seventh category -->
<?php foreach ($park_rec_categoriesRecords as $categoryRecord): ?>
<option value="<?php echo $categoryRecord['name'] ?>"><?php echo maxWords($categoryRecord['name'], 3); ?></option>
<?php endforeach ?>
<!-- eighth category -->
<?php foreach ($resident_categoriesRecords as $categoryRecord): ?>
<option value="<?php echo $categoryRecord['name'] ?>"><?php echo maxWords($categoryRecord['name'], 3); ?></option>
<?php endforeach ?>
<!-- ninth category -->
<?php foreach ($visitor_categoriesRecords as $categoryRecord): ?>
<option value="<?php echo $categoryRecord['name'] ?>"><?php echo maxWords($categoryRecord['name'], 3); ?></option>
<?php endforeach ?>
</select>
<input name="submit" type="submit" class="button" value="begin">
</form>
<!-- /search filter -->
</body>
</html>

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/