Php 根据情况为表格的特定行指定颜色
我从数据库列名“DoyouhavePassport”中创建了一个表,用户回答是或否,我希望其中用户回答是是,行应该是绿色,而用户回答是否,行是白色,有人能告诉我如何将css应用于这个动态工作的表吗Php 根据情况为表格的特定行指定颜色,php,html,mysql,css,mysqli,Php,Html,Mysql,Css,Mysqli,我从数据库列名“DoyouhavePassport”中创建了一个表,用户回答是或否,我希望其中用户回答是是,行应该是绿色,而用户回答是否,行是白色,有人能告诉我如何将css应用于这个动态工作的表吗 <html> <head> <style> table, th, td { border: 1px solid black; border-collapse: collapse; } </style> </head> <
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
</style>
</head>
</html>
<?php
$conn = new mysqli("localhost","root","","db_dat");
$havepassport='';
$sql="SELECT * from upload;
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
$havepassport.='<table>'.'<tr>'.'<td>';
$havepassport.=$row['having_passport'];
$havepassport.='</table>'.'</tr>'.'</td>';
echo $havepassport;
}
?>
表,th,td{
边框:1px纯黑;
边界塌陷:塌陷;
}
签出此小提琴:
HTML
<table id="mytable">
<tr>
<td>User Name</td>
<td>Yes</td>
</tr>
<tr>
<td>User id</td>
<td>No</td>
</tr>
<tr>
<td>Whatever</td>
<td>Yes</td>
</tr>
</table>
选择包含指定文本的所有元素
文件:
编辑
您可以使用其他方法。每个函数如下所示
$('#mytable td').each(function() {
var value = $(this).html();
if (value == "Yes") {
$(this).parent().css("background", "red");
}
});
试试这个:
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
</style>
</head>
</html>
<?php
$conn = new mysqli("localhost","root","","db_dat");
$havepassport='';
$sql="SELECT * from upload;
$result = $conn->query($sql);
$havepassport.='<table>';
while($row = $result->fetch_assoc()) {
$passHaveClass = '';
if($row['having_passport'] =="yes"){
$passHaveClass = "greenColor";
}
$havepassport.='<tr class='.$passHaveClass.'>';
$havepassport.= '<td>'.$row['having_passport'].'</td>';
$havepassport.='</tr>;
}
$havepassport.='</table>';
echo $havepassport;
?>
.green {
background: green;
}
.red {
background: red;
}
表,th,td{
边框:1px纯黑;
边界塌陷:塌陷;
}
你做错了。您的while
将新的表添加到HTML中。因此,如果您有100行,那么将向DOM添加100个table
s,而不是行
使用以下内容:
PHP
$sql = "SELECT * from upload";
$result = $conn -> query($sql);
$havepassport = '<table>';
while ($row = $result -> fetch_assoc()) {
$passportClass = $row['having_passport'] == 'Yes' ? 'green' : 'red';
// ^^^^^^^^^^^ Getting classname depending on the passport value
$havepassport .= '<tr class='.$passportClass.'>'.
// ^^^^^^^^^^^^^^ Add Class Here
'<td>';
$havepassport. = $row['having_passport'];
$havepassport .= '</td>'.
'</tr>';
}
$havepassport .= '</table>';
echo $havepassport;
除了相当明显的语法错误外,您自己还试过什么?我想$havepassport.='''''代码>应为$havepassport.=''代码>如果我尝试过,我肯定会发布它,但我仍然没有得到任何逻辑。我想在有护照栏ans的地方添加颜色是的。你是否尝试提供有条件的css?css的任何代码?@Rupsharora:你检查过我的小提琴吗?非常感谢Tushar
.green {
background: green;
}
.red {
background: red;
}