Hi,
I'm trying to write a small software catelog search form, with a link that sorts the results. However, currently, if you filter the results using the form, then click on the link to sort it, the results of the form are lost. I think I need to save the queries to a session, but I'm not 100% sure how!
I'm using 2 queries in the form vendor & category, the link sorts by price. The code below is at the start of my page, and gets the form post:
if (isset($_POST ['vendor']) ) {
$vendor = $_POST ['vendor']; } else {
$vendor = '%';
}
if (isset($_POST ['category'])) {
$category = $_POST ['category']; } else {
$category = '%';
}
This code gets the URL variable 'price' and sets the query sort to ASC or DESC. It also changes the URL to the reverse, so if its set to ASC, you click on it, an ascending sort is done on the data, and the URL variable changes to DESC and vice versa.
if ($_GET['price']) {
$sort = $_GET['price'];
if ($sort == 'ASC') {
$url = 'DESC'; } else {
$url = 'ASC';
}
} else {
$sort = 'ASC';
$url = 'DESC';
}
The link is: <a href="search.php?price='. $url .'">Price</a>
My filter form is:
<form method="post" action="'. $_SERVER['PHP_SELF'] .'?price='. $url .'">
Filter by Vendor
<select name="vendor"><option value="%"> - </option>
<option value="microsoft">Microsoft</option>
<option value="symantec">Symantec</option>
Filter by Category
<select name="category">
<option value="%"> - </option>
<option value="operatingsystem">Operating System</option>
<option value="security">Security Software</option>
<input type="submit" value="submit" name="filter" alt="Filter Results" />
</form>
My search is:
$result = mysql_query("SELECT * FROM `database`.`software_products` WHERE `vendor` LIKE '". $vendor ."' AND `category` LIKE '". $category ."' ORDER BY price ". $sort ." LIMIT 0 , 30")
or die(mysql_error());
while($row = mysql_fetch_array( $result )) {
echo $row['product_name'];
echo $row['product_price']
}
I think I have to put session_start(); at the top of the page, and set $SESSION['vendor'] and $SESSION['category'] to $POST ['vendor'] & $POST ['category'], but I tried this, and it didn't seem to work!
Can anyone help, or point me in the right direction?
Thanks
Ben