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
Thanks for the answer, a question, if delete the record and register with a new auto increment, you have to change the SELECT again?
– ElvisP
@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.
– Kaique Garcia
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
– ElvisP
see my @Eliseub edition. this case test can serve any parameter you want
– Kaique Garcia
Perfect, thank you for the collaboration, gratitude!
– ElvisP
@Bacco I think not, because Kiko’s help encouraged me to study about CASE, WHEN and THEN, of which I was not aware.
– ElvisP
Unnecessary complication, just one
ORDER BY cat_nome = "Outros", cat_nome,....
- see working here: http://sqlfiddle.com/#! 9/3a865dd/1– Bacco
@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.
– Bacco
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.
– ElvisP
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
– Bacco
Thanks for the help.
– ElvisP
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.
– Kaique Garcia