簡體   English   中英

如何從不在MySQL中的php中的值總和中獲得最高值

[英]How to get the highest value from a sum of values in php that is not in MySQL

美好的一天,我在php中有一張表,總結了存儲在MySQL中的值。

MySQL表:

USER 1:
crit_1 = 75
crit_2 = 75
USER2:
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, '.',' ');

這是我的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存儲在MySQL中的crit_1和crit_2。 我想做的就是從第三列中獲得最高的值,即$total並將其放置在另一列或另一個文本框中的某個地方(任何人都可以做)

到目前為止,我嘗試過的是。

    $value = max($total);
    $key = array_search($value, $total);

<td align="center"><?php echo (max($total)); ?></td>

但我有這個錯誤:

max(): When only one parameter is given, it must be an array

所以我想我的真正問題是,如何將$ total轉換為字符串數組,以便獲得最高的價值。 或者,如果您確實有其他方法可以解決我的問題。 謝謝。

我創建了一個名為$max_totalarray ,每次looping時在其中insert $ total value 然后使用max() fuction獲得最大值。

<?php
$servername = "localhost";
$username = "root";
$password = "";
$mysql_database = "a";

$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['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));

根據您的database name更改table namedatabase name

有關更多信息,請訪問http://php.net/manual/en/function.max.php

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM