简体   繁体   中英

a href background-color change based on field value

I have a field called 'availability' that can be changed between 'Available' and 'UNavailable' for users to choose. If the user is available, I want to have a background of a green color; red is for UNavailable users.

$sql = $db_con->prepare("SELECT * FROM `users` WHERE username = '" . $_SESSION['username'] . "'");
                        $sql->execute();
                        while($row = $sql->fetch()){
                    ?>
            <tr>
                <td>
                    <?php if ($availability = 'UNavailable') { ?>
                      <a style="margin:10px;background-color:#FF2828;color:#fff;border-radius:25px;padding:10px;" href="avail/index.php" class="true_home"><?php echo $row['availability']; ?></a></td>
                      <?php
                    } else if ($availability = 'Available') { ?>
                        <a style="margin:10px;background-color:#008000;border-radius:25px;padding:10px;" href="avail/index.php" class="true_home"><?php echo $row['availability']; ?></a></td>
                        <?php
                    }
                }
            ?>

Whichever availability status I list first, the color always chooses that one. It ignores the else. I've used else if and just else to see if any changes would occur to allow the value to dictate the background color. Neither have worked.

As always, I truly value everyone's input.

i think this will help you

<style>
.Available {
    background-color:#008000;
}
.UNavailable {
    background-color:#FF2828;
}
</style>
<?php
$sql = $db_con->prepare("SELECT * FROM `users` WHERE username = '" . 
$_SESSION['username'] . "'");
$sql->execute();
while ($row = $sql->fetch())
{ ?>
<tr>
    <td>
    <a style="margin:10px;border-radius:25px;padding:10px;" href="avail/index.php" class="true_home <?php echo $availability ?>"><?php echo $row['availability']; ?></a>
    </td>
</tr><?php
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM