2

我有这个表名“ casillas

            colum_1     colum_2     colum_3     colum_4
        ___     ___     ___     ___   ___   ___   ___
field_1 |   46654   |   8463    |   1284    |   1654
field_2 |   14668   |   547896  |   11563   |   4565
field_3 |   6486    |   54763   |   15697   |   56776
field_4 |   45684   |   5668    |   989     |   48678

我像这样在 PHP 中显示它:

$result = mysql_query("SELECT SUM(colum_1) FROM casillas");
while($row = mysql_fetch_array($result)){
$colum_1_result = $row['SUM(colum_1)'];
}

$result = mysql_query("SELECT SUM(colum_2) FROM casillas");
while($row = mysql_fetch_array($result)){
$colum_2_result = $row['SUM(colum_2)'];
}

$result = mysql_query("SELECT SUM(colum_3) FROM casillas");
while($row = mysql_fetch_array($result)){
$colum_3_result = $row['SUM(colum_3)'];
}

$result = mysql_query("SELECT SUM(colum_4) FROM casillas");
while($row = mysql_fetch_array($result)){
$colum_4_result = $row['SUM(colum_4)'];
}

echo '<table style="width: 100%">
                <tr>
                                <td>Results:</td>
                                <td>'.$colum_1_result.'</td>
                                <td>'.$colum_2_result.'</td>
                                <td>'.$colum_3_result.'</td>
                                <td>'.$colum_4_result.'</td>
                </tr>
</table>';

我的问题是:当值比其他值更大时,有没有办法让它变粗或从单元格中更改 CSS?

在这种情况下,它将是来自colum_2的那个

4

1 回答 1

2

首先,当您可以在一个请求中获得所需的一切时,不要多次访问数据库。您可以通过这样的查询获得所有总和和最大值

SELECT sum1, sum2, sum3, sum4, GREATEST(sum1, sum2, sum3, sum4) max_sum
  FROM
(
  SELECT SUM(colum_1) sum1, SUM(colum_2) sum2, SUM(colum_3) sum3, SUM(colum_4) sum4
  FROM casillas
) q

查询的示例输出:

| 总和1 | 总和2 | 总和3 | 总和4 | 最大总和 |
----------------------------------------------
| 113492 | 616790 | 29533 | 111673 | 616790 |

这是SQLFiddle演示

现在在 php 代码中将总和值与最大值进行比较,如果匹配,则为相应的<td>. 显然,您需要在外部样式表中的某处定义该样式。

<style>
    .maxsum {
        font-weight: bold;
    }
</style>

<?php 

$link = mysql_connect('localhost', 'user', 'password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db('dbname', $link);
if (!$db_selected) {
    die ('Can\'t use db : ' . mysql_error());
}

$sql = "SELECT sum1, sum2, sum3, sum4, GREATEST(sum1, sum2, sum3, sum4) max_sum
  FROM
(
  SELECT SUM(colum_1) sum1, SUM(colum_2) sum2, SUM(colum_3) sum3, SUM(colum_4) sum4
    FROM casillas
) q";

$result = mysql_query($sql);
if($result === FALSE) {
    die(mysql_error()); // TODO: better error handling
}
if ($row = mysql_fetch_assoc($result)) {
    echo '<table style="width: 100%">
            <tr>
                <td>Results:</td>
                <td '. ($row['sum1'] == $row['max_sum'] ? 'class="maxsum"' : '') .' >'.$row['sum1'].'</td>
                <td '. ($row['sum2'] == $row['max_sum'] ? 'class="maxsum"' : '') .' >'.$row['sum2'].'</td>
                <td '. ($row['sum3'] == $row['max_sum'] ? 'class="maxsum"' : '') .' >'.$row['sum3'].'</td>
                <td '. ($row['sum4'] == $row['max_sum'] ? 'class="maxsum"' : '') .' >'.$row['sum4'].'</td>
            </tr>
    </table>';
}

附带说明:mysql_扩展已被弃用,不再受支持。因此考虑切换到mysqli_or PDO

于 2013-07-03T09:13:32.120 回答