0

I am generating a list of cuisines like this:

$lists='';
$stmt="SELECT cuisine_id, cuisine_name, cuisine_name_trans FROM db_cuisine";

if ( $res=$DbExt->rst($stmt)){
    if ($list){
        foreach ($res as $val) {    

            $cuisine_json['cuisine_name_trans']=!empty($val['cuisine_name_trans'])?
            json_decode($val['cuisine_name_trans'],true):'';

            $lists[$val['cuisine_id']]="".qTranslate($val['cuisine_name'],'cuisine_name',$cuisine_json);
        }
        return $lists;
    }
    return $res;
}       
return false;

The list is then returned as:

1: "American"
5: "Sandwiches"
6: "Barbeque"
8: "Italian"
9: "Mexican"
10: "Sushi"
11: "Burgers"
13: "Japanese"

(The IDs are according to the database ID). I am trying to sort them now descending by name but I can't seem to get it done with sort as it needs a key name. How do I do that?

6
  • What are $list, $cuisine_json and $lists? Where are they defined? What does qTranslate() do? Commented Mar 6, 2019 at 5:48
  • Sorry, I forgot to include that. $list is just a true/false. $lists is defined with $lists=''; and $cuisine_json comes from cuisine_name_trans. Commented Mar 6, 2019 at 5:50
  • 2
    This question need significant improvement if you are to get it to a state where it contains a MCVE. Commented Mar 6, 2019 at 5:51
  • instead of converting the row data to output string directly, store it as array key=>value first. after the loop is exited, do an asort() and then json_encode() to convert it as json string. Commented Mar 6, 2019 at 5:51
  • If $lists is a string, you're going to run in to problems with that $lists[$val['cuisine_id']] bit. I'd recommend an array instead, ie $lists = []; Commented Mar 6, 2019 at 5:51

2 Answers 2

2

Do Sorting with sql instead of php

$stmt="SELECT cuisine_id, cuisine_name, cuisine_name_trans FROM db_cuisine WHERE 1 ORDER BY cuisine_name DESC ";
Sign up to request clarification or add additional context in comments.

5 Comments

I tried that but somehow it's not working. As to why, that's beyond me. So I'm trying to get it done with PHP...
And since I use translations after retrieving it, the sorting may differ.
in your code you are sorting the array before translate.So i think its not affect the translate function
And if($list) is always false.So its not passing to the statment
Looks like I will need to rewrite the whole code there and take a different approach. Thanks a lot for your help :)
0

You can use PHP's arsort function. It sorts an array by value in the descending order. Your final code should be like:

if ( $res=$DbExt->rst($stmt)){
    if ($list){
        foreach ($res as $val) {    

            $cuisine_json['cuisine_name_trans']=!empty($val['cuisine_name_trans'])?
            json_decode($val['cuisine_name_trans'],true):'';

            $lists[$val['cuisine_id']]="".qTranslate($val['cuisine_name'],'cuisine_name',$cuisine_json);
        }
        arsort($lists);
        return $lists;
    }
    return $res;
}       
return false;

I also recommend to check this page to get more info about the PHP array sort functions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.