Jump to content

How to reqquery the databse to sort results by form


AdRock

Recommended Posts

I have a search form which displays a list of results

 

I would like to be able to sort those results like you see on shopping websites where you can sort price by high/low

 

I have got the form which is supposed to call the page again with the results but it is not requerying the database or something else isn't working as the results are still in the same order

 

<?php
    $title = "Car Shares";
    include("includes/init.php");
    include_once("header.inc.php");

    if (!empty($_GET['address'])) $seats = $_GET['address'];

    if (!empty($_GET['departure'])) $departure = $_GET['departure'];

    if ($_GET['seats'] !="any" )  $seats = $_GET['seats'];

    if ($seats =="any") $seats="";

    if ($sortby =="str") {
$order ="start_street";
    }
    else if ($sortby =="dpt") {
$order ="depart_time";
    }
    else {
$order ="id";
    }

    if (!empty($address)) {
    $sql = "SELECT id, userid, seats_available, start_street, start_postcode, 
end_street, end_postcode, TIME_FORMAT(depart_time,'%H:%i') as depart, 
MATCH(start_street, start_postcode, end_street, end_postcode) AGAINST('$address*' IN BOOLEAN MODE)
as score FROM carshare WHERE MATCH(start_street, start_postcode, end_street, end_postcode) 
    AGAINST('$address*' IN BOOLEAN MODE) ";
    }
    else {
    	$sql = "SELECT id, userid, seats_available, start_street, start_postcode, 
end_street, end_postcode, TIME_FORMAT(depart_time,'%H:%i') as depart FROM carshare WHERE 1=1 ";
    }

    if (!empty($seats)) {
    	$sql .= "AND seats_available = '$seats' ";
    }
    if (!empty($departure)) {
    	$sql .= "AND depart_time = '$departure' ";
    }

    if (!empty($address)) {
    	$sql .= "ORDER BY score, $order ASC";
     }
    else {
    	$sql .=" ORDER BY $order ASC";
    } 
  
    /* Get timestamp before executing the query: */
    $start_time = getmicrotime();
    
    $result = mysql_query($sql) or die(mysql_error());

    $end_time = getmicrotime();

    if(!$result) die(mysql_error()); 
$err = mysql_num_rows($result); 
        if($err == 0) {
    echo "No matches met your criteria.";
}
else {
    ?>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<p style="text-align:right"><label>Sort results by:</label>
<select name="sortby" class="sort" style="width:130px;">
    <option value="any"></option>
    <option value="str">Street Name</option>
    <option value="dep" selected>Departure Time</option>
</select>
    	<input type="submit" name="sortbutton" value="Sort Results" class="sendbutton" /></p>
    </form>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.