0

I am trying to allow users to sort mysql queries based on name, price, etc. This drop down gives them the power to do that, it changes the "ORDER BY" clause based on what the user chooses. It's 3am here and I just can't spot the mistake:

    <?php
        $sortBy = $_POST['sortBy'];
        if ($sortBy) {

            $priceLowToHigh = $_POST['price-low-to-high'];
            $priceHighToLow = $_POST['price-high-to-low'];
            $dateMostRecent = $_POST['date-most-recent'];
            $dateOldest = $_POST['date-oldest'];
            $alphabeticalOrder = $_POST['alphabetical-order'];

            if ($priceLowToHigh) {
                $sortOrder = "price ASC";
            } elseif ($priceHighToLow) {
                $sortOrder = "price DESC";
            } elseif ($dateMostRecent) {
                $sortOrder = "date DESC";
            } elseif ($dateOldest) {
                $sortOrder = "date ASC";
            } elseif ($alphabeticalOrder) {
                $sortOrder = "name ASC";
            }

        } else {
            $sortOrder = "date DESC";
        }
    ?>
    <form action="" method="post">
        <select name="sortBy" onchange="this.form.submit()">
            <option>Sort By</option>
            <option value="price-low-to-high">Price (low to high)</option>
            <option value="price-high-to-low">Price (high to low)</option>
            <option value="date-most-recent">Date (most recent)</option>
            <option value="date-oldest">Date (oldest)</option>
            <option value="alphabetical-order">Alphabetical Order</option>
        </select>
    </form>

later on the ORDER BY clause is used like this:

    $query = mysql_query("SELECT * FROM products WHERE category = $categoryId ORDER BY $sortOrder");
2
  • Shouldn't the form tag have a value for the action? Like the name of the current page... Where to POST to? Jul 3, 2010 at 6:13
  • 2
    Not necessarily as the default will be the current page.
    – spinon
    Jul 3, 2010 at 6:14

4 Answers 4

4

You're handling your menu incorrectly. Your $sortBy variable will already be what the user selects, not separate $_POST fields.

This is what you should do instead -

<?php
    $sortBy = $_POST['sortBy'];
    if($sortBy == 'price-low-to-high') {
        $sortOrder = "price ASC";
    } else if($sortBy == 'price-high-to-low') {
        $sortOrder = "price DESC";
    } else if($sortBy == 'date-most-recent') {
        $sortOrder = "date DESC";
    } else if($sortBy == 'date-oldest') {
        $sortOrder = "date ASC";
    } else if($sortBy == 'alphabetical-order') {
        $sortOrder = "name ASC";
    } else {
        $sortOrder = "date DESC";
    }
?>
4
  • Actually he is not using sort by except for detecting whether they are choosing to perform a custom sort. You can see after that that he is checking the individual post fields to see which one is being used.
    – spinon
    Jul 3, 2010 at 6:16
  • The code above will default to not being used if the options aren't selected, like the original code he posted. Except the one I posted works =) Jul 3, 2010 at 6:19
  • Sorry I guess I am wrong. I was making an assumption that there was another field that was getting populated named those names. But I think you are in fact right.
    – spinon
    Jul 3, 2010 at 6:19
  • Can't believe I missed that, coding while half asleep leads to silly mistakes. Thanks.
    – James
    Jul 3, 2010 at 6:26
1

I can't comment on your answer Raphael because I'm not high enough, but I just want to mention possibly using a switch statement for readability. But you are spot on there. Upvote

0

It seems your code is in double quotes when it should be in single to have php do the replace of your code. You should printing that sql to the screen and you will see that it is likely not replacing the value.

I am referring to the line where you make the mysql_query call.

0

Your basic code is a little bit wrong. It should be the following:-

<?php
$sortBy = $_POST['sortBy'];

if($sortBy == 'price-low-to-high')
  $sortOrder = "price ASC";
else if($sortBy == 'price-high-to-low')
  $sortOrder = "price DESC";
else if($sortBy == 'date-most-recent')
  $sortOrder = "date DESC";
else if($sortBy == 'date-oldest')
  $sortOrder = "date ASC";
else if($sortBy == 'alphabetical-order')
  $sortOrder = "name ASC";
else
  $sortOrder = "date DESC";
?>

<form action="" method="post">
        <select name="sortBy" onchange="this.form.submit()">
            <option>Sort By</option>
            <option value="price-low-to-high">Price (low to high)</option>
            <option value="price-high-to-low">Price (high to low)</option>
            <option value="date-most-recent">Date (most recent)</option>
            <option value="date-oldest">Date (oldest)</option>
            <option value="alphabetical-order">Alphabetical Order</option>
        </select>
</form>  

I think this will work now, if you change your code to the above code.

Hope it helps.

Not the answer you're looking for? Browse other questions tagged or ask your own question.