Creating Advanced Search Web page

3 posts by 2 authors in: Forums > CMS Builder
Last Post: January 31, 2011   (RSS)

Hello,

I am setting up a classified ads website allowing people to post ads of equipment for sale.

I am trying to create an "Advanced Search" web page (i.e. a page that lets the user search for listings using multiple fields rather than just a single keyword).

I have managed to insert a dropdown field that allows the user to select the "Category" they want to filter the search results by. The dropdown menu is correctly populated with category options I have setup in the Admin control panel.

However I can't get any other dropdown menu, for example a field called "Condition" (i.e. the condition of the product being listed - brand new, good, refurbished), to populate with the options I have created under the "listings" MySQL table.

The code is correctly populating the Category dropdown field, however it is populating the "Condition" dropdown menu with the Condition "values" of the listings that have been created in the system. I would like the Condition dropdown menu to instead list the Condition "options", which I have defined in the Admin control panel.

Below is the code for my search.php web page:

<?php
require_once "init.php";
list($homepageRecords, $homepageListDetails) = getRecords(array(
'tableName' => 'homepages')
);

?>

<?php
list($listingsRecords, $listingsMetaData) = getRecords(array(
'tableName' => 'listings',
));
?>

<?php
require_once "cmsAdmin/lib/viewer_functions.php";
list($categoryRecords, $categoryMetaData) = getRecords(array(
'tableName' => 'category',
'allowSearch' => '1',
));
?>

<?php include "include_header.php"; ?>

<h2>Search Listings</h2>

<form method="post" action="searchResults.php">

<table border="0" cellspacing="10" cellpadding="0">
<tr>
<td>Search For:</td>
<td><input type="text" name="product_keyword" value=""></td>
</tr>
<tr>
<td>Category:</td>
<td>
<select name="category_match">
<option value="">All Categories</option>
<?php foreach ($categoryRecords as $record): ?>
<option value="<?php echo htmlspecialchars($record['num']) ?>"><?php echo htmlspecialchars($record['name']) ?></option>
<?php endforeach; ?>
</select>
</td>
</tr>
<tr>
<td>Condition:</td>
<td>
<select name="condition_match">
<option value="">Any Condition</option>
<?php foreach ($listingsRecords as $record): ?>
<option value="<?php echo htmlspecialchars($record['num']) ?>"><?php echo htmlspecialchars($record['condition']) ?></option>
<?php endforeach; ?>
</select>
</td>
</tr>
</table>
<br/>
<div align="left">
<input type="submit" name="search" value=" Search Listings "/>
</div>
</form>
<?php include "include_footer.php"; ?>



Any help to get this working would be greatly appreciated.

Many thanks,
Greg

Re: [gversion] Creating Advanced Search Web page

By Jason - January 31, 2011

Hi,

The best way to do this is to use the function getListOptions. This takes in a given table name and field and returns an array or options. The option value is the index and the array value is the option label. Try changing your code to this:

<select name="condition_match">
<option value="">Any Condition</option>

<?php $conditions = getListOptions('listings','condition');?>
<?php foreach($conditions as $value => $label):?>
<option value="<?php echo htmlspecialchars($value); ?>"><?php echo htmlspecialchars($label); ?></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/