我是PHP的新手,iam尝试根据你可以在IF块中看到的条件对column4,column7,column9的单元格着色,下面是我的代码我试过的,好心的帮助我理解如何实现这一点。我使用数组是因为我有80多列,在下面的示例中,iam只显示了10个数据。PHP多个数组while循环if条件
<?php
$con=mysqli_connect("server","user","password","db");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM table1");
echo "<table id='table_id' class='mytable'>
<thead>
<tr>
<th class='heading'>Column1</th>
<th class='heading'>Column2</th>
<th class='heading'>Column3</th>
<th class='heading'>Column4</th>
<th class='heading'>Column5</th>
<th class='heading'>Column6</th>
<th class='heading'>Column7</th>
<th class='heading'>Column8</th>
<th class='heading'>Column9</th>
<th class='heading'>Column10</th>
</tr>
</thead>";
echo "<tbody>";
while ($row = mysqli_fetch_array($result))
{
$colorfields = array($row['Column4'],$row['Column7'],$row['Column9']);
if ($colorfields < 195 && $colorfields !='')
$classname = "red";
else if($colorfields >= 195 && $colorfields < 199.99)
$classname = "yellow";
else if($colorfields >= 199.99)
$classname = "green";
echo "<tr>";
echo "<td class='normal_cell'>" . $row['Column1']."</td>";
echo "<td class='normal_cell'>" . $row['Column2']."</td>";
echo "<td class='normal_cell'>" . $row['Column3']."</td>";
echo "<td class=".$classname.">". $row['Column4']."</td>";
echo "<td class='normal_cell'>" . $row['Column5']."</td>";
echo "<td class='normal_cell'>" . $row['Column6']."</td>";
echo "<td class=".$classname.">". $row['Column7']."</td>";
echo "<td class='normal_cell'>" . $row['Column8']."</td>";
echo "<td class=".$classname.">". $row['Column9']."</td>";
echo "<td class='normal_cell'>" . $row['Column10']."</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
mysqli_close($con);
?>
和你面对什么问题? –
您的代码很奇怪,'$ colorfields'是一个数组,您不能将它与标量值(如字符串或数字)进行比较。这些神奇的价值是什么:195,199.99?我认为这个问题只能用css3选择器来解决。 –
@Anant,我没有得到IF条件为真的单元格的预期颜色,而是在所有单元格上获得了绿色。 – davidb