Sample Header Ad - 728x90

ORDER BY with IF-ELSE statement (DESC doesn't work)

0 votes
1 answer
157 views
Descending sort type does not work with this syntax, please help. prepare("SELECT * FROM informations ORDER BY gender ASC, first_name ASC"); $holdData->execute(); $getData = $holdData->fetchAll(PDO::FETCH_ASSOC); $newData = json_encode($getData);; print_r($newData); }elseif ($compare == "last_name") { $holdData = $conn->prepare("SELECT * FROM informations ORDER BY last_name ASC"); $holdData->execute(); $getData = $holdData->fetchAll(PDO::FETCH_ASSOC); $newData = json_encode($getData);; print_r($newData); }elseif ($compare == "first_name") { $holdData = $conn->prepare("SELECT * FROM informations ORDER BY first_name ASC"); $holdData->execute(); $getData = $holdData->fetchAll(PDO::FETCH_ASSOC); $newData = json_encode($getData);; print_r($newData); }elseif ($compare == "timestamp") { $holdData = $conn->prepare("SELECT * FROM informations ORDER BY timestamp ASC"); $holdData->execute(); $getData = $holdData->fetchAll(PDO::FETCH_ASSOC); $newData = json_encode($getData);; print_r($newData); }else{ } }elseif(isset($_GET["sorttype"]) == "desc"){ if($compare == "gender"){ $holdData = $conn->prepare("SELECT * FROM informations ORDER BY gender DESC, first_name DESC"); $holdData->execute(); $getData = $holdData->fetchAll(PDO::FETCH_ASSOC); $newData = json_encode($getData);; print_r($newData); }elseif ($compare == "last_name") { $holdData = $conn->prepare("SELECT * FROM informations ORDER BY last_name DESC"); $holdData->execute(); $getData = $holdData->fetchAll(PDO::FETCH_ASSOC); $newData = json_encode($getData);; print_r($newData); }elseif ($compare == "first_name") { $holdData = $conn->prepare("SELECT * FROM informations ORDER BY first_name DESC"); $holdData->execute(); $getData = $holdData->fetchAll(PDO::FETCH_ASSOC); $newData = json_encode($getData);; print_r($newData); }elseif ($compare == "timestamp") { $holdData = $conn->prepare("SELECT * FROM informations ORDER BY timestamp DESC"); $holdData->execute(); $getData = $holdData->fetchAll(PDO::FETCH_ASSOC); $newData = json_encode($getData);; print_r($newData); }else{ } }else{ } }else{ echo json_encode(["Message"=>"No Sortfield and Sort type found!"]); echo "
"; echo json_encode(["Message"=>"Please select the sortfield and sort type!"]); }
Asked by Ellie Chsya (1 rep)
Feb 9, 2020, 09:33 AM
Last activity: Jul 16, 2025, 04:05 PM