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
Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
Rio Eduardo
  • 233
  • 2
  • 4
  • 8

2 Answers2

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.

Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
  • but the total_item is not in mysql – Rio Eduardo Jun 27 '13 at 06:17
  • Where is it from? I saw you said it was in `COUNT(\`id\`)`, but then you deleted that comment. Is that correct? – Danny Beckett Jun 27 '13 at 06:18
  • 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 – Rio Eduardo Jun 27 '13 at 06:26
  • If you edit your question to contain your full query, I can give you the exact query you could use. – Danny Beckett Jun 27 '13 at 06:27
  • 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 – Rio Eduardo Jun 27 '13 at 06:32
  • The thing is, you're putting your server under much more stress by doing this in PHP. By doing it in SQL, you get unparalleled performance. – Danny Beckett Jun 27 '13 at 06:34
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
JunM
  • 7,040
  • 7
  • 37
  • 58
  • 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" ); – Rio Eduardo Jun 27 '13 at 06:19
  • 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 Jun 27 '13 at 06:27
  • @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. – JunM Jun 27 '13 at 06:29
  • I forgot that I have another atribute except name that is number serial, so how can i sort the number serial too? – Rio Eduardo Jun 27 '13 at 07:38
  • how can i sort the number serial too together with name and total? Thanks – Rio Eduardo Jun 27 '13 at 07:44
  • @Rio Eduardo I can think only of creating another array for serial number. In this case, you need the `names` as key of your array of serial number similar to an array for `total`. So, you will have the `names` as keys for both serial number and total. Then you can manipulate your arrays to your desired output. – JunM Jun 27 '13 at 08:26