php - 根据字段中的文本更改文本颜色

时间:2016-01-04 18:13:23

标签: php html

我正在尝试让我的代码更改文本的颜色。 如果“返回”字段为“是”,我希望文本更改为绿色。如果该字段为“否”则为红色

从数据库中提取信息并运行while语句以获取表中的每一行。

我是php& amp;的初学者html所以只是一直玩,但似乎是我正在尝试的一切错误。我得到的代码是;

value

2 个答案:

答案 0 :(得分:0)

您可以将代码更改为:

<?php
require_once 'header.php';
require_once('my_connect.php');


$my_query="select * from loans order by user";
$result= mysqli_query($connection, $my_query);
?>

<table border=1 cellpadding=10>
<thead>
<tr><th>Loan ID</th<th>Product</th><th>User</th><th>Expected Return Date?</th><th>Returned?</th> <th>Edit? </th>
</thead>
<tbody>
<?php
while ($myrow = mysqli_fetch_array($result)){
$loanid = $myrow["loanid"];
$product = $myrow["product"];
$user = $myrow["user"];
$date_return=$myrow["date_return"];
$returned=$myrow["returned"];
echo "
<tr>
<td>$loanid </td>
<td>$product </td>
<td>$user </td>
<td>$date_return </td>
<td style='color: " . ($returned == "Yes") ? "green" : "red" . ";'> $returned </td>
<td><a onClick =\"confirm('Are You Sure You Want To Edit This   Loan?')\" href=editloaninfo.php?loanid=$loanid><img src=\"edit.png\"> </td>";
}
?>
</tbody>
</table>

答案 1 :(得分:0)

我不知道你要改变字体颜色的位置,所以这是一个例子:

if($returned == 'Yes'){
     $style = 'green'; //or hex value
 }else{
     $style = 'red';
 }

 <td style="color:<?=$style;?>"></td>