Php 基于MYSQL值将单元格高亮显示为特定颜色

Php 基于MYSQL值将单元格高亮显示为特定颜色,php,mysql,css,Php,Mysql,Css,我有三个值可以从mysql中的表/列中显示,红色、绿色、黄色为字段“ProspectStatus” 我是否可以让单元格根据值更改其背景颜色 e、 g echo”“$行['ProspectStatus']。""; PHP代码: $result = mysql_query("SELECT * FROM customerdetails"); //List the Columns for the Report echo "<table border='1'> <tr>

我有三个值可以从mysql中的表/列中显示,红色、绿色、黄色为字段“ProspectStatus”

我是否可以让单元格根据值更改其背景颜色

e、 g

echo”“$行['ProspectStatus']。"";
PHP代码:

 $result = mysql_query("SELECT * FROM customerdetails"); 
//List the Columns for the Report 
echo "<table border='1'> 
<tr> 
<th>CustomerID</th> 
<th>Customer Name</th> 
<th>Prospect Status</th> 
<th>Address</th> 
</tr>"; 

while($row = mysql_fetch_array($result)) 
  { 
  echo "<tr>"; 
  echo "<td>" . $row['CustomerID'] . "</td>"; 
  echo "<td>" . $row['CustomerName'] . "</td>"; 
  echo "<td>" . $row['ProspectStatus'] . "</td>"; //this is the field I want to show either RED, GREEN or YELLOW 
  echo "<td>" . $row['Address'] . "</td>"; 
  echo "</tr>"; 
  } 
echo "</table>";  
$result=mysql\u查询(“从customerdetails中选择*);
//列出报告的列
回声“
客户ID
客户名称
前景状况
地址
"; 
while($row=mysql\u fetch\u数组($result))
{ 
回声“;
回显“$row['CustomerID']”;
回显“$row['CustomerName]”;
echo“$row['ProspectStatus']”;//这是我要显示红色、绿色或黄色的字段
回显“$row['Address']”;
回声“;
} 
回声“;

为该单元格创建所需颜色的
,然后执行
if
语句检查该值。根据值,回显
class

是您可以将其添加到表数据标记中,如下所示

以下代码将起作用:

while($row = mysql_fetch_array($result)) 
  { 
  echo "<tr style='background-color: ". $row['ProspectStatus'] ."'>"; 
  echo "<td>" . $row['CustomerID'] . "</td>"; 
  echo "<td>" . $row['CustomerName'] . "</td>"; 
  echo "<td>" . $row['ProspectStatus'] . "</td>"; //this is the field I want to show either RED, GREEN or YELLOW 
  echo "<td>" . $row['Address'] . "</td>"; 
  echo "</tr>"; 
  } 
while($row=mysql\u fetch\u array($result))
{ 
回声“;
回显“$row['CustomerID']”;
回显“$row['CustomerName]”;
echo“$row['ProspectStatus']”;//这是我要显示红色、绿色或黄色的字段
回显“$row['Address']”;
回声“;
} 
echo”“;
或者做一个if语句,检查颜色,然后显示回声


这应该行得通。

既然您说只想更改一个特定单元格的颜色,那么这应该可以做到:

while($row = mysql_fetch_array($result)) 
      { 
      echo "<tr>"; 
      echo "<td>" . $row['CustomerID'] . "</td>"; 
      echo "<td>" . $row['CustomerName'] . "</td>"; 
      echo "<td  style='background-color: ". $row['ProspectStatus'] ."'>" . $row['ProspectStatus'] . "</td>";
      echo "<td>" . $row['Address'] . "</td>"; 
      echo "</tr>"; 
      } 
while($row=mysql\u fetch\u array($result))
{ 
回声“;
回显“$row['CustomerID']”;
回显“$row['CustomerName]”;
回显“$row['ProspectStatus']”;
回显“$row['Address']”;
回声“;
} 

您可以通过以下方式执行此操作:

while($row = mysql_fetch_array($result)) 
  { 
  echo "<tr>"; 
  echo "<td>" . $row['CustomerID'] . "</td>"; 
  echo "<td>" . $row['CustomerName'] . "</td>"; 
  if($row['ProspectStatus']=='[val1]') // [val1] can be 'approved'
         echo "<td style='background-color: #00FF00;'>".$row['ProspectStatus']."</td>"; 
  else if($row['ProspectStatus']=='[val2]')// [val2]can be 'rejected'
         echo "<td style='background-color: #FF0000;'>".$row['ProspectStatus']."</td>"; 
  else if($row['ProspectStatus']=='[val3]') //[val3] can be 'on hold'
         echo "<td style='background-color: #FFFF00;'>".$row['ProspectStatus']."</td>"; 
  echo "<td>" . $row['Address'] . "</td>"; 
  echo "</tr>"; 
  } 
echo "</table>";  
while($row=mysql\u fetch\u array($result))
{ 
回声“;
回显“$row['CustomerID']”;
回显“$row['CustomerName]”;
如果($row['ProspectStatus']=='[val1]')/[val1]可以被“批准”
回显“$row['ProspectStatus']”;
否则,如果($row['ProspectStatus']=='[val2]')/[val2]可以被“拒绝”
回显“$row['ProspectStatus']”;
如果($row['ProspectStatus']=='[val3]')/[val3]可以是“暂挂”
回显“$row['ProspectStatus']”;
回显“$row['Address']”;
回声“;
} 
回声“;

状态值可能取决于颜色。我假设
val1
val2
val3
是这三种颜色的值。

首先,使用CSS,而不是凌乱的内嵌样式定义。

最易维护的选项可能会将颜色代码本身与视图逻辑分离,因此在CSS中,创建一些新类:

td.done { background-color: green; }
td.working { background-color: yellow; }
td.stopped { background-color: red; }
然后,在循环中执行此操作(您不需要echo语句,任何
标记之外的内容都将被解析为HTML而不是PHP):

对于这一行,如果
ProspectStatus
大于80,则字段将为绿色,如果介于40和80之间,则字段将为黄色,如果低于40,则字段将为红色

注意:
mysql\uuz
函数已弃用

通过使用
mysql\uuz
功能,您的程序可能无法在未来版本的PHP中运行,因为这些函数从5.5开始就已被正式弃用

执行查询的新建议方法如下:

$mysqli = new mysqli("username","password","hostname","password");
$result = $mysqli->query("SELECT * FROM customerdetails");
while($row = $result->fetch_assoc())...

希望这有帮助。

我非常喜欢这个答案,它对黄色和绿色都很有效,但红色不起作用。我想这是因为“[红色]”很高兴它能帮上忙。您可以将颜色更改为html代码,如红色=#FF0000、黄色=#FFFF00和绿色=#00FF00。不过,你可以接受这个答案。我将为您编辑此内容。您在这里有机会解决一位新来者使用的糟糕的编码实践,而您发布了一个像这样草率的解决方案。悲哀的是,与这样的编码惯例争论是毫无意义的。尽管如此,我还是回答了这个问题。好吧,假设您必须引用
ProspectStatus
的值,然后您必须创建一个新的CSS类?同样的可维护性问题,而您可以在线进行。谢谢你的建设性批评。@ChristianMark这是怎么回事?在现实编程中,视图的变化比业务逻辑的变化要频繁得多。如果他使用相同的数据(或另一段使用红色/黄色/绿色背景的数据),他可以重复使用
红色
绿色
黄色
类,当他想要更改显示逻辑时,他只需在一个地方更改它。@ChristianMark-我在生产代码中有类似的东西,我倾向于使用嵌套的三元内联来选择要显示的类,比如我在“If XYZ是一个范围…”下面的内容。有人能帮我解决上述问题吗?
td.done { background-color: green; }
td.working { background-color: yellow; }
td.stopped { background-color: red; }
<?php while($row = mysql_fetch_array($result)): ?>
<tr>
    ....
    <td class='<?= $row['ProspectStatus']; ?>'><?= $row['ProspectStatus']; ?></td>
    ....
</tr>
<?php endwhile; ?>
<tr class='<?php echo ($row['ProspectStatus'] >= 80) ? "done" : (($row['ProspectStatus'] >= 40) ? "working" : "stopped"); ?>'>
$mysqli = new mysqli("username","password","hostname","password");
$result = $mysqli->query("SELECT * FROM customerdetails");
while($row = $result->fetch_assoc())...