Try using this code,
- Changes what I have done are, created anchor's of your field name as table header
- by default the headers are in ASC order, if we click it, it changes to DESC order
- the sql query then uses the field name in GET to order the records
- the search term is also then saved in the GET request so that we can persist it ahead
- as search term is sometimes received in GET and sometimes in POST, have used REQUEST here, to get data
if (!empty($search_name)){
if (strlen($search_name)>=3) {
if(empty($_REQUEST['searchTrm']))
$_REQUEST['searchTrm'] = 'price';
if(empty($_REQUEST['order']))
$_REQUEST['order'] = 'ASC';
$query = "SELECT * FROM `shoes` WHERE `brand/model` LIKE '".mysql_real_escape_string($search_name)."%' ORDER BY ".$_REQUEST['searchTrm']." ".$_REQUEST['order'];
$query_run = mysql_query($query);
$query_num_rows = mysql_num_rows($query_run);
if ($query_num_rows>=1) {
echo $query_num_rows.' Results found:<br>';
?>
<table border=1>
<tr>
<td>
<?php
//use your sql field name as searchTrm value
if($_REQUEST['searchTrm'] == 'brand/model' && $_REQUEST['order'] == 'DESC') { ?>
<a href='?searchTrm=brand/model&order=ASC&search_name=<?php echo $_REQUEST['search_name']?>' title = 'ASC'>Brand Name</a>
<?php } else { ?>
<a href='?searchTrm=brand/model&order=DESC&search_name=<?php echo $_REQUEST['search_name']?>' title = 'DESC'>Brand Name</a>
<?php } ?>
</td>
<td>
<?php if($_REQUEST['searchTrm'] == 'price' && $_REQUEST['order'] == 'DESC') { ?>
<a href='?searchTrm=price&order=ASC&search_name=<?php echo $_REQUEST['search_name']?>' title = 'ASC'>Price</a>
<?php } else { ?>
<a href='?searchTrm=price&order=DESC&search_name=<?php echo $_REQUEST['search_name']?>' title = 'DESC'>Price</a>
<?php } ?>
</td>
<td>Image</td>
</tr>
<?php
while ($query_row = mysql_fetch_array($query_run)) {
$picture = $query_row['picture'];
?>
<tr>
<td><?php echo $query_row['brand/model'];?></td>
<td><?php echo $query_row['price'];?></td>
<td><img src='image.php?id=<?php echo $query_row['id'];?>' width='300' height='200' /></td>
</tr>
<?php
}
?> </table> <?php
} else {
echo 'No Results Found.';
}
} else {
echo 'Text field must be more than 3 characters.';
}
} else {
echo 'Text Field Cannot be Empty!';
}
}