Populating search form from database

By andybarn - April 9, 2013

Hi 

I am using the Basic Realty plugin

On the search.php file that comes with package, the search form pulldowns are hard coded with the select options.

I want to pull these in dynamically from the cmsAdmin property_type field

For example, the property_type pulldown is as follows

<td>
<select name="property_type">
<option value="">&lt;select&gt;</option>
<option value="">Any</option>
<option>Home</option>
<option>Condo</option>
<option>Townhouse</option>
<option>Apartment</option>
<option>Mobile Home</option>
<option>Lot</option>
<option>Acreage</option>
<option>Duplex</option>
<option>Triplex</option>
<option>Farm or Ranch</option>
<option>Other</option>
</select>
</td>

Please could you provide an example of the code required to populate the property_type pulldown dynamically from the database. Please use the standard form as an example so I can see how this works and then will be able to apply the code to the other pulldowns.

Thanks for your help.

Kind Regards

Andy

By gregThomas - April 10, 2013 - edited: April 10, 2013

Hi,

You could use getListOptions function to retrieve them from the database, something like this should work:

$options = getListOptions('listings', 'property_type');

?>
<td>
  <select name="property_type">
    <option value="">&lt;select&gt;</option>
    <option value="">Any</option>
    <?php foreach($options as $value => $label): ?>
      <option value="<?php echo $value; ?>" ><?php echo $label; ?></option>
    <?php endforeach; ?>
  </select>
</td>

The getListOptions function returns an array of list options, the two variables required for it are the table and field name. 

Let me know if you have any questions.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com