PDA

View Full Version : dynamic menu in search form


russlock
11-12-2005, 10:41 AM
Hi everyone, hope someone can help me.

Here goes ..

I have a search form and i have 2 search options on it.
a standard text box which is working fine on the results page as it is using a wildcard for searching, but i also have a dropdown menu which is being dynamically populated from the database.

it is showing me all the records (catagories), but when i select one of the catagories and hit the search button i get nothing.

any help would be great.

Thanks in advance.

Russ

Creative Insanity
11-12-2005, 11:20 AM
You need to setup a selective search for each catagory.
In your database have cat field in your table and use those when setting up the dropdown for the catagory referance.
That way it will only search the catagories for the cat field you have selected in your database.

russlock
11-12-2005, 11:45 AM
I think i have already done that.

here is the code for the search form.


<form id="searchform" name="searchform" method="get" action="catalog.php?rsTemplates=true">
<table width="100%" border="2" cellspacing="2" cellpadding="2">
<tr>
<td>SEARCH</td>
</tr>
<tr>
<td>Type:
<label>
<select name="listtype" size="1" id="listtype">
<option value=""></option>
<?php
do {
?>
<option value="<?php echo $row_rsType['tID']?>"><?php echo $row_rsType['tname']?></option>
<?php
} while ($row_rsType = mysql_fetch_assoc($rsType));
$rows = mysql_num_rows($rsType);
if($rows > 0) {
mysql_data_seek($rsType, 0);
$row_rsType = mysql_fetch_assoc($rsType);
}
?>
</select>
</label></td>
</tr>
<tr>
<td>Category:
<label>
<select name="listcat" id="listcat">
<option value=""></option>
<?php
do {
?>
<option value="<?php echo $row_rsCategory['catID']?>"><?php echo $row_rsCategory['catname']?></option>
<?php
} while ($row_rsCategory = mysql_fetch_assoc($rsCategory));
$rows = mysql_num_rows($rsCategory);
if($rows > 0) {
mysql_data_seek($rsCategory, 0);
$row_rsCategory = mysql_fetch_assoc($rsCategory);
}
?>
</select>
</label></td>
</tr>
<tr>
<td>Keywords:
<label>
<input name="keywords" type="text" id="keywords" />
</label></td>
</tr>
<tr>
<td><label>
<input name="searchbutton" type="submit" id="searchbutton" value="Search" />
</label></td>
</tr>
</table>
</form>

and these are the recordsets set up.

<?php
$keywords_rsMaster = "%";
if (isset($_GET['keywords'])) {
$keywords_rsMaster = (get_magic_quotes_gpc()) ? $_GET['keywords'] : addslashes($_GET['keywords']);
}
$listtype_rsMaster = "%";
if (isset($_GET['listtype'])) {
$listtype_rsMaster = (get_magic_quotes_gpc()) ? $_GET['listtype'] : addslashes($_GET['listtype']);
}
$listcat_rsMaster = "%";
if (isset($_GET['listcat'])) {
$listcat_rsMaster = (get_magic_quotes_gpc()) ? $_GET['listcat'] : addslashes($_GET['listcat']);
}
mysql_select_db($database_mytemplates, $mytemplates);
$query_rsMaster = sprintf("SELECT * FROM templates, type,category WHERE (templates.description LIKE '%%%s%%' OR templates.name LIKE '%%%s%%' OR type.tname LIKE '%%%s%%' OR category.catname LIKE '%%%s%%'OR templates.tID = '%%%s%%' OR templates.catID = '%%%s%%') AND templates.tID = type.tID AND templates.catID = category.catID", $keywords_rsMaster,$keywords_rsMaster,$keywords_rs Master,$keywords_rsMaster,$listtype_rsMaster,$list cat_rsMaster);
$rsMaster = mysql_query($query_rsMaster, $mytemplates) or die(mysql_error());
$row_rsMaster = mysql_fetch_assoc($rsMaster);
$totalRows_rsMaster = mysql_num_rows($rsMaster);

mysql_select_db($database_mytemplates, $mytemplates);
$query_rsCategory = "SELECT * FROM category";
$rsCategory = mysql_query($query_rsCategory, $mytemplates) or die(mysql_error());
$row_rsCategory = mysql_fetch_assoc($rsCategory);
$totalRows_rsCategory = mysql_num_rows($rsCategory);

mysql_select_db($database_mytemplates, $mytemplates);
$query_rsType = "SELECT * FROM type";
$rsType = mysql_query($query_rsType, $mytemplates) or die(mysql_error());
$row_rsType = mysql_fetch_assoc($rsType);
$totalRows_rsType = mysql_num_rows($rsType);
?>

thanks a lot

Creative Insanity
11-12-2005, 06:30 PM
The most important part to this is your database.
Make sure that there is a cat (catagory) field in your database. Most important as without that then you will get the wrong result.
Then setup your results page with the cat field in mind with something like this.

On your form page name the dropdown field (we will call it cats for now), and with your dropdown list values, the label can be anything you want, but the VALUE MUST be the same as you have used for in your cat field in the database.