0

I have data like this in mysql sort by id ASC:

NOTE: total is not in mysql, total is from price * total_item -> for example

     id       name         total
    ----   -----------   ----------
     1        item1          3
     2        item2          5
     3        item3          1
     4        item4          2
     5        item5          4

and I want to sort it in php

first, I sort the total to get the highest total in first place

//insert total into list
for($i=0;$i<5;$i++){
  $total_list[] = $total;
  $b = $total_list;
  rsort($b);

  //display total from highest to lowest
  echo $b[$i];
}

the result will be like this:

         id       name         total
        ----   -----------   ----------
         1        item1          5
         2        item2          4
         3        item3          3
         4        item4          2
         5        item5          1

ok, I already got the total sorted according to my code above

so to get the name sorted too, I have to sort it but I already tried the same way as I sorted the total but the result is different

nah, I want the result is like this

         id       name         total
        ----   -----------   ----------
         1        item2          5
         2        item5          4
         3        item1          3
         4        item4          2
         5        item3          1
6
  • 3
    Why don't you do the sort in your mysql select? Like: select * from table name order by total, name Commented Jun 27, 2013 at 5:57
  • @deceze - no I think, it's not duplicate Commented Jun 27, 2013 at 5:58
  • @bitfiddler - no, because total is not in mysql Commented Jun 27, 2013 at 5:59
  • Where does the total column come from? Commented Jun 27, 2013 at 6:00
  • 3
    If you're calculating total in your query, you can sort by it. Why don't you post your query? Commented Jun 27, 2013 at 6:03

2 Answers 2

1

This can (and should) all be done from the SQL query; something like:

SELECT `id`, `name`, `price` * `total_item` AS `total`
FROM `mytable`
ORDER BY
  `price` * `total_item` DESC,
  `id` ASC

When ORDERing, you can't use the name given in AS, but you can order on the same calculation.

Sign up to request clarification or add additional context in comments.

6 Comments

but the total_item is not in mysql
Where is it from? I saw you said it was in COUNT(`id`), but then you deleted that comment. Is that correct?
yeah count(id), but total_item is not in mysql, it's from sum of $total_demand and number of demand is in table demand. and I have 3044 record in table item and 60.000 record in table demand, so if i use join table, it's impossible, so i prefer array in php not in mysql
If you edit your question to contain your full query, I can give you the exact query you could use.
can you help in array php not query? because I am already half way to finish it and from the start i dont use function sum, average in mysql, I purely use logic math like $a += $a in php to get the sum of $a
|
0

I tried something like this: using asort

$data = array(
"item1"=>"1",
"item2"=>"2",
"item3"=>"5",
"item4"=>"3",
"item5"=>"4"
);

asort($data);

print_r($data);

Resut: Array ( [item1] => 1 [item2] => 2 [item4] => 3 [item5] => 4 [item3] => 5 )

In order to obtain the highest total value and name, get the last element on the array. Though you can manipulate the names and keys by using foreach.

If we add foreach like this:

foreach($data as $key=>$val){
    echo "Name:".$key."  Total:".$val."<br>";
}

Result:

Name:item1 Total:1
Name:item2 Total:2
Name:item4 Total:3
Name:item5 Total:4
Name:item3 Total:5

6 Comments

so how to make the name and the total in the same array like this: $data = array( "item1"=>"1", "item2"=>"2", "item3"=>"5", "item4"=>"3", "item5"=>"4" );
no, that's not I want to ask, I want to ask how to get the name and the total together in array?
@Rio Eduardo Sorry I just updated my answer, you can create an array like that: example, $data[$name]=$total. You may study first on php arrays.
I forgot that I have another atribute except name that is number serial, so how can i sort the number serial too?
how can i sort the number serial too together with name and total? Thanks
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.