0

Descending sort type does not work with this syntax, please help.

<?php

require_once("../connection/conn.php");

$compare = $_GET["sortfield"];

if(isset($_GET["sortfield"])){
    if(isset($_GET["sorttype"]) == "asc"){  
        if($compare == "gender"){

            $holdData = $conn->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 "<br>";
    echo json_encode(["Message"=>"Please select the sortfield and sort type!"]);
}
4
  • 1
    descending sort type does not function with this syntax Unclear... what you mean? the query with DESC ordering option is sent to MySQL (how was this checked?) but the output sorting do not match? – Akina Feb 9 '20 at 15:14
  • I would suggest that you build the query string first, then prepare and execute that in one place. Since this question is more related to php than database administration, you may get more answers in stackoverflow. – Lennart Feb 9 '20 at 17:40
  • 1
    Please show us SQL code and not PHP! Many DBAs have been programmers, many have not - of those of us who have been, many have never used PHP (nor want to - i.e. me! :-) ). But seriously, try and do what you want through the mysql CLI (or MySQL Workbench) - if it works there, then the problem lies with your PHP code and should be posted on StackOverflow - if it doesn't work via the CLI then it's an SQL problem and is on-topic here. p.s. welcome to the forum! :-) – Vérace Feb 9 '20 at 20:57
  • Please provide a brief set of input rows and show us both the "wrong" output and the desired output. – Rick James Feb 13 '20 at 22:39
0

Welcome.

Please notice that you are in a wrong place.

dba - here you can give your mysql code alone and ask for help.

You might have asked this question in stackoverflow.

Anyway here is what you miss.

if(isset($_GET["sortfield"])){

is right. But

if(isset($_GET["sorttype"]) == "asc"){  

is not right as the expression to the left of equal condition operator would evaluate to true or false and if you check the logical value == "asc" it will fail. It should be

if(isset($_GET["sorttype"]) and $_GET["sorttype"]== "asc"){  

or simply

if($_GET["sorttype"]== "asc"){  

if you are sure that it is already set (as of the first if).

Also, please go through this before asking. https://stackoverflow.com/help/minimal-reproducible-example

If you had gone through the mentioned page, formatted your question clearly and asked at the right place, you would have got your answer on day one itself for sure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.