Jump to content

[SOLVED] order by drop down


anthony-needs-you

Recommended Posts

Does anyone know how to create a drop down menu that orders by destination, price, duration.

 

My php is below:

 

//check if the starting row variable was passed in the URL or not
if (!isset($_GET['startrow']) or !is_numeric($_GET['startrow'])) {
//we give the value of the starting row to 0 because nothing was found in URL
  $startrow = 0;
//otherwise we take the value from the URL
} else {
  $startrow = (int)$_GET['startrow'];
}

$query = "SELECT id, destination, airport, resort, hotel, board, duration, departureDate, expireDate, price, description, stars,  customerRef FROM test WHERE expireDate > CURDATE() LIMIT $startrow, 10";
$result = mysql_query($query) or die('Error : ' . mysql_error());

while(list($id, $destination, $airport, $resort, $hotel, $board, $duration, $departureDate, $expireDate, $price, $description, $stars,  $customerRef) = mysql_fetch_array($result, MYSQL_NUM))
{

?>
        <div id="hol-wrapper">
        <div class="holiday-hedlistwrap">
        <ul class="hol-hedlist">
        <li class="long">Destination</li>
	<li class="med">Airport</li>
	<li class="long">Resort</li>
        <li class="long">Hotel</li>
	<li class="med">Board</li>
	<li class="med">Duration</li>
        <li class="med">Depart On</li>
        <li class="small">Price</li>
	</ul>
        </div>
        
        <div class="holiday-detailswrap">
        <ul class="hol-details">
        <li class="long"><?php echo $destination;?></li>
	<li class="med"><?php echo $airport;?></li>
	<li class="long"><?php echo $resort;?></li>
        <li class="long"><?php echo $hotel;?></li>
	<li class="med"><?php echo $board;?></li>
	<li class="med"><?php echo $duration;?></li>
        <li class="med"><?php echo date('d/m/Y',strtotime($departureDate));?></li>
        <li class="small">£<?php echo $price;?></li>
	</ul>
        <div class="clearboth" id="foo"><!--empty--></div>
        </div>
        
        <div class="holiday-descwrap">
        <ul class="hol-hedlist">
        <li class="huge">Description</li>
        <li class="med">Rating</li>
        <li class="med">Ref. No.</li>
	</ul>
        </div>
        
        <div class="holiday-detailswrap">
        <ul class="hol-details">
        <li class="huge"><?php echo $description=nl2br($description);?><?php echo date('d/m/Y',strtotime($expireDate));?></li>
        <li class="med"><?php echo $stars;?></li>
        <li class="ref"><?php echo $customerRef;?></li>
	</ul>
        <div class="clearboth" id="foo"><!--empty--></div>
        </div>
        <div id="hol-footer">
        </div><!--hol-footer-->
        <div class="clearboth" id="foo"><!--empty--></div>
        </div><!-- hol-wrapper-->


	<?php
}


//now this is the previous link..
echo '<a href="'.$_SERVER['PHP_SELF'].'?startrow='.($startrow-10).'">Previous</a>';

//now this is the next link..
echo '<a href="'.$_SERVER['PHP_SELF'].'?startrow='.($startrow+10).'">Next</a>';
?>

 

Any help is really apprieciated

Link to comment
Share on other sites

You would need to use javascript onChange event that when the drop down is changed it posts back to the page and at the top of your page, check for that post data. If the post data is there then add what was selected to the end of the ORDER BY, if nothing was selected make it the default of `price`.

Link to comment
Share on other sites

something along these lines?:

 

<script language="JavaScript" type="text/javascript">
function display_data(id) { 
    xmlhttp=GetXmlHttpObject();
    if (xmlhttp==null) {
        alert ("Your browser does not support AJAX!");
        return;
    } 
    var url="holiday-template.php";
    url=url+"?id="+id;
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 || xmlhttp.readyState=="complete") {
            document.getElementById('employ_data').innerHTML=xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
}
</script>

 

<select onchange="display_data(this.value);">
    <option>Order by</option>
    <?php
    $query="select price, duration from test order by destination asc";
    $result=mysql_query($query);
    while(list($price, $duration)=mysql_fetch_row($result)) {
        echo "<option value=\"".$price."\">".$duration."</option>";
    }
    ?>
</select>

 

<?php
if (is_numeric($_GET['id'])) {

    $query="select price, duration from test where id=$_GET[id]";
    $result=mysql_query($query);
    $employ=mysql_fetch_array($result);
}
?>

Link to comment
Share on other sites

Do it this way as well, with the name off the submit button creating the

order by condition.

 

<?php


if(isset($_POST['letters'])){

       $letters=$_POST['letters'];

	$type="letters";

}elseif(isset($_POST['numbers'])){

       $numbers=$_POST['numbers'];

	$type="numbers";

}elseif(isset($_POST['double_numbers'])){

       $double_numbers=$_POST['double_numbers'];

	$type="double_numbers";
}



$sql="select * from what_ever order by $type limit 10";

echo "$sql<br><br>";



?>
<form method="POST" action=" ">

<select name="letters">

<option value="a">a</option>
<option value="b">b</option>
<option value="c">c</option>

</select>

<br><br>

<input type="submit" name="letters" value="Send">

</form>

<br><br>

<form method="POST" action=" ">

<select name="numbers">

<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>

</select>


<br><br>

<input type="submit" name="numbers" value="Send">

</form>

<br><br>

<form method="POST" action=" ">

<select name="double_letters">

<option value="aa">aa</option>
<option value="bb">bb</option>
<option value="cc">cc</option>

</select>

<br><br>

<input type="submit" name="double_numbers" value="Send">

</form>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.