PHP数字降序数据库


PHP numerical descending in database?

我有一个table person。

id, name    product_A  product_B
--  -----   ---------  --------- 
1   Joe        1         4        // result product A + Product B = 5
2   Leo        5         1        // result product A + Product B = 6
3   Lia        2         2        // result product A + Product B = 4

首先,我需要计算产品A +产品b。

function count($a,$b){
$total = $a+$b;
return $total;
}

然后调用数据库中的value

    $count_row = mysql_query("SELECT * FROM person ");
    $result_row= mysql_num_rows($count_row);
$i=0;
$j=0;
$k=0;
    $query = "SELECT * FROM person "
    $result = mysql_query($query) or die ("Query error: " . mysql_error());
    while($row = mysql_fetch_array($result))
        {
          $array_a[$i++]=$row['product_A'];
          $array_b[$j++]=$row['product_B'];
          $array_name[$j++]=$row['name'];
        }
for ($i = 0 ; $i < $count_row ; $i++){
$tot[i] = count($array_a,$array_a);
$array_name[i];
}

//排序

   function desc($result_row){
    $array = array();
    global $tot;
    for ($n = 0 ; $n <$result_row ; $n++){
        array_push($array,$tot[$n]);
    }
    for($i = 0 ; $i < sizeof($array) ; $i++)
        rsort($array);
                for ($n = 0 ; $n <$result_row ; $n++){
                echo $array_name[$n]."<br>";
                            echo $array[$n]."<br>";
                }
            }
   desc($result_row); 

然后我要根据产品A +产品B的最大值或降序来回呼名称。"输出= Leo, Joe, Lia"。我如何在PHP代码中做到这一点?

你可以这样试试

SELECT  (tp.product_A + tp.product_B) AS total,tp.name
FROM table_person tp
ORDER BY total DESC

最简单的方法是使用人们为您编写的建议SQL。在php中这样做不适合您的情况。不如使用SQL,这样可以节省一些代码编写和服务器上的内存。

的例子:在SQL:

SELECT *, product_A  + product_B AS product_sum FROM persons ORDER BY product_sum DESC

然后在PHP中:

while($record = mysql_fetch_assoc($result_set))
{
    // Results Already will be sorted for you you just have to output them.
    echo $record['name'].' has '.$record['product_sum'].' product sum.';
    echo '<br/>'; 
}

现在您可以输出名称或产品计数或总数或任何您想要的。

Try this:

<?php
//product array ($products is the array fetched from the db)
$products = array();
$products[0] = array('id'=>1, 'name'=>'Joe', 'productA'=>1, 'productB'=>4);
$products[1] = array('id'=>2, 'name'=>'Leo', 'productA'=>5, 'productB'=>1);
$products[2] = array('id'=>3, 'name'=>'Lia', 'productA'=>2, 'productB'=>2);
//Create a single-column array with sums
$productSum = array();
foreach ($products as $product) {
    $productSum[] = (int)$product['productA'] + (int)$product['productB'];
}
//Do the magic, sort DESCENDING by $productSum-array into $products-array
array_multisort($productSum, SORT_DESC, $products);
//Output
print_r($products);
?>
输出:

Array (
[0] => Array ( [id] => 2 [name] => Leo [productA] => 5 [productB] => 1 )
[1] => Array ( [id] => 1 [name] => Joe [productA] => 1 [productB] => 4 )
[2] => Array ( [id] => 3 [name] => Lia [productA] => 2 [productB] => 2 )
)

有关array_multisort()的更多信息,请查看:http://php.net/manual/en/function.array-multisort.php

为解决您的问题,请尝试执行以下查询。

select *,product_A + product_B as total from person order by total desc