Jump to content

Problem with "Sort by" dropdown menu?


pappakaka

Recommended Posts

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!

 

Link to comment
Share on other sites

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>

 

Link to comment
Share on other sites

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";

Link to comment
Share on other sites

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?

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.