我这里有问题。我试过到处寻找,但似乎无法找到它。我知道有关于此的帖子,也许我无法正确搜索我的搜索查询。
我发现它类似于这个2: Applying different CSS to top 10 results of a query .addClass with IF statement based on value
目前这是我的代码的简短示例:
<?php
$select=mysql_query("select * from products_list ORDER BY id ASC");
while($row=mysql_fetch_array($select)){
echo "<tr>
<td>".$row['id']."</td>
<td>".$row['title']."</td>
<td>".$row['new_arrival']."</td>";
?>
数据库中new_arrival的数据是yes还是no。
我的问题是如果它是“是”,我如何在TD上为“new_arrival”添加一个类,如果是“否”则不添加类
希望你们明白我想说的话。感谢。
此致 杰夫
答案 0 :(得分:0)
如果new_arrival == yes
添加了其他类,则使用简单的if语句将其清空
$select=mysql_query("select * from products_list ORDER BY id ASC");
while($row=mysql_fetch_array($select)){
$class = ($row['new_arrival'] == 'yes') ? 'class="new_arrival"' : '';
echo "<tr>
<td ".$class.">".$row['id']."</td>
<td ".$class.">".$row['title']."</td>
<td ".$class.">".$row['new_arrival']."</td></tr>";
}
答案 1 :(得分:0)
<?php
$select=mysql_query("select * from products_list ORDER BY id ASC");
while($row=mysql_fetch_array($select)){
echo "<tr>
<td>".$row['id']."</td>
<td>".$row['title']."</td>";
if(if($row['new_arrival']=='yes')
{
echo "<td class='classname'>".$row['new_arrival']."</td>";
}
else
{
echo "<td>".$row['new_arrival']."</td>";
}
?>
答案 2 :(得分:0)
试试这个简单的IF
echo "<tr>
<td>".$row['id']."</td>
<td>".$row['title']."</td>";
if($row["new_arrival"] == "yes"){
echo "<td class='positive'>".$row["new_arrival"]."</td>"; //yes
}else{
echo "<td>".$row['new_arrival']."</td>"; // no
}
答案 3 :(得分:0)
<?php
$select = mysql_query( "SELECT * FROM products_list ORDER BY id ASC" );
while( $row = mysql_fetch_array( $select ) ) {
$class = '';
if ( $row[ 'new_arrival' ] == 'yes' )
$class = ' class="your_class" ';
$result = "<tr>
<td>" . $row['id'] . "</td>
<td>" . $row['title'] . "</td>
<td" . $class. ">" . $row['new_arrival'] ."</td>
</tr>";
echo $result;
}
?>