What's wrong with my Code? [closed]
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.
精彩评论