MySQLi 查询 - 结果颜色替代表行

MySQLi Query - while result color alternative table rows

提问人:Armitage2k 提问时间:6/16/2021 最后编辑:Armitage2k 更新时间:6/16/2021 访问量:77

问:

这是非常基本的东西,但我被难住了,也许有人可以给我指出正确的方向,为什么这不起作用?

我运行一个 MySQL 查询,该查询返回 5 行,然后将其回显到表中。在每行输出后,我将行 bgcolor 更改为交替并使其更具可读性,只有代码似乎不起作用,因为我看到所有行都采用相同的颜色。我尝试了 2 个选项,但都不起作用,感谢任何指导。

选项 1 - 带颜色的数组

// query database
if (!$result8 = $conn->query($sql8)) {
    die('There was an error running SQL query #8 [' . $conn->error . ']');
}

// define alternating row background colors
$rowColors = Array('#EEEEEE','#FFFFFF');
$i = 0;

// output all string categories in array
while ($row8 = $result8->fetch_assoc()) {
    $dml_problem_mbr = $row8['Count_Member'];
    $dml_problem_mbr_pc = round(($dml_problem_mbr / $dml_problem_total) * 100);

    $body_message .= "
        <tr bgcolor=\"" . $rowColors[$i++ % count($rowColors)] . "\" style=\"-ms-text-size-adjust: 100%;-webkit-text-size-adjust: 100%;\">
            <td width=\"50%\" align=\"left\" style=\"font-family: Verdana, Geneva, Helvetica, Arial, sans-serif;font-size: 12px;color: #252525;padding: 10px;padding-right: 0;-ms-text-size-adjust: 100%;-webkit-text-size-adjust: 100%;mso-table-lspace: 0pt !important;mso-table-rspace: 0pt !important;\">
                ".$dml_problem_name. "
            </td>
            <td width=\"16.67%\" align=\"right\" style=\"font-family: Verdana, Geneva, Helvetica, Arial, sans-serif;font-size: 12px;color: #252525;padding: 10px;padding-left: 0;-ms-text-size-adjust: 100%;-webkit-text-size-adjust: 100%;mso-table-lspace: 0pt !important;mso-table-rspace: 0pt !important;\">
                ".$dml_problem_total. "
            </td>
            <td width=\"16.67%\" align=\"right\" style=\"font-family: Verdana, Geneva, Helvetica, Arial, sans-serif;font-size: 12px;color: #252525;padding: 10px;padding-left: 0;-ms-text-size-adjust: 100%;-webkit-text-size-adjust: 100%;mso-table-lspace: 0pt !important;mso-table-rspace: 0pt !important;\">
                ".$dml_problem_mbr." (".$dml_problem_mbr_pc. "%)
            </td>
            <td width=\"25%\" align=\"right\" style=\"font-family: Verdana, Geneva, Helvetica, Arial, sans-serif;font-size: 12px;color: #252525;padding: 10px;padding-left: 0;-ms-text-size-adjust: 100%;-webkit-text-size-adjust: 100%;mso-table-lspace: 0pt !important;mso-table-rspace: 0pt !important;\">
                3,000 THB
            </td>
        </tr>";
}

选项 2 - 在变量中定义颜色

// query database
if (!$result8 = $conn->query($sql8)) {
die('There was an error running SQL query #8 [' . $conn->error . ']');
}

// set counter to 0
$i = 0;

// output all string categories in array
while ($row8 = $result8->fetch_assoc()) {
    $dml_problem_mbr = $row8['Count_Member'];
    $dml_problem_mbr_pc = round(($dml_problem_mbr / $dml_problem_total) * 100);

    // adjust bgcolor of row
    if ($i % 2 == 0) { $bgcolor = "#FFFFFF"; } else { $bgcolor = "#EEEEEE"; }
    $i++;


    $body_message .= "
    <tr bgcolor=\"" . $bgcolor . "\" style=\"-ms-text-size-adjust: 100%;-webkit-text-size-adjust: 100%;\">
        <td width=\"50%\" align=\"left\" style=\"font-family: Verdana, Geneva, Helvetica, Arial, sans-serif;font-size: 12px;color: #252525;padding: 10px;padding-right: 0;-ms-text-size-adjust: 100%;-webkit-text-size-adjust: 100%;mso-table-lspace: 0pt !important;mso-table-rspace: 0pt !important;\">
            ".$dml_problem_name. "
        </td>
        <td width=\"16.67%\" align=\"right\" style=\"font-family: Verdana, Geneva, Helvetica, Arial, sans-serif;font-size: 12px;color: #252525;padding: 10px;padding-left: 0;-ms-text-size-adjust: 100%;-webkit-text-size-adjust: 100%;mso-table-lspace: 0pt !important;mso-table-rspace: 0pt !important;\">
            ".$dml_problem_total. "
        </td>
        <td width=\"16.67%\" align=\"right\" style=\"font-family: Verdana, Geneva, Helvetica, Arial, sans-serif;font-size: 12px;color: #252525;padding: 10px;padding-left: 0;-ms-text-size-adjust: 100%;-webkit-text-size-adjust: 100%;mso-table-lspace: 0pt !important;mso-table-rspace: 0pt !important;\">
            ".$dml_problem_mbr." (".$dml_problem_mbr_pc. "%)
        </td>
        <td width=\"25%\" align=\"right\" style=\"font-family: Verdana, Geneva, Helvetica, Arial, sans-serif;font-size: 12px;color: #252525;padding: 10px;padding-left: 0;-ms-text-size-adjust: 100%;-webkit-text-size-adjust: 100%;mso-table-lspace: 0pt !important;mso-table-rspace: 0pt !important;\">
            3,000 THB
        </td>
    </tr>";
}

编辑:

我尝试了一大堆其他解决方案,包括用 2 个颜色变量回显结果两次,但没有任何效果。我确实看到的一件事是每行都有相同的整数值,这意味着变量在每次之后都不会递增,这让我相信这是 Mysqli While 循环本身的问题。$i

我使用了各种提取方法,包括 和 但没有结果。请帮忙?while ($row8 = mysqli_fetch_array($result8)) {while ($row8 = $result8->fetch_assoc()) {

enter image description here

php mysqli while循环

评论

0赞 Jerson 6/16/2021
只有颜色不起作用?,您可以查看源并查看颜色的结果吗?
0赞 Armitage2k 6/16/2021
源为每行显示相同的颜色,这意味着整数增量存在问题,这不应该是。
0赞 Jerson 6/16/2021
太奇怪了,我在本地机器上用虚拟数据测试你的代码,它就可以工作了
0赞 Armitage2k 6/16/2021
我尝试了一大堆不同的选项,包括用不同的可变颜色回显代码两次,但没有效果。不知道为什么这适用于您的系统,但不适用于我的系统。也没有CSS干扰,它都是内联的。

答:

0赞 amirhosein karimi 6/16/2021 #1

您在 html 中输入了错误的属性。
你有这个功能 像这样使用:
bgcolorbackground-colorstyle
$i=0
if ($i % 2 == 0) { $bgcolor = "#FFFFFF"; } else { $bgcolor = "#EEEEEE"; }
$i++;

 <tr style=\"-ms-text-size-adjust: 100%;-webkit-text-size-adjust: 100%;background-color:$bgColor\">

评论

0赞 Armitage2k 6/16/2021
尝试了两种选择,都不起作用。不管是 it 还是 in ,通过整数的颜色变化似乎都不会发生。bgcolorstyle
0赞 amirhosein karimi 6/16/2021
我测试了代码,数字正确。