Jump to content

Drop Down menu with php


hyeteck

Recommended Posts

hey guys, i have this php code that has a drop down menu that sorts the products on a website by name and price.

 

<br /><form method="POST" action="">
<?php
if(@$_SESSION['sortby'] != '') $thesortorder=(int)$_SESSION['sortby'];
if(@$_POST['sortby']!='') $thesortorder=(int)$_POST['sortby'];
?>
<select name="sortby" size="1">
<option value="1"<?php if(@$thesortorder==1) print ' selected'?>>Sort by product name</option>
<option value="3"<?php if(@$thesortorder==3) print ' selected'?>>Sort by product price (ascending)</option>
<option value="4"<?php if(@$thesortorder==4) print ' selected'?>>Sort by product price (descending)</option>
</select>
<input type="submit" value="Go">
</form><br />

 

instead of a dropdown menu, i want it to look something like this, where Name, Price (ascending), and price(descending) are links.

 

Sort By: Name | Price (ascending) | Price (descending)

 

how can i do that?

thanks

Link to comment
https://forums.phpfreaks.com/topic/41245-drop-down-menu-with-php/
Share on other sites

is there a reason to use "get" instead of "post" ?

 

you'd use the 'get' method because you will be using links. what you do, is in the a href="" you add some parameters for the sql query to order by. for instance in benjamin's post you'd use something like...

 

<?php
        if($_GET['thesortorder'] == 1){
                $order = "ASC";
        }
        if($_GET['thesortorder'] == 4){
                $oder = "DESC";
        }
        if(!isset($_GET['thesortorder'])){
                $order = "DESC";
        }

        $sql = "SELECT * FROM your_table ORDER BY ". $order ."";
?>

 

if you were to use the 'post' method, you wouldn't be able to use links. you'd have to use forms, which is too much of a hassle when 'get' works this easily. at least, that's what i think benjamin meant by his post.

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.