【发布时间】:2021-08-31 22:27:28
【问题描述】:
这是非常基本的东西,但我很难过,也许有人可以指出正确的方向,为什么这不起作用?
我运行一个 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 个颜色变量来回显结果两次,但没有任何效果。我确实看到的一件事是每一行都有相同的整数值,这意味着 $i 变量不会在每个时间后递增,这让我相信这是 Mysqli While 循环本身的问题。
我使用了各种提取方法,包括 while ($row8 = mysqli_fetch_array($result8)) { 和 while ($row8 = $result8->fetch_assoc()) {,但没有结果。请帮忙?
【问题讨论】:
-
只有颜色不起作用?,你能查看源代码并看到颜色的结果吗?
-
源显示每一行的颜色相同,这意味着整数增量存在问题,不应该。
-
太奇怪了,我在本地机器上用虚拟数据测试你的代码,它的工作原理
-
我尝试了一大堆不同的选项,包括用不同的可变颜色回显代码两次,但没有效果。不知道为什么这适用于您的系统但不适用于我的系统。也没有 CSS 干扰,都是内联的。
标签: php mysqli while-loop