pappakaka Posted February 19, 2011 Share Posted February 19, 2011 Ok i'm trying to make a dropdown menu to sort content on my site. The dropdown looks like this: <select> <option selected="selected">Date</option> <option>Clicks</option> <option>Ratings</option> <option>Comments</option> </select> The content that the dropdown needs to sort are taken from my MySQL database and put on the site using these lines of code: (there is alot more code to do other stuff aswell, but that isn't importand for my problem) $sql = "SELECT * FROM links LIMIT $offset, $rowsperpage"; $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); $dyn_table = '<table class="main_table">'; while ($row = mysql_fetch_assoc($result)) { etc... The dropdown and the php script that inserts the content on the page are (and needs to be) in two seperate files. So how can i make so that when a user selects "Sort by: Clicks" for example, the page refresh and maybe inserts "ORDER BY clicks ASC" into "$sql = "SELECT * FROM links LIMIT $offset, $rowsperpage";"or something?? I'm really open for ideas here, Javascript and jQuery is also welcome if it needs to be used! Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted February 19, 2011 Share Posted February 19, 2011 If that's the actual code, your <select> has no name= attribute, and your <options> have no value= attribute. You'll need to take care of that before this even has a chance of working. Quote Link to comment Share on other sites More sharing options...
pappakaka Posted February 19, 2011 Author Share Posted February 19, 2011 Ok, so if i use this instead, what should i do? <select name="sortby_dropdown"> <option value="date" selected="selected">Date</option> <option value="clicks">Clicks</option> <option value="ratings">Ratings</option> <option value="comments">Comments</option> </select> Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted February 19, 2011 Share Posted February 19, 2011 Assuming that each value is actually the name of a field in the table, you can add an ORDER BY clause to the query string using the value of $_POST['sortby_dropdown'] as the value. Validate it first; even <select> field data is subject to user manipulation. Make sense? $order_values = array( 'date', 'clicks', 'ratings', 'comments'); // valid values for the <select> field if( in_array($_POST['sortby_dropdown'], $order_values) ) { // make sure the value is valid $order = "ORDER BY {$_POST['sortby_dropdown']}"; } else { $order = ''; } $sql = "SELECT * FROM links $order LIMIT $offset, $rowsperpage"; Quote Link to comment Share on other sites More sharing options...
pappakaka Posted February 20, 2011 Author Share Posted February 20, 2011 Ok, now i've tried to get that to work for a while but something is wrong cause it isn't working. I used your code, and yes the values in the form (date, clicks, ratings, comments) has the same names as the colomns/fields in my table so that can't be the problem. I've treid to test it in a "test.php" file with only the form and code in the same file with no succsess. Could the problem be that the page doesn't automaticly refresh and order the content in the page correspondly when a user selects a value from the dropdown? Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted February 20, 2011 Share Posted February 20, 2011 You need to provide more information. What's the form method? Is this being used in conjunction with pagination? Etc. Quote Link to comment Share on other sites More sharing options...
pappakaka Posted February 20, 2011 Author Share Posted February 20, 2011 I use: <form action="all_links.php" method="post"> And yes the whole website use on pagination! Quote Link to comment 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.