Change the order of a result in PHP WHILE from Mysql

Asked

Viewed 100 times

0

I am with the following difficulty, in a table are stored records of product categories, I made the connection mysqli, SELECT and WHILE, and everything is fine so far, but what I’m having difficulty is to leave a record of WHILE last, in case it would be the category record called "OTHERS", because the SELECT is with ORDER BY cat_nome ASC; and below "OTHERS" there are other categories with names beginning with "P", "Q", "R", "S" (...).

Summarizing the SELECT, it’s like this:

<?php
$sql_cat_cad = "SELECT 
                   `cat_id`,
                   `cat_nome`
                FROM 
                    `categoria` 
                WHERE 
                    `cat_id_pai` IS NULL
                ORDER BY 
                    `cat_nome`
                        ASC
                ";

$query_cat_cad = mysqli_query( $mysqli, $sql_cat_cad );
$conta_cat_cad = mysqli_num_rows( $query_cat_cad );
if( $conta_cat_cad > 0 ) {
    echo "<select name='categorias' id='categorias'>\n";
    echo "<option value=''>Selecione a categoria</option>\n";
    while( $cat = mysqli_fetch_array( $query_cat_cad ) ) {
        echo "<option value='" . $cat['cat_id'] . "'>" . stripslashes( $cat['cat_nome'] ) . "</option>\n";
    }
    echo "</select>\n";
}
?>

Briefly resulting in WHILE:

Adapters

Supports for Forearm

Speakers and Subwoofer

Microphones

Mouse Pads

Other

Scanners

CPU supports

USB Padlocks

I’d like to change the code so it stays that way:

Adapters

Supports for Forearm

Speakers and Subwoofer

Microphones

Mouse Pads

Scanners

CPU supports

USB Padlocks

Other

1 answer

2


First you must get the cat_id relating to cat_nome "Others". This way you can make an ordering with case test:

SELECT cat_id, cat_nome FROM categoria WHERE cat_id_pai IS NULL
ORDER BY (CASE WHEN `cat_id`=<ID_QUE_ENCONTROU> THEN 1 ELSE 0 END) ASC, cat_nome ASC

Reference: https://www.w3schools.com/sql/func_mysql_case.asp

EDIT

Dynamically ordering by the term:

SELECT `cat_id`, `cat_nome` FROM `categoria` WHERE `cat_id_pai` IS NULL
ORDER BY (CASE WHEN `cat_nome`="Outros" THEN 1 ELSE 0 END) ASC, `cat_nome` ASC
  • Thanks for the answer, a question, if delete the record and register with a new auto increment, you have to change the SELECT again?

  • @Eliseub. Yes, unless you do a word search to get that ID dynamically before calling that query. If you accept a suggestion, the term "Others" (in order to "Other options") is not usually used in this way. For example, there may be situations (in the future) where there are so many categories that you might want to summarize some with the generic option "Others". If this is not the case, I advise you to make this search before the call.

  • 1

    With a subquery in the same sentence you must solve this then to receive the id of the record, right? I will take your suggestion into consideration, but for future implementations, because in this case there are several parent categories

  • see my @Eliseub edition. this case test can serve any parameter you want

  • Perfect, thank you for the collaboration, gratitude!

  • @Bacco I think not, because Kiko’s help encouraged me to study about CASE, WHEN and THEN, of which I was not aware.

  • 2

    Unnecessary complication, just one ORDER BY cat_nome = "Outros", cat_nome,.... - see working here: http://sqlfiddle.com/#! 9/3a865dd/1

  • 1

    @Eliseub. That would be like you saying you learned how to use a hammer to screw the wall, but if you did it right, that’s fine. For the rest of the readers, I preferred to leave the direct solution even, then each one chooses the one that it finds best. I only suggest in these cases search better on the site, because it is the second post of yours that I see in a few minutes that is duplicate.

  • Thanks for your point of view, I marked as valid because for me it worked, could add an answer explaining so I could put yours as the best answer,, gratitude.

  • 1

    In the link above you already have answers with the example I gave, I just repeated in the comments to help you and Kiko himself to simplify in everyday use. As for the answer, if it was the one you solved, you’re right to mark as accepted. Note that I am not saying that CASE does not work (until pq query Planner gives a simplified internally), I just think it’s nice to make it clear that it has how to make it much simpler

  • Thanks for the help.

  • I was so focused on the problem of sorting a specific element that I even forgot about sorting by the term. I showed CASE because it serves much more, especially if there are other built-in ordering cases. Calm down, young people.

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.