2017-08-25 53 views
0

我正在实施布料购物网站,通过管理员和管理员可以将库存添加到数据库中,并且可以查看,更新和删除库存。同时在数据库中显示表格中的记录时,我希望在客户购买该行颜色变成红色后,数量变为10或小于10的库存物品,以便它应该对管理员警告特定库存数量较低。 这里是我的代码:如果数量列的数值小于或等于10,如何更改表格行的背景颜色

<table> 
    <tr> 
     <th>Sr.No</th> 
     <th>Product ID</th> 
     <th>Brand</th> 
     <th>Price</th> 
     <th>Gender</th> 
     <th>Category</th> 
     <th>Material</th> 
     <th>Size</th> 
     <th>Description</th> 
     <th>Quantity</th> 
     <th>Image</th> 
    </tr> 
    <?php 
    $query = "SELECT * FROM add_stock ORDER BY id DESC"; 
    $rs_result = mysqli_query ($query); 
    while ($result=mysqli_fetch_array($rs_result)) 
    { 
    ?> 
     <?php $qty =$result['dress_quantity']; ?> 
      <tr <?php if($qty<=10){echo 'style="background:red"';} ?> > 
      <td><?php echo $result['id']; ?></td> 
      <td><?php echo $result['brand_name'];</td> 
      <td><?php echo $result['price']; ?></td> 
      <td><?php echo $result['gender_name']; ?></td> 
      <td><?php echo $result['category_name']; ?></td> 
      <td><?php echo $result['material_name']; ?></td> 
      <td><?php echo $result['size_name']; ?></td> 
      <td><?php echo $result['dress_description']; ?></td> 
      <td><?php echo $result['dress_quantity']; ?></td> 
      <td><a href="javascript:window.open('<?php echo $result['image'] ?>','mypopuptitle', '_parent')" >View Image</a></td> 
     </tr> 
</table> 
<?php 
} 
?> 

CSS代码:

table { 
    color: #333; 
    font-family: Helvetica, Arial, sans-serif; 

    border-collapse: 
    collapse; border-spacing: 0; 
} 

td, th { 
    border: 1px solid; /* No more visible border */ 
    height: 30px; 

    transition: all 0.3s; /* Simple transition for hover effect */ 
} 

th { 
    background: #DFDFDF; /* Darken header a bit */ 
    font-weight: bold; 
    text-align: center; 
    height: 50px; 
} 

td { 
    background: #FAFAFA; 

    height: 40px; 
} 

/* Cells in even rows (2,4,6...) are one color */   
tr:nth-child(even) td { background: #F1F1F1; } 

/* Cells in odd rows (1,3,5...) are another (excludes header cells) */   
tr:nth-child(odd) td { background: #FEFEFE; } 
+0

您的查询缺少右引号。这只是你的问题中的一个错字,或者是在你的实际代码中? –

+0

错字有问题没有实际的代码,我编辑它 –

+0

不是'tr:nth-​​child(odd)td {background:#FEFEFE; }'在'style =“background:red”'之上? –

回答

0

在CSS创建一个类像.isLess

.isLess { background-color:red;} 

然后做这样的事情:

三元运算符回显类,如果qty是< 10,则添加该类,如果不是则不输出任何东西。

<tr <?php echo ($result['dress_quantity'] < 10 ? "class='isLess'" : ""); ?> > 
      <td><?php echo $result['id']; ?></td> 
      <td><?php echo $result['brand_name'];</td> 
      <td><?php echo $result['price']; ?></td> 
      <td><?php echo $result['gender_name']; ?></td> 
      <td><?php echo $result['category_name']; ?></td> 
      <td><?php echo $result['material_name']; ?></td> 
      <td><?php echo $result['size_name']; ?></td> 
      <td><?php echo $result['dress_description']; ?></td> 
      <td><?php echo $result['dress_quantity']; ?></td> 
      <td><a href="javascript:window.open('<?php echo $result['image'] ?>','mypopuptitle', '_parent')" >View Image</a></td> 
     </tr> 

或者,你可以不喜欢

$class = ""; 
if($result['dress_quantity'] < 10) { $class='isLess'; } 
<tr <?php echo $class; ?> > 
+0

非常感谢它的完美工作 –

+0

@ShoaibSharoon请接受为答案,以便其他人可以看到:) – clearshot66

相关问题