基于PHP结果的行的不同颜色

时间:2017-11-10 11:13:48

标签: php

我有以下php代码从mysql表中获取结果。如果columnX = error

,我想更改行的颜色
    <?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="PASS"; // Mysql password
$db_name="syslog"; // Database name
$tbl_name="logs"; // Table name
// Connect to server and select databse
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
// deinf host
$color="$host";
echo '<table width="1600" border="2" align="left" cellpadding="1" cellspacing="1">';
while($rows=mysql_fetch_array($result)){
//If $color==1 table row color = #FFC600
if($color=='10.0.0.1'){
        echo "<tr bgcolor='#f2f2f2'><td>".$rows['seq']."</td<td>".$rows['host']."</td><td>".$rows['datetime']."</td<td>".$rows['priority']."</td><td>".$rows['program']."</td<td>".$rows['msg']."</t$
}else if($color=='10.0.0.2'){//updated you condition here
      echo "<tr bgcolor='#cccccc'><td>".$rows['seq']."</td<td>".$rows['host']."</td><td>".$rows['datetime']."</td><td>".$rows['priority']."</td><td>".$rows['program']."</td><td>".$rows['msg']."</t$
}
}
echo '</table>';
mysql_close();
?>

想要的是,如果$ color(或host)是= x.x.x.x然后显示color1,如果$ color(或host)= y.y.y.y则显示color2。

1 个答案:

答案 0 :(得分:0)

尝试使用以下代码

您必须为$color变量

指定值
echo '<table width="1600" border="2" align="left" cellpadding="1" cellspacing="1">';
echo '<tr><th>col1</th><th>col1</th><th>col1</th><th>col1</th><th>col1</th><th>col1</th></tr>';

    while($rows=mysql_fetch_array($result)){

//$color assign here from result
$color=$rows['host'];

        if($color==1){

        echo "<tr bgcolor='#f2f2f2'><td>".$rows['seq']."</td<td>".$rows['host']."</td><td>".$rows['datetime']."</td<td>".$rows['priority']."</td><td>".$rows['program']."</td<td>".$rows['msg']."</td></tr>";

}else if($colorX=='error'){//updated you condition here    


      echo "<tr bgcolor='#cccccc'><td>".$rows['seq']."</td<td>".$rows['host']."</td><td>".$rows['datetime']."</td><td>".$rows['priority']."</td><td>".$rows['program']."</td><td>".$rows['msg']."</td></tr>";
}