Click here to Skip to main content
15,513,571 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello I want to change the background color based on the date. something like this here
this code changes the colors when the date is 30/4/2022
$date_now = new DateTime();
$date_time_koningsdag = new DateTime("2022-4-30");
if($date_now == $date_time_koningsdag){
    ?>
    <div class="flag" style="height: 130px; width: 100%;">
    <div class="orange" style="height: 25%; background-color: orange;"></div>
    <div class="red" style="height: 25%; background-color: red;"></div>
    <div class="white" style="height: 25%; background-color: white;"></div>
    <div class="blue" style="height: 25%; background-color: blue;"></div>

    </div>
    <?php
}else{
   ?>
    <div class="flag" style="height: 130px; width: 100%;">
    <div class="orange" style="height: 25%; background-color: green;"></div>
    <div class="red" style="height: 25%; background-color: purple;"></div>
    <div class="white" style="height: 25%; background-color: pink;"></div>
    <div class="blue" style="height: 25%; background-color: yellow;"></div>

    </div>
   <?php
}
?>


now I also want to have this for my background but that is in a section tag. and that section tag is inside of a while loop
Maybe I'm thinking to difficult about this but how would I do it when it's like that??
include "config.php";

        $sql  = 'SELECT * FROM broodjes';
        $stmt = $conn->prepare($sql); 
        $stmt->execute();
        $result = $stmt->get_result(); // get the mysqli result
        
        while($row = mysqli_fetch_assoc($result)){
            echo '        <section class="py-5">
            <div class="container px-4 px-lg-5 mt-5">
                <div class="row gx-4 gx-lg-5 row-cols-2 row-cols-md-3 row-cols-xl-4 justify-content-center">
                    <div class="col mb-5">
                        <div class="card h-100">
                            <!-- Product image dit kan later nog wel-->
                            <div class="fw-image">
                                <img class="card-img-top" src="brood.webp" alt="..." />
                            </div>
                            <!-- Product details-->
                            <div class="card-body p-4">
                                <div class="text-center">
                                    <div class="test1">
                                    <input type="hidden" name="broodjes_ID" value='. $row['broodjes_ID'] . '/>
                                    </div>
                                    <!-- Product name-->
                                    <div class="test3">
                                        <input type="hidden" name="broodnaam" value='. $row['broodnaam'] . '/>
                                        <h5 class="fw-bolder">'.$row['broodnaam']. '</h5>
                                    </div>
                                    <!-- Product price-->
                                    <div class="test2">
                                        <input type="hidden" name="broodnaam" value='. $row['prijs'] . '/>
                                        <h3> Prijs:  </h3>   <h3 class="fw-price"> €'.$row['prijs']. '</h3><br>
                                    </div>
                                    <!--voorraad--> 
                                    '; 
                                    if($row['voorraad'] == 0){
                                        echo '<h3 class="fw-voorraad" style="color: red;"> uitverkocht</h3><br>'; 
                                    }else{
                                        //link die misschien kan helpen https://www.withinweb.com/info/a-shopping-cart-using-php-sessions-code/
                                        $broodjes = $row['broodjes_ID'];
                                        echo ' Voorraad: '.$row['voorraad'].'<br>
                                        </div>
                                        </div>
                                        <!-- Product actions-->
                                        
                                        <form action="cart.php?broodjes_ID='. $broodjes.'" method="POST" name="broodjes"  value=""
                                            <div class="card-footer p-4 pt-0 border-top-0 bg-transparent">
                                                <div class="text-center"><button class="btn btn-outline-dark mt-auto shop-item-button" type="submit" name="add_to_cart">Bestellen</button></div>
                                            </div>
                                        </form>
                                    '; 
                                }
                                
                        echo '</div>
                    </div>
                </div>
            </div>
        </section>';                           

        
        }


the styling
CSS
<style>
         section{
            background-color: orange;
        }
</style>


What I have tried:

I looked it up but then I get then people tell me to do it like I did it in the first example that I showed. but my section is in a while loop that creates the styling
Posted
Comments
Gerry Schmitz 9-Mar-22 12:03pm    
If your "loop" is generating invalid markup, then you need to fix it. There is no way around it.
Maciej Los 11-Mar-22 15:21pm    
Try to debug your code to find out what's wrong. See: How to debug Php code? - Stack Overflow[^]
Rebecca2002 14-Mar-22 8:13am    
sorry but what do you guys mean? the loops work? and the codes work I just wanna know how I will put this styling on the section tag that is inside the while loop?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900