开发者

What's wrong with my Code? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 11 years ago.

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'];
            $alph开发者_Go百科abeticalOrder = $_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");


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


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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜