Populate drop down search form field
3 posts by 2 authors in: Forums > CMS Builder
Last Post: November 6, 2010 (RSS)
By darrylo - November 3, 2010
I wanted to auto populate a drop-down form field in my search form. The field is a text field. The code I'm using of course duplicates the field for each: (i don't want duplicates) is there a way to do this?
I know I can use a text field on my search form but I was hoping I could get a list.
http://ridgefieldconnecticuthomes.com/listings.php
<select name="city">
<option value="">Any Town</option>
<?php
foreach ($listingsRecords as $record) {
print "<option value='{$listingsRecords['city']}'>{$record['city']}</option>";
}
?>
</select>
I know I can use a text field on my search form but I was hoping I could get a list.
http://ridgefieldconnecticuthomes.com/listings.php
<select name="city">
<option value="">Any Town</option>
<?php
foreach ($listingsRecords as $record) {
print "<option value='{$listingsRecords['city']}'>{$record['city']}</option>";
}
?>
</select>
Darryl
http://darrylo.com
http://darrylo.com
Re: [darrylo] Populate drop down search form field
By Jason - November 4, 2010
Hi Darrylo,
What you can do is use 2 foreach loops. The first one would be used to create an array of cities like this:
Any duplicates will overwrite each other, so you'll only every have one of each city. Next, you just need to output your cities like this:
Hope this helps
What you can do is use 2 foreach loops. The first one would be used to create an array of cities like this:
<?php
$cities = array();
foreach ($listingsRecords as $record){
$cities[$record['city']]=$record['city'];
}
?>
Any duplicates will overwrite each other, so you'll only every have one of each city. Next, you just need to output your cities like this:
<select name="city">
<option value="">Any Town</option>
<?php foreach($cities as $city): ?>
<option value="<?php echo $city;?>"><?php echo $city;?></option>
<?php endforeach?>
</select>
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/
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] Populate drop down search form field
By darrylo - November 6, 2010
Awesome - you're the man Jason - Thank you!
Darryl
http://darrylo.com
http://darrylo.com