jeudi 14 janvier 2021

change html style with "if" condition PHP

I have mini site that displays site list I manage as a table, with expire date. I want to change color depending it's expire date. this is html code

<div class="col-lg table-responsive">
    <table class="table table-hover">
        <thead class="thead thead-dark">
        <tr>
            <th scope="col">#</th>
            <th scope="col">title</th>
            <th scope="col">date</th>
            <th scope="col">contact person</th>
            <th scope="col">phone</th>
            <th scope="col">status</th>
            <th scope="col" >action</th>
        </tr>
        </thead>
        <tbody>
        <?php
        $button_upd = '<button type="submit" class="btn btn-primary mb-2" name="update" id="update" data-toggle="modal" data-target="#exampleModal">change</button>';
        foreach ($res as $rem) {
            $expire = (strtotime($rem[2]) / 86400) - (strtotime("now") / 86400);
            echo  "<tr>
                <td>" . $rem[0] . "</td>
                <td>" . $rem[1] . "</td>
                <td>" . $rem[2] . "</td>
                <td>" . $rem[3] . "</td>
                <td>" . $rem[4] . "</td>
                <td>" . $rem[5] . "</td>
                <td>" . $button_upd . "</td>                   
                </tr>";
        }
        ?>
        </tbody>
    </table>
</div>

This code takes data from database and puts it in the table, now I want to change row color (all columns separately of course) with php if condition. I have this code

    <?php
if($expire <= 30){
    echo 'expiring';
}
elseif($expire <= 0){
    echo 'expired';
}
else {
    echo 'active';
}
?>

and finally some css

.expiring{
background-color: yellow;
color: black;
}
.expired{
    background-color: red;
    color: white;
}
.active{
    background-color: green;
    color: white;
}

now as I understand I need to implement my php code into HTML, as it than takes it into cycle and displays table with right colors. can someone help with this? I can't figure out how to put this if condition in my HTML code.

Aucun commentaire:

Enregistrer un commentaire