anthony-needs-you Posted December 19, 2008 Share Posted December 19, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/137686-solved-order-by-drop-down/ Share on other sites More sharing options...
waterssaz Posted December 19, 2008 Share Posted December 19, 2008 use the ORDER BY vlause for you sql query, then that means it is is in the order you want when you come to use the data in your php code. Quote Link to comment https://forums.phpfreaks.com/topic/137686-solved-order-by-drop-down/#findComment-719622 Share on other sites More sharing options...
anthony-needs-you Posted December 19, 2008 Author Share Posted December 19, 2008 How would i make that as a dropdown option with 3 values though if i did this woulnt it fix it to 1? .....customerRef FROM test ORDER BY price Quote Link to comment https://forums.phpfreaks.com/topic/137686-solved-order-by-drop-down/#findComment-719624 Share on other sites More sharing options...
RussellReal Posted December 19, 2008 Share Posted December 19, 2008 ORDER BY `price` will just order by price, it won't select 1 or the other, use the LIMIT clause to specify how many results you want Quote Link to comment https://forums.phpfreaks.com/topic/137686-solved-order-by-drop-down/#findComment-719634 Share on other sites More sharing options...
anthony-needs-you Posted December 19, 2008 Author Share Posted December 19, 2008 yeah i've already used the LIMIT in the code at the top, what I need some help on is how to change the results so that people can order them by destination, price, duration. My thinking is to use a drop down menu where people can select the order they want, but how would i intergrate this into the above code? Quote Link to comment https://forums.phpfreaks.com/topic/137686-solved-order-by-drop-down/#findComment-719667 Share on other sites More sharing options...
premiso Posted December 19, 2008 Share Posted December 19, 2008 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`. Quote Link to comment https://forums.phpfreaks.com/topic/137686-solved-order-by-drop-down/#findComment-719686 Share on other sites More sharing options...
anthony-needs-you Posted December 19, 2008 Author Share Posted December 19, 2008 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); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/137686-solved-order-by-drop-down/#findComment-719699 Share on other sites More sharing options...
redarrow Posted December 19, 2008 Share Posted December 19, 2008 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> Quote Link to comment https://forums.phpfreaks.com/topic/137686-solved-order-by-drop-down/#findComment-719791 Share on other sites More sharing options...
anthony-needs-you Posted December 19, 2008 Author Share Posted December 19, 2008 blinding works perfectly, thanks a lot. Best Wishes Quote Link to comment https://forums.phpfreaks.com/topic/137686-solved-order-by-drop-down/#findComment-719842 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.