簡體   English   中英

如果數量列的數值小於或等於10,如何更改表格行的背景顏色

[英]How to change background color of table row if quantity column has numeric value less than or equal to 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; }  

在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; ?> >

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM