簡體   English   中英

如何使用表格中的數據顯示每列的最大值?

[英]How to display the highest value per column using data from a table?

我的桌子看起來像這樣。 這是我的代碼,用於顯示每個類別的平均值。 但是我想找到每列的最高價值。 像在C1.1中一樣,我想在該列中找到最高的平均值。

<div class="container" class="col-lg-5">
<table class="table table-striped table table-bordered" >
<tr><th>Campus</th>
<th>No. of Staff</th>
<th>C1.1</th>
<th>C1.2</th>
<th>C1.3</th>
<th>C1.4</th>
<th>C1.5</th>
<th>C1.6</th>
<th>C2.1</th>
<th>C2.2</th>
<th>C2.3</th>
<th>C3.1</th>
<th>C3.2</th>
<th>C3.3</th>
<th>C3.4</th>

    <tr>
    <td>MAIN 1 - CABEIHM</td>
    <td>
            <?php   
            $queryn ="SELECT COUNT(dept_code) FROM employment where                                     dept_code=3 and empg_code=1";
            $resultn = mysql_query($queryn) or die(mysql_error());
            while($row1 = mysql_fetch_array($resultn)){
            echo "".$row1['COUNT(dept_code)'];
             echo "<br />";
             }
         ?>
    </td>
    <td><!--1.1-->
    <?php

    $query1 = ("SELECT  ROUND(AVG(Compv11),2) , dept_code ,  camp_code
                                FROM performance 
                                INNER JOIN employment 
                                ON employment.emp_code=performance.emp_id AND employment.dept_code=performance.dept_id
                                WHERE empg_code=1  AND dept_id=3
                                ");
                $result1 = mysql_query($query1) or die(mysql_error());
            [enter image description here][1]

                // Print out result
                while($row = mysql_fetch_array($result1))
                {
                echo "".$row['ROUND(AVG(Compv11),2)'];
                }

        ?>

要從C1.1列中獲取平均值最高的行,最好的選擇是在PHP中進行計算。

從數據庫中獲取值后,當前您將直接輸出這些值。 您需要將平均值存儲在變量中,以便以后可以獲取最大值:

$averages = array();

$query1 = ("SELECT  ROUND(AVG(Compv11),2) , dept_code ,  camp_code
            FROM performance 
            INNER JOIN employment 
            ON employment.emp_code=performance.emp_id AND employment.dept_code=performance.dept_id
            WHERE empg_code=1  AND dept_id=3
");

$result1 = mysql_query($query1) or die(mysql_error());

// Print out result
while($row = mysql_fetch_array($result1))
{
    $averages[1] = $row['ROUND(AVG(Compv11),2)'];
    echo "".$row['ROUND(AVG(Compv11),2)'];
}

請注意,我將結果添加到$averages索引1中,因為您的變量分別稱為$query1$result1並且我假設對於所有其他行,您都使用遞增變量(即第二行的$query2$result2 ,等等。)。 接下來的方法不是很有效,之后我會給您一個替代方法。

一旦所有平均值都在$averages ,就可以對數組進行排序:

arsort($averages);

這將以相反的順序對數組進行排序(這意味着第一個條目將是具有最高值的條目),同時保持索引與值的關聯。 現在我們reset()數組設置為安全的,然后可以使用key()函數獲取索引,並使用current()函數獲取值本身:

reset($averages);
$highest_index = key($averages);
$highest_value = current($averages);

$highest_query = "query" . $highest_index;
$highest_result = "result" . $highest_index;

現在,您知道最大值來自$$highest_query ,其結果是$$highest_result ,以防您想對該信息進行某些操作。 例如,您可以使用mysql_data_seek($$highest_result, 0)重置它,然后可以使用mysql_fetch_array($$highest_result)再次獲取其行。

盡管您應該真正考慮從舊的,不受支持的mysql_*函數轉移到mysqliPDO進行數據庫通信。

現在,我提到:

根據您要使用的最大值(您說要輸出它並“對其進行報告”),您可能還想知道您要在查詢中過濾哪個empg_codedept_id ,或者您選擇的dept_codecamp_code以及平均值。

因此,讓我們在$averages數組中填充的不僅僅是平均值:

while($row = mysql_fetch_array($result1))
{
    $averages[] = array(
        'avg' => $row['ROUND(AVG(Compv11),2)'],
        'empg_code' => 1,
        'dept_id' => 3,
        'dept_code' => $row['dept_code'],
        'camp_code' => $row['camp_code']
    );
    echo "".$row['ROUND(AVG(Compv11),2)'];
}

現在我們可以使用usort通過自定義函數對數組進行排序:

usort($averages, function($a, $b) {
    if ($a['avg'] == $b['avg']) {
        return 0;
    }

    // note: reverse check of the example, because we want to
    // sort in DESCENDING order
    // https://php.net/usort
    return ($a['avg'] > $b['avg']) ? -1 : 1;
});

現在,具有最高值的條目的數據將是$averages數組的第一個條目:

$highest = $averages[0];

echo $highest['avg'];       // highest average value
echo $highest['empg_code']; // 1
echo $highest['dept_id'];   // 3
echo $highest['dept_code']; // dept_code for the highest average value
echo $highest['camp_code']; // camp_code for the highest average value

希望有幫助!

暫無
暫無

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

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