tags:

views:

52

answers:

4

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");
A: 

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.

spinon
+3  A: 

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";
    }
?>
Raphael Caixeta
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
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 =)
Raphael Caixeta
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
Can't believe I missed that, coding while half asleep leads to silly mistakes. Thanks.
James
A: 

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.

Knowledge Craving
+1  A: 

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

phoffer