2016-07-01 55 views
-1

我正在使用PHP来计算来自MySQL的值的总和,并且能够获得最高值$total的值。如何使用PHP获取整行使用max()使用PHP

我的SQL表是

USER 1 
crit_1 = 75 
crit_2 = 75 
USER 2 
crit_1 = 100 
crit_2 = 100 

我的PHP是这样的:

$crit_1 = $row["crit_1"]; 
$crit_2 = $row["crit_2"]; 
$sum = ($crit_1 + $crit_2); 
$total = number_format ($sum/2, 2, '.',' '); 
$totals[] = $total; 
$value = max($totals); 

,这里是我的HTML

<th><strong>Score from Crit_1</strong></th> 
<th><strong>Score from Crit_2</strong></th> 
<th><strong>TOTAL SCORE OF crit_1 and crit_2</strong></th> 

<td align="center"><?php echo $crit_1; ?></td> 
<td align="center"><?php echo $crit_2; ?></td> 
<td align="center"><?php echo $total; ?></td> 

和呼应

<?php echo $value; ?> 

这只能反映数字。我想要做的是如何获得最高价值的整个行数(从crit_1到total)。

+1

可怕的db结构规范化,使旅游生活更容易一百万次 –

回答

0

原因$value = max($totals);回显相同的值是,在您的代码示例中,您只给$ totals数组赋值1。

$totals = array(); 
$totals[] =$row['crit1']; 
$totals[] = $row['crit2']; 
$value = max($totals); 

应该给你总数组中的最大值。然后你需要用它做点什么...

不知道你为什么要除以2?平均? $total = number_format ($sum/2, 2, '.',' '); ???

参考文档为MAX() http://php.net/manual/en/function.max.php

我也建议看MySQL的SUM和GROUPBY功能 http://www.w3schools.com/sql/sql_func_sum.asp http://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html

0

下面的SQL会给你最一共只有该行:

SELECT (u.crit_1 + u.crit_2)/2 AS total, max((u.crit_1 + crit_2)/2), u.* FROM users AS u; 

但是,如果您想要获取总计的所有行,您可以执行以下操作:

SELECT (u.crit_1 + u.crit_2)/2 AS total, u.* FROM users AS u 

但是你不能在这里得到最大值。但是在你的示例代码中,你已经在其他地方显示了表中的所有数据和最大值。因此,以相同的方式,您可以循环显示表格中的所有数据,并且当时您可以检查最大总数。

$value = -1;  // outside of your loop 
$value = max($value, $total); //inside your loop 

注意:更改users与您的实际表名称。

0

我假设你有你的数据库结构像

user_id , crit_1, crit_2 in which user_id is unique field, or whatever you have as unique field for each user. 

然后添加字段在$ max_total阵列状

<?php 
    $max_total = array(); 
    $conn = mysqli_connect($servername, $username, $password) or die ("Connection failed: " . mysqli_connect_error()); 
    mysqli_select_db($conn,$mysql_database) or die("Opps some thing went wrong"); 
    $result = mysqli_query($conn, "select * from reg")or die("error"); 
    echo '<table border="1">'; 
    echo '<tr><th><strong>Score from Crit_1</strong></th> 
    <th><strong>Score from Crit_2</strong></th> 
    <th><strong>TOTAL SCORE OF crit_1 and crit_2</strong></th></tr>'; 
    while($row = mysqli_fetch_assoc($result)){ 
     $max_total[$row['user_id']] = $row['crit_1'] + $row['crit_2']; 
     echo '<tr><td align="center">'.$row['crit_1'].'</td><td align="center">'.$row['crit_2'].'</td><td align="center">'.($row['crit_1'] + $row['crit_2']).'</td></tr>'; 
     } 
    echo '</table>'; 
    // echo(max($max_total)); 

$new = array_flip($max_total); 
echo $id = $new[max($max_total)]; 

$id是你unique field只是用这样并运行一个查询wherecondition在此queryfetch所需行

请阅读从这里大约array_filphttp://php.net/manual/en/function.array-flip.php

0

的MySQL> SELECT *,(crit_1 + crit_2)AS总FROM由总递减限1测试顺序; + -------- + -------- + -------- + ------- + ------- + |用户ID | crit_1 | crit_2 |总| |总| | + -------- + -------- + -------- + ------- + ------- + | 2 | 100 | 100 | 0 | 200 | + -------- + -------- + -------- + ------- + ------- +

+ -------- + -------- + -------- + ------- + ------- + |用户ID | crit_1 | crit_2 |总| |总| | + -------- + -------- + -------- + ------- + ------- + | 1 | 75 | 75 | 0 | 150 | | 2 | 100 | 100 | 0 | 200 | + -------- + -------- + -------- + ------- + ------- +