Search code examples
phpsuperglobals

PHP if-then-else statement not working


My url is something such as: "inventory.php?sorting=1" and so forth. Page loads fine but does not display the information properly.

mysql_connect("localhost","user","pass"); 
mysql_select_db("database"); 

if ($sorting == 1){
$result = mysql_query("select * from vehicles ORDER BY year DSC");
}
elseif ($sorting == 2){
$result = mysql_query("select * from vehicles ORDER BY make DSC");
}
elseif ($sorting == 3){
$result = mysql_query("select * from vehicles ORDER BY miles DSC");
}
elseif ($sorting == 4){
$result = mysql_query("select * from vehicles ORDER BY downpay DSC");
}
elseif ($sorting == 5){
$result = mysql_query("select * from vehicles ORDER BY pricepay DSC");
}
elseif ($sorting == 6){
$result = mysql_query("select * from vehicles ORDER BY pricecash DSC");
}
else {
$result = mysql_query("select * from vehicles");
}

while($r=mysql_fetch_array($result))

Solution

  • You need to replace $sorting with $_GET["sorting"]

    but, also:

    Would it not be a better idea to use the switch statement?

    switch($_GET["sorting"]{
        case 1:
        $result = mysql_query("select * from vehicles ORDER BY year DSC");
        break;
    case 2:
    

    etc.