This doesn’t make sense.. I’m trying to sort posts according to the value of a URL parameter but my elseif statement isn’t working.
This is a function that adds another WHERE clause to the query. There are no MYSQL errors I’m just having statement trouble.
function sort_where($where)
{
if (isset($_GET['sort'])) {
$sort = $_GET['sort'];
if ($sort = "up") {
$where .= " AND $sort > 1";
}
elseif ($sort = "down") {
$where .= " AND $sort > 1";
}
}
return $where;
}
The query eventually looks like this
$query = "SELECT * FROM posts WHERE something = $something AND $sort > 1";
The if statement works, the elseif is ignored. I get posts with up > 1 regardless or vice-versa if $sort = down in the if statement.
Actually, neither of the two inner ifs are working correctly.
You need to use
==not=. One equals sign means assignment, and if you assign to a truthy value it always evaluates to true in an if condition. That’s why your elseif appears to never happen.You may also need to fix your
WHEREclauses, they don’t make sense to me (you’re sorting but comparing to anupcolumn and adowncolumn?). Or maybe that’s how you designed your table…Based on your comments, try the following SQL
WHEREclauses and see if you get the correct posts: