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: [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: [ryan_dot] Populate a dropmenu with database values

By sev - January 12, 2009

hi ryan

many thanks for taking the time to reply much appreciated

i had the load records code on the services.php as follows

<?php

require_once "/home/site/public_html/cms/lib/viewer_functions.php";

list($servicesRecords, $servicesMetaData) = getRecords(array(
'tableName' => 'services',
'useSeoUrls' => true,
'perPage' => '20',
));

?>

i replaced it with your example code above which unlike before now allows all available types to be seen after the search (once its jump to example /services.php?type=Insurance) however its not filtering the results and displays all listings

if i flick 'loadUploads' => '0', & 'allowSearch' => '0', to 'true' i have the same issue as before where only the searched for type is displayed in the drop down

i should explain that the services.php page (the list page) is actually the initial search page as well

heres a copy of services.php as it stands at the moment sorry im having trouble explaining clearly

<?php

require_once "/home/playa/public_html/cms/lib/viewer_functions.php";

list($servicesRecords, $servicesMetaData) = getRecords(array(
'tableName' => 'services',
'loadUploads' => '0',
'allowSearch' => '0',
));
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Services</title>
<link href="/css/site.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="/js/mootools.js"></script>
<script type="text/javascript" src="/js/slideshow.js"></script>
</head>
<body>
<div id="header">
<img src="/css/test.jpg" alt="test" class="logo" />
<div id="search">
<p><strong>Search all Services</strong></p>
<form method="post" action="/services.php">
<div>
<input type="text" name="name,content_keyword" value="" />
<input name="submit" type="submit" class="submit-button" value="Search" />
</div>
</form>
</div>
</div>
<div id="menuwrap">
<? include 'english-menu.php'; ?>
</div>
<? include 'slideshow.php'; ?>
<div id="wrapper">
<div id="content">
<h1>Services Directory</h1>
<p>Here we can write some nice text</p>

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

<br />

<span class="line"></span>
<?php foreach ($servicesRecords as $record): ?>
<?php foreach ($record['images'] as $upload): ?>
<?php if ($upload['hasThumbnail']&&$record['advertiser']==1): ?>
<div class="advertiser">
<a href="<?php echo $record['_link'] ?>" title="Services - <?php echo $record['name'] ?>"><img src="<?php echo $upload['thumbUrlPath'] ?>" alt="<?php echo $record['name'] ?>" height="<?php echo $upload['thumbHeight'] ?>" class="listing-photo" /></a>

<?php endif ?>
<?php break; ?>
<?php endforeach ?>

<?php if ($record['advertiser']==1): ?><h2><?php echo $record['name'] ?></h2>
<p><strong>Telephone</strong>: <?php echo $record['telephone'] ?><br/>
<strong>Address</strong>: <?php echo $record['address'] ?><br />
<strong>Business Type</strong>: <?php echo $record['type'] ?></p>
<p><a href="<?php echo $record['_link'] ?>" title="<?php echo $record['name'] ?>">Read more about <?php echo $record['name'] ?></a></p>
<br style="clear:left" />
</div>
<?php else: ?>
<p><strong><?php echo $record['type'] ?></strong> - <strong><?php echo $record['name'] ?></strong> - <?php echo $record['telephone'] ?></p>
<?php endif ?>

<span class="line"></span>
<?php endforeach; ?>

<?php if ($servicesMetaData['invalidPageNum']): ?>
Results page '<?php echo $servicesMetaData['page']?>' not found, <a href="<?php echo $servicesMetaData['firstPageLink'] ?>">start over &gt;&gt;</a>.
<?php elseif (!$servicesRecords): ?>
<h2>No listings were found!</h2>
<?php endif ?>

<?php if ($servicesMetaData['prevPage']): ?>
<a href="<?php echo $servicesMetaData['prevPageLink'] ?>">&lt;&lt; Previous page</a>
<?php endif ?>
<?php if ($servicesMetaData['prevPage']&&$servicesMetaData['nextPage']): ?>
|
<?php endif ?>
<?php if ($servicesMetaData['nextPage']): ?>
<a href="<?php echo $servicesMetaData['nextPageLink'] ?>">Next page &gt;&gt;</a>
<?php endif ?>
<? include 'footer.php'; ?>
</div>
<? include 'slideshow-sidebar2.php'; ?>
</div>
<? include 'google.php'; ?>
</body>
</html>

best regards

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