Populate a dropmenu with database values

12 posts by 4 authors in: Forums > CMS Builder
Last Post: January 19, 2009   (RSS)

By Ryan - November 18, 2008

Hi Folks,

I have a table called 'cars' in my database with a list menu field called 'make'. I want to create a dropdown menu on the website that will allow users to select from the all values from this 'make' field. Is this possible?

I want to use it as part of a search feature for filtering search results.

Thanks

Re: [ryan_dot] Populate a dropmenu with database values

By Dave - November 19, 2008

Hi Ryan,

Try something like this:

<?php
// get field options
$tablename = 'widgets';
$fieldname = 'color';
$selectedValue = '';
$valuesAndLabels = getListOptions($tablename, $fieldname);
$optionsHTML = getSelectOptions($selectedValue, array_keys($valuesAndLabels), array_values($valuesAndLabels));
?>

Widgets Colors:

<select name="color">
<?php echo $optionsHTML ?>
</select>


Hope that helps!
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] Populate a dropmenu with database values

By Ryan - November 19, 2008

Hi Dave, i must try that the next time, it looks more efficient than what i ended up doing.

I ended up creating a seperate table for the 'make' and linked it to the cars table and used this code to create a drop down menu for the search feature.

<select name="make" class="dropdown" id="make" >
<option value="">Any</option>
<?php foreach ($makeRecords as $record): ?>
<option value="<?php echo $record['make'] ?>"><?php echo $record['make'] ?></option>
<?php endforeach; ?>
</select>

It works fine for me, and since there is now a table for 'make' it allows the site owner to add new makes to the database and it automatically updates the search menu.

Thanks

Re: [ryan_dot] Populate a dropmenu with database values

By sev - January 12, 2009

hi guys

id like to do something similar but im having a few problems with my understanding of the code [:/]

hopefully someone here can point me in the right direction

i have a category to list business services which includes a field "type" (electrician, plumber etc)

id like to have on my list viewer page a drop down which populates with only the types which currently have listings entered in the database via which visitors can filter the search results then as my client adds more listings in each of the different types the drop down auto populates...

the following code works fine on the primary list viewer page and filters the results fine but once ive select one of the available types and jumped to the search results only the type that i have selected in the previous search is displayed in the drop down and not all available (hope that makes sense!)

<form id="form" action="/services.php">

<select name="type" class="dropdown" id="type" >
<option value="">Any</option>
<?php foreach ($servicesRecords as $record): ?>
<option value="<?php echo $record['type'] ?>"><?php echo $record['type'] ?></option>
<?php endforeach; ?>
</select>
<input type="submit" value="Search">
</form>

thanks for your support

Re: [sev] Populate a dropmenu with database values

By Ryan - January 12, 2009

Make sure you have the load records code on the services.php page as well as your initial search page.

It would look something like this.

<?php

require_once "/xxx/cmsadmin/lib/viewer_functions.php";

list($servicesRecords, $servicesMetaData) = getRecords(array(
'tableName' => 'services',
'loadUploads' => '0',
'allowSearch' => '0',
));

?>

Re: [sev] Populate a dropmenu with database values

By Ryan - January 12, 2009

Sorry it should be...

'allowSearch' => '1', NOT 'allowSearch' => '0',

and you shouldn't need 'loadUploads' => '0',

See if that works for you.

Re: [ryan_dot] Populate a dropmenu with database values

By sev - January 12, 2009

thanks ryan

unfortunately this hasnt solved it and im back to only the searched for 'type' appearing in the drop down after any search

bit of an odd one this

Re: [sev] Populate a dropmenu with database values

By sev - January 12, 2009

many thanks to ryan for spending time with me on this today! [:)]

we have it to a stage where the drop down loads only the types which have listings both before and after the search however if i have for example 2 listings as 'type' plumber it loads plumber into the drop down twice so im just missing a way of it only pulling in the 'type' once

im a newbie using php so this is a little above my head for now, dave could you possibly give some advice on how i could achieve this either by a seperate table as suggested by ryan or some other coding?

i have attaced the list page services.php to show how it looks at present:
Attachments:

services.php 5K