使用PHP中的if语句更改backgroundcolor

时间:2016-10-26 19:10:48

标签: php

如果time2大于时间1,我希望背景颜色变为红色。 我在这方面是一个完全的菜鸟,我现在尝试了几天,但没有让它工作。 在CSS中我有.bgred {background: red;}

<?php
$sql = ("SELECT name, time1, time2 FROM myTable WHERE dates LIKE '".$_POST['dat']."' ");
$result = mysqli_query($conn, $sql);        

if (mysqli_num_rows($result) > 0) {     

    echo "<table action='' method='POST'><tr>
    <th>Name</th>
    <th>Time1</th>
    <th>Time2</th></tr>";

    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {

        $var1 = $row["time1"];
        $var2 = $row["time2"];
        if($var2 < $var1) {
            $bgred = 'class="bgred"';
        } else {
            $bgred = '';
        }

        echo "<tr><td>".$row["name"]."</td>
        <td>".$row["time1"]."</td>
        <td class="$bgred">".$row["time2"]."</td></tr>";
        }
    }

    echo "</table>";

mysqli_close($conn);
?> 

2 个答案:

答案 0 :(得分:1)

你并没有在任何地方回应$ bgred。

<?php
$sql = ("SELECT name, time1, time2 FROM myTable WHERE dates LIKE '".$_POST['dat']."' ");
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {

echo "<table action='' method='POST'><tr>
<th>Name</th>
<th>Time1</th>
<th>Time2</th></tr>";

// output data of each row
while($row = mysqli_fetch_assoc($result)) {     


    $var1 = $row["time1"];
    $var2 = $row["time2"];

//edited here
    if(strtotime($var2) < strtotime($var1)) {
        $bgred = 'bgred'; 
//done with edit      
    } else {
        $bgred = '';
    }

    echo "<tr><td class='$bgred'>".$row["name"]."</td>
    <td class='$bgred'>".$row["time1"]."</td>
    <td class='$bgred'>".$row["time2"]."</td></tr>";       
    }       
}       

echo "</table>";
mysqli_close($conn); ?>

答案 1 :(得分:0)

import re

s = "htmlhtl {% static path1 %} htmlhtml {% static path2/path3 %} htmlhtml " 

rx = re.compile(r'{% static (?P<path>\S+) %}')
# search for {% static ...%}

s = rx.sub(r'www.static.com/\g<path>', s)
print(s)
# htmlhtl www.static.com/path1 htmlhtml www.static.com/path2/path3 htmlhtml 

你有理由使用双引号吗?