Loop for w3.css cards

I am working on a project using w3.css, am having some issues with creating a card within a w3 responsive third element. Is there a way where I could create an additional row for when there is already three third columns in the row?

This is for like a product view card, how do i make use of a loop to create rows with 3 cards populated with items from my database and there after start another row to populate 3 more items?

     <div class="w3-container w3-row">
                            <?php
                            foreach ($products as $index => $product) :
                                ?>
                                <div class="w3-third w3-card w3-white w3-container" style="width:30%">
                                    <div class="w3-container">
                                        <img src="../images/<?= $product[prodImg] ?>" >
                                        <div class='w3-container'>
                                            <p class="w3-small">
                                                <strong><?= $product[prodName] ?></strong>
                                                <strong>Price:  </strong><?= $product[price] ?>
                                                <input type="text" name="quantity" id="quantity<?= $product[productid] ?>" class="form-control w3-input" value="1" />
                                                <button type="button" class="w3-btn w3-green" data-prodId="<?= $product[productid] ?>" data-price="<?= $product[price] ?>" data-name="<?= $product[prodName] ?>" data-img="../images/<?= $product[prodImg] ?>" class="btn btn-success add_to_cart">Add To Cart</button>
                                            </p>
                                        </div>
                                    </div>
                                </div>
                            <?php endforeach; ?>
                        </div>

Current output:

<div class="w3-container w3-row">
      <div class="w3-third w3-card w3-white w3-container" style="width:30%">
           <div class="w3-container">
                <img src="../images/kueypng.jpeg" style="display:block; height:15vw; width:15vw; margin-left: auto; margin-right: auto">
                     <div class="w3-container">
                          <p class="w3-small">
                          <strong>chicken rice</strong>
                          <strong>Price:  </strong>$2.50                                                
                          <input type="text" name="quantity" id="quantity13" class="form-control w3-input" value="1">
                         <button type="button" class="w3-btn w3-green" data-prodid="13" data-price="$2.50" data-name="chicken rice" data-img="../images/kueypng.jpeg">Add To Cart</button>
                         </p>
                     </div>
                 </div>
            </div>
                                                                
                            <div class="w3-third w3-card w3-white w3-container" style="width:30%">
                                            <div class="w3-container">
                                                <img src="../images/test.png" style="display:block; height:15vw; width:15vw; margin-left: auto; margin-right: auto">
                                                <div class="w3-container">
                                                    <p class="w3-small">
                                                        <strong>duck rice</strong>
                                                        <strong>Price:  </strong>3.00                                                <input type="text" name="quantity" id="quantity14" class="form-control w3-input" value="1">
                                                        <button type="button" class="w3-btn w3-green" data-prodid="14" data-price="3.00" data-name="duck rice" data-img="../images/test.png">Add To Cart</button>
                                                    </p>
                                                </div>
                                            </div>
                                        </div>
         <div class="w3-third w3-card w3-white w3-container" style="width:30%">
                                            <div class="w3-container">
                                                <img src="../images/test.png" style="display:block; height:15vw; width:15vw; margin-left: auto; margin-right: auto">
                                                <div class="w3-container">
                                                    <p class="w3-small">
                                                        <strong>mee goreng</strong>
                                                        <strong>Price:  </strong>3.00                                                <input type="text" name="quantity" id="quantity15" class="form-control w3-input" value="1">
                                                        <button type="button" class="w3-btn w3-green" data-prodid="15" data-price="3.00" data-name="mee goreng" data-img="../images/test.png">Add To Cart</button>
                                                    </p>
                                                </div>
                                            </div>
                                        </div>
            <div class="w3-third w3-card w3-white w3-container" style="width:30%">
                                            <div class="w3-container">
                                                <img src="../images/test.png" style="display:block; height:15vw; width:15vw; margin-left: auto; margin-right: auto">
                                                <div class="w3-container">
                                                    <p class="w3-small">
                                                        <strong>maggi goreng</strong>
                                                        <strong>Price:  </strong>3.00                                                <input type="text" name="quantity" id="quantity16" class="form-control w3-input" value="1">
                                                        <button type="button" class="w3-btn w3-green" data-prodid="16" data-price="3.00" data-name="maggi goreng" data-img="../images/test.png">Add To Cart</button>
                                                    </p>
                                                </div>
                                            </div>
                                        </div>
               <div class="w3-third w3-card w3-white w3-container" style="width:30%">
                                            <div class="w3-container">
                                                <img src="../images/test.png" style="display:block; height:15vw; width:15vw; margin-left: auto; margin-right: auto">
                                                <div class="w3-container">
                                                    <p class="w3-small">
                                                        <strong>egg prata</strong>
                                                        <strong>Price:  </strong>1.50                                                <input type="text" name="quantity" id="quantity18" class="form-control w3-input" value="1">
                                                        <button type="button" class="w3-btn w3-green" data-prodid="18" data-price="1.50" data-name="egg prata" data-img="../images/test.png">Add To Cart</button>
                                                    </p>
                                                </div>
                                            </div>
                                        </div>

Expected Output:

    <div class="w3-container w3-row">
          <div class="w3-third w3-card w3-white w3-container" style="width:30%">
               <div class="w3-container">
                    <img src="../images/kueypng.jpeg" style="display:block; height:15vw; width:15vw; margin-left: auto; margin-right: auto">
                         <div class="w3-container">
                              <p class="w3-small">
                              <strong>chicken rice</strong>
                              <strong>Price:  </strong>$2.50                                                
                              <input type="text" name="quantity" id="quantity13" class="form-control w3-input" value="1">
                             <button type="button" class="w3-btn w3-green" data-prodid="13" data-price="$2.50" data-name="chicken rice" data-img="../images/kueypng.jpeg">Add To Cart</button>
                             </p>
                         </div>
                     </div>
                </div>
    
                                <div class="w3-third w3-card w3-white w3-container" style="width:30%">
                                                <div class="w3-container">
                                                    <img src="../images/test.png" style="display:block; height:15vw; width:15vw; margin-left: auto; margin-right: auto">
                                                    <div class="w3-container">
                                                        <p class="w3-small">
                                                            <strong>duck rice</strong>
                                                            <strong>Price:  </strong>3.00                                                <input type="text" name="quantity" id="quantity14" class="form-control w3-input" value="1">
                                                            <button type="button" class="w3-btn w3-green" data-prodid="14" data-price="3.00" data-name="duck rice" data-img="../images/test.png">Add To Cart</button>
                                                        </p>
                                                    </div>
                                                </div>
                                            </div>
             <div class="w3-third w3-card w3-white w3-container" style="width:30%">
                                                <div class="w3-container">
                                                    <img src="../images/test.png" style="display:block; height:15vw; width:15vw; margin-left: auto; margin-right: auto">
                                                    <div class="w3-container">
                                                        <p class="w3-small">
                                                            <strong>mee goreng</strong>
                                                            <strong>Price:  </strong>3.00                                                <input type="text" name="quantity" id="quantity15" class="form-control w3-input" value="1">
                                                            <button type="button" class="w3-btn w3-green" data-prodid="15" data-price="3.00" data-name="mee goreng" data-img="../images/test.png">Add To Cart</button>
                                                        </p>
                                                    </div>
                                                </div>
                                            </div>
</div>

<div class="w3-container w3-row">
      <div class="w3-third w3-card w3-white w3-container" style="width:30%">
           <div class="w3-container">
                <img src="../images/kueypng.jpeg" style="display:block; height:15vw; width:15vw; margin-left: auto; margin-right: auto">
                     <div class="w3-container">
                          <p class="w3-small">
                          <strong>chicken rice</strong>
                          <strong>Price:  </strong>$2.50                                                
                          <input type="text" name="quantity" id="quantity13" class="form-control w3-input" value="1">
                         <button type="button" class="w3-btn w3-green" data-prodid="13" data-price="$2.50" data-name="chicken rice" data-img="../images/kueypng.jpeg">Add To Cart</button>
                         </p>
                     </div>
                 </div>
            </div>

                            <div class="w3-third w3-card w3-white w3-container" style="width:30%">
                                            <div class="w3-container">
                                                <img src="../images/test.png" style="display:block; height:15vw; width:15vw; margin-left: auto; margin-right: auto">
                                                <div class="w3-container">
                                                    <p class="w3-small">
                                                        <strong>duck rice</strong>
                                                        <strong>Price:  </strong>3.00                                                <input type="text" name="quantity" id="quantity14" class="form-control w3-input" value="1">
                                                        <button type="button" class="w3-btn w3-green" data-prodid="14" data-price="3.00" data-name="duck rice" data-img="../images/test.png">Add To Cart</button>
                                                    </p>
                                                </div>
                                            </div>
                                        </div>
         <div class="w3-third w3-card w3-white w3-container" style="width:30%">
                                            <div class="w3-container">
                                                <img src="../images/test.png" style="display:block; height:15vw; width:15vw; margin-left: auto; margin-right: auto">
                                                <div class="w3-container">
                                                    <p class="w3-small">
                                                        <strong>mee goreng</strong>
                                                        <strong>Price:  </strong>3.00                                                <input type="text" name="quantity" id="quantity15" class="form-control w3-input" value="1">
                                                        <button type="button" class="w3-btn w3-green" data-prodid="15" data-price="3.00" data-name="mee goreng" data-img="../images/test.png">Add To Cart</button>
                                                    </p>
                                                </div>
                                            </div>
                                        </div>
</div>

1 answer

  • answered 2021-11-29 07:57 Ammar Aslam

    You can count the number of cards that have been echoed and use the modulus operator to echo the div inside the loop after 3 iterations

     <?php
     $count = 0;
     foreach ($products as $index => $product) {
         if($count % 3 == 0) {
              if($count != 0) {
                  echo '</div>';
              }
              echo '<div class="w3-container w3-row">';
         }
     ?>
         <div class="w3-third w3-card w3-white w3-container" style="width:30%">
             <div class="w3-container">
                 <img src="../images/<?= $product[prodImg] ?>" >
                 <div class='w3-container'>
                     <p class="w3-small">
                         <strong><?= $product[prodName] ?></strong>
                         <strong>Price:  </strong><?= $product[price] ?>
                         <input type="text" name="quantity" id="quantity<?= $product[productid] ?>" class="form-control w3-input" value="1" />
                         <button type="button" class="w3-btn w3-green" data-prodId="<?= $product[productid] ?>" data-price="<?= $product[price] ?>" data-name="<?= $product[prodName] ?>" data-img="../images/<?= $product[prodImg] ?>" class="btn btn-success add_to_cart">Add To Cart</button>
                     </p>
                 </div>
             </div>
          </div>
      <?php 
         $count++;
      }
      ?>
    
      </div>
    

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum