猿问

CSS/PHP:根据从 MySQL 检索的值更改字体颜色

我的代码有问题。目前,我在数据库中有一个名为“Book_status”的列。有待处理,批准和拒绝3个条件。


我希望字体根据该条件在 PHP 中更改其颜色。例如,Pending 将字体更改为 Yellow,Approved 为 Green,Reject 为 Red。


以下是我当前的 PHP 代码:


echo "<tr>";

        echo "<td>" . $row['id'] . "</td>";

        echo "<td>" . $row['Requested_by'] . "</td>";                                              

        echo "<td>" . $row['Fac_ID'] . "</td>";

        echo "<td>" . $row['Room_Desc'] . "</td>";

        echo "<td>" . $row['Meeting_Description'] . "</td>";    

        echo "<td>" . $row['Book_Status'] . " </td>";      

        echo "<td><a type='button' class='btn btn-primary btn sm'href='view.php?id=". $row['id'] ."' title='Edit Booking' data-toggle='tooltip'>View</a></td>";

echo "</tr>";


慕桂英546537
浏览 134回答 3
3回答

慕姐8265434

只需将一些 CSS 类命名为与状态值相同的名称,并将其作为样式分配给表格行<style>&nbsp; &nbsp; .pending td{color:yellow;}&nbsp; &nbsp; .approved td{color:green}&nbsp; &nbsp; .reject td{color:red}</style>然后,在 PHP/HTML 中:&nbsp; &nbsp; $class=strtolower( $row['Book_Status'] );&nbsp; &nbsp; echo "<tr class='$class'>";&nbsp; &nbsp; echo "<td>" . $row['id'] . "</td>";&nbsp; &nbsp; echo "<td>" . $row['Requested_by'] . "</td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; echo "<td>" . $row['Fac_ID'] . "</td>";&nbsp; &nbsp; echo "<td>" . $row['Room_Desc'] . "</td>";&nbsp; &nbsp; echo "<td>" . $row['Meeting_Description'] . "</td>";&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; echo "<td>" . $row['Book_Status'] . " </td>";&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; echo "<td><a type='button' class='btn btn-primary btn sm'href='view.php?id=". $row['id'] ."' title='Edit Booking' data-toggle='tooltip'>View</a></td>";&nbsp; &nbsp; echo "</tr>";

largeQ

这将工作<?php$status=$row['Book_Status'];if($status=="Approved"){&nbsp; &nbsp; $color="color:green";}else if($status=="Pending"){&nbsp; &nbsp; $color="color:yellow";}else&nbsp;{&nbsp; &nbsp; $color="color:red";}&nbsp;echo "<table><tr><td></td><td></td><td></td><td></td><td></td><td style='$color'>".$status ."</td></tr></table>";&nbsp;&nbsp;?>

斯蒂芬大帝

这在某种程度上与此相似但不是传递数字而是传递状态function statusColor($status){&nbsp; &nbsp; if ($status == 'Pending')&nbsp; &nbsp; &nbsp; &nbsp; return 'is-pending';&nbsp; &nbsp; else if ($status == 'Approved')&nbsp; &nbsp; &nbsp; &nbsp; return = 'is-approved';&nbsp; &nbsp; else if ($status == 'Rejected')&nbsp; &nbsp; &nbsp; &nbsp; return = 'is-rejected';}对于 html 是这样的<tr class="<?=statusColor('Pending');?>">...</tr>我总是使用类而不是内联 css.is-pending {&nbsp; color: yellow;}.is-approved {&nbsp; color: green;}.is-rejected {&nbsp; color: red;}
随时随地看视频慕课网APP
我要回答