0

So i learned about for loop, do while, for each but i just cant figure out how to write the code so an loop would automatically create the informations i did now manually (just copy & Pasta )

See Code below

My Question: -) How to put thru a loop ? I maybe have to create an Matrix array or how is called -) how to include the img link in the array and insert it in the html code with loop

<?php

// Available, Name , Model,  Price, Rent Location, 


$car1 = array(
    "available" => "Available",
    "name" => "Duster",
     "model" =>"Nissan", 
     "price" => 333,
     "location" => "Hollywood");

     $car2 = array(
        "available" => "Available",
        "name" => "Panzer",
         "model" =>"Bundesheer", 
         "price" => 98753,
         "location" => "Kaserne");

         $car3 = array(
            "available" => "NOT Available",
            "name" => "Hubschrauber",
             "model" =>"Pew Pew", 
             "price" => 98756,
             "location" => "Dschungel");

             $car4 = array(
                "available" => "Available",
                "name" => "Schiff",
                 "model" =>"Öltanker", 
                 "price" => 223123213132,
                 "location" => "Baltisches Meer");

     echo '
     
     <div class="card" style="width: 18rem;">
  <img class="card-img-top" src="https://cdn.pixabay.com/photo/2018/09/15/12/33/nissan-3679299_1280.jpg" alt="Card image cap">
  <div class="card-body">
    <h5 class="card-title">'.$car1["name"]. ' </h5>
    <p class="card-text">Model: '.$car1["model"]. '<br> Preis: '.$car1["price"]. '<br>Location: '.$car1["location"]. '</p>
    <p class="card-text"><b>'.$car1["available"]. '</b></p>
    <a href="#" class="btn btn-primary">Order NOW</a>
  </div>
</div>
     
     
     ';

     echo '
     
     <div class="card" style="width: 18rem;">
  <img class="card-img-top" src="https://cdn.pixabay.com/photo/2020/06/22/10/24/lost-places-5328687_1280.jpg" alt="Card image cap">
  <div class="card-body">
    <h5 class="card-title">'.$car2["name"]. ' </h5>
    <p class="card-text">Model: '.$car2["model"]. '<br> Preis: '.$car2["price"]. '<br>Location: '.$car2["location"]. '</p>
    <p class="card-text"><b>'.$car2["available"]. '</b></p>
    <a href="#" class="btn btn-primary">Order NOW</a>
  </div>
</div>
     
     
     ';

     echo '
     
     <div class="card" style="width: 18rem;">
  <img class="card-img-top" src="https://cdn.pixabay.com/photo/2016/03/27/07/38/dangerous-1282330_1280.jpg" alt="Card image cap">
  <div class="card-body">
    <h5 class="card-title">'.$car3["name"]. ' </h5>
    <p class="card-text">Model: '.$car3["model"]. '<br> Preis: '.$car3["price"]. '<br>Location: '.$car3["location"]. '</p>
    <p class="card-text"><b>'.$car3["available"]. '</b></p>
    <a href="#" class="btn btn-primary">Order NOW</a>
  </div>
</div>
     
     
     ';

     echo '
     
     <div class="card" style="width: 18rem;">
  <img class="card-img-top" src="https://cdn.pixabay.com/photo/2016/03/07/12/40/tanker-1242111_1280.jpg" alt="Card image cap">
  <div class="card-body">
    <h5 class="card-title">'.$car4["name"]. ' </h5>
    <p class="card-text">Model: '.$car4["model"]. '<br> Preis: '.$car4["price"]. '<br>Location: '.$car4["location"]. '</p>
    <p class="card-text"><b>'.$car4["available"]. '</b></p>
    <a href="#" class="btn btn-primary">Order NOW</a>
  </div>
</div>
     
     
     ';
?>
  • 2
    If all of your source data was in 1 array (even `$cars = [$car1, $car2, $car3, $car4];` then have a read of [Loop through an array php](https://stackoverflow.com/questions/4414623/loop-through-an-array-php) – Nigel Ren Jul 13 '20 at 12:46

1 Answers1

3

You need to create a multidimensional array (array of arrays), more about arrays here

Once you have your array, you can iterate over it.

$cars = [
    [
        "available" => "Available",
        "name" => "Duster",
        "model" =>"Nissan",
        "price" => 333,
        "location" => "Hollywood"
    ],
    [
        "available" => "Available",
        "name" => "Schiff",
        "model" =>"Öltanker",
        "price" => 223123213132,
        "location" => "Baltisches Meer"
    ],
    [
        "available" => "NOT Available",
        "name" => "Hubschrauber",
        "model" =>"Pew Pew",
        "price" => 98756,
        "location" => "Dschungel"
    ],
    [
        "available" => "Available",
        "name" => "Panzer",
        "model" =>"Bundesheer",
        "price" => 98753,
        "location" => "Kaserne"
    ],
];

foreach ($cars as $car) {
    echo $car['available'];
}
meewog
  • 1,690
  • 1
  • 22
  • 26
  • 2
    thanks very much . with this 2 lines of code (foreach and then how to choose the keys) you explained everything to me and i fully understood and have it working properly now :D even the img was then easy to insert. thank you! – Tomerson Jefferson Jul 13 '20 at 13:06