Dynamically populate a drop down menu from one of my fields and remove duplicates

6 posts by 2 authors in: Forums > CMS Builder
Last Post: December 27, 2012   (RSS)

By gkornbluth - December 21, 2012 - edited: December 21, 2012

Hi All,

I’m stumped again...

Back in 2009, there was a now archived post #70187 that asked the same question “How do I dynamically populate a drop down menu from one of my fields (and remove duplicates from the drop down)”

It suggested using the following

<?php foreach ($categoryRecords as $record):
$productCategory[] = $record['category'];
endforeach;
?>
<?php $productCategory = array_unique($productCategory); ?>

<select name="category_match">
<?php foreach ($productCategory as $record): ?>
<option value="<?php echo htmlspecialchars($record) ?>"><?php echo htmlspecialchars($record) ?></option>
<?php endforeach; ?>
</select>

In my implementation, I’m using the record number for option values and the pseudo :label field for label values.

When I use the code below, I get this error when a search is executed:

Notice: Undefined variable: mobilityAidCategory in /hsphere/local/home/gkornblu/jkwebdesigns.com/drlaura7.php on line 113 Warning: array_unique(): The argument should be an array in /hsphere/local/home/gkornblu/jkwebdesigns.com/drlaura7.php on line 113

<?php foreach ($mobility_aidsRecords as $record):
$mobilityAidCategory[] = $record['mobility_aid_category:label'];
endforeach;
?>
<?php $mobilityAidCategory = array_unique($mobilityAidCategory); ?>
<select name="mobility_aid_category_match">
<?php foreach ($mobilityAidCategory as $record): ?>
<option value="<?php echo htmlspecialchars($record) ?>"><?php echo ($record) ?></option>
<?php endforeach; ?>

</select>

Any ideas?

Thanks,

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Hi Jerry,

Are these labels coming from a list field? If they are then this might be an easier way of doing it:

 <select name="profession" class="textbox" id="profession">
   <option value= "">Select Publication Type</option>
   <?php foreach(getListOptions('table_name_here', 'mobility_aid_category') as $value => $name): ?>
      <option value="<?php echo $value; ?>"><?php echo $name; ?></option>
   <?php endforeach; ?>
</select>

If you replace table_name_here with the table name that contains the list field you want to use.

getListOptions is a CMS Builder function that should automatically generate an array of drop down options for you.

Thanks

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By gkornbluth - December 22, 2012 - edited: December 22, 2012

Hi Greg,

Thanks for this. It certainly simplified the code required.

I've modified the code you suggested to eliminate those categories that do not have records associated with them, but can't seem to get rid of the duplicate entries in the list when there is more than one record in that category.

IE If there are 3 record in one of the categories that category will appear in the pull doen options list three times.

Here's the code that I'm using:

 <select name="mobility_aid_category" >
   <option value= "">Choose The Category To Display And Click/Tap The Search Button</option>
   <?php foreach(getListOptions('mobility_aids', 'mobility_aid_category') as $value => $name): ?>
   <?php foreach ($mobility_aidsRecords as $record): ?>
   <?php $in_list = $record['mobility_aid_category:label'] ?>
    <?php if ($name == $in_list ):?>
      <option value="<?php echo $value; ?>"><?php echo $name; ?></option>
      <?php endif  ?>
   <?php endforeach; ?><?php endforeach; ?>
</select>
 

Thanks,

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Hi Jerry,

If your already pulling all of the relevant records for the section and you only want to show items in the drop down that have been already selected, it might be easier to do something like this:

<?php 
//Create an array to store the values that have been selected
$selectArray = array(); 
//foreach section item
foreach($mobility_aidsRecords as $record){ 
  //If the record has something selected for mobility_aid_category 
  if(@$record['mobility_aid_category:label']){
    //If the selected value isn't already in the array
    if(!in_array(@$record['mobility_aid_category:label'],$selectArray)){ 
      //Add it to the $selectArray
      $selectArray[$record['mobility_aid_category']] = $record['mobility_aid_category:label']; 
    }
  } 
}
?>

<select name="mobility_aid_category" >
  <option value= "">Choose The Category To Display And Click/Tap The Search Button</option>
  <?php foreach ($selectArray as $value => $name): ?>
    <option value="<?php echo $value; ?>"><?php echo $name; ?></option>
  <?php endforeach; ?>
</select> 

Let me know if this doesn't work.

Thanks

Greg

Greg Thomas







PHP Programmer - interactivetools.com

Hi Greg,

Thanks for setting this straight.

It works perfectly, and makes much more sense.

(Now if I can only apply the principle to other issues.)

Hope you enjoyed the time off for the holidays,

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php