yooooooooooo
yooooooooooo

Reputation: 75

Foreach not showing all items in multidimensional array

This is my first question in a long time, any help is greatly appreciated!

I've got one database storing vehicles and one database storing their images. I am using an INNER JOIN to grab a list of vehicles and their images. After the database query, I put the return into an array; so 2 arrays in 1 array:

return array($vehicles, $vehicle_images);

When I do a print_r I get the correct return:

<?php print_r($your_listings[0]); ?>
<br />
<?php print_r($your_listings[1]); ?>

Returns:

Array
(
    [0] => Array
        (
            [vehicle_id] => 35
            [vehicle_type] => jeep
            [vehicle_vin] => 6969
            [owner_email] => [email protected]
            [vehicle_make] => Jeep
            [vehicle_year] => 2008
            [vehicle_model] => cherokee
        )

    [1] => Array
        (
            [vehicle_id] => 36
            [vehicle_type] => motorcycle
            [vehicle_vin] => 1234
            [owner_email] => [email protected]
            [vehicle_make] => honda
            [vehicle_year] => 2018
            [vehicle_model] => random
        )

    [2] => Array
        (
            [vehicle_id] => 39
            [vehicle_type] => atv
            [vehicle_vin] => 3215
            [owner_email] => [email protected]
            [vehicle_make] => Yamaha
            [vehicle_year] => 1990
            [vehicle_model] => OHYEA
        )

)

Array
(
    [0] => Array
        (
            [vehicle_id] => 35
            [image_display] => placeholder
        )

    [1] => Array
        (
            [vehicle_id] => 36
            [image_display] => /new/images/vehicles/users/42/image.jpg
        )

    [2] => Array
        (
            [vehicle_id] => 36
            [image_display] => /new/images/vehicles/users/42/vehicle1.jpg
        )

    [3] => Array
        (
            [vehicle_id] => 35
            [image_display] => /new/images/vehicles/users/42/vehicle.jpg
        )

    [4] => Array
        (
            [vehicle_id] => 39
            [image_display] => placeholder
        )

)

Now when I do a foreach (including bootstrap 4 styling), it only shows 2 vehicles instead of 3; the 2 vehicles it shows appear to be showing exactly as I want them:

<div class="container-fluid">
 <div class="row no-gutters">
<?php
$your_listings = owner_listings($_SESSION['user']);

if (!($your_listings[0])) {
  echo '<div class="col-sm"><div class="alert alert-danger" role="alert"><i class="fas fa-exclamation"></i> You do not have any listings active at this time.</div></div>';
}
else {
 foreach ($your_listings as $i => $item) {
  $make = $your_listings[0][$i]['vehicle_make'];
  $model = $your_listings[0][$i]['vehicle_model'];
  $year = $your_listings[0][$i]['vehicle_year'];
  $vehicle = $your_listings[0][$i]['vehicle_id'];
  $image = $your_listings[1][$i]['image_display'];
    if ($image != 'placeholder') {
      echo '<div class="col-sm"><div class="card" style="width: 18rem;">
            <h5 class="card-title text-center font-weight-bold">'.$year.' '.$make.' '.$model.'</h5>
            <img class="card-img-top" src="'.$image.'" alt="'.$year.' '.$make.' '.$model.'">
            <div class="card-body">
              <a href="#" class="btn btn-primary edit_button" value="'.$vehicle.'" id="'.$vehicle.'" data-vehicle="'.$vehicle.'">Edit</a>
            </div>
            </div></div>';
          }
          else {
            if ($your_listings[0][$i]['vehicle_type'] == 'atv') {
              $image = '/new/images/vehicles/types/atv.png';
              }
            elseif ($your_listings[0][$i]['vehicle_type'] == 'jeep') {
              $image = '/new/images/vehicles/types/jeep.png';
              }
            elseif ($your_listings[0][$i]['vehicle_type'] == 'motorcycle') {
              $image = '/new/images/vehicles/types/motorchycle.png';
              }
            echo '<div class="col-sm"><div class="card" style="width: 18rem;">
            <h5 class="card-title text-center font-weight-bold">'.$year.' '.$make.' '.$model.'</h5>
            <img class="card-img-top" src="'.$image.'" alt="'.$year.' '.$make.' '.$model.'">
            <div class="card-body">
              <a href="#" class="btn btn-primary edit_button" value="'.$vehicle.'" id="'.$vehicle.'" data-vehicle="'.$vehicle.'">Edit</a>
            </div>
            </div></div>';
          }
        }
  }
  ?>

</div>
</div>

Have I just been staring at this too long? What am I doing wrong? Any help is appreciated.

Thanks!

Upvotes: 0

Views: 808

Answers (2)

Praveen Kumar
Praveen Kumar

Reputation: 2408

Give a try to this answer...

<div class="container-fluid">
    <div class="row no-gutters">
    <?php
        $your_listings = owner_listings($_SESSION['user']);

        if (!($your_listings[0])) 
        {
          echo '<div class="col-sm"><div class="alert alert-danger" role="alert"><i class="fas fa-exclamation"></i> You do not have any listings active at this time.</div></div>';
        }
        else 
        {
            $newarray = array();
            foreach($your_listings[0] as $i => $item)
            {
                $newarray[$item["vehicle_id"]] = $item["image_display"];
            }
            foreach ($your_listings[0] as $i => $item) 
            {
              $make = $item['vehicle_make'];
              $model = $item['vehicle_model'];
              $year = $item['vehicle_year'];
              $vehicle = $item['vehicle_id'];         
              $image = $newarray[$vehicle];
                if ($image != 'placeholder') 
                {
                    echo '<div class="col-sm"><div class="card" style="width: 18rem;">
                        <h5 class="card-title text-center font-weight-bold">'.$year.' '.$make.' '.$model.'</h5>
                        <img class="card-img-top" src="'.$image.'" alt="'.$year.' '.$make.' '.$model.'">
                        <div class="card-body">
                          <a href="#" class="btn btn-primary edit_button" value="'.$vehicle.'" id="'.$vehicle.'" data-vehicle="'.$vehicle.'">Edit</a>
                        </div>
                        </div></div>';
                }
                else 
                {
                    if ($item['vehicle_type'] == 'atv') {
                      $image = '/new/images/vehicles/types/atv.png';
                      }
                    elseif ($item['vehicle_type'] == 'jeep') {
                      $image = '/new/images/vehicles/types/jeep.png';
                      }
                    elseif ($item['vehicle_type'] == 'motorcycle') {
                      $image = '/new/images/vehicles/types/motorchycle.png';
                      }
                    echo '<div class="col-sm"><div class="card" style="width: 18rem;">
                    <h5 class="card-title text-center font-weight-bold">'.$year.' '.$make.' '.$model.'</h5>
                    <img class="card-img-top" src="'.$image.'" alt="'.$year.' '.$make.' '.$model.'">
                    <div class="card-body">
                      <a href="#" class="btn btn-primary edit_button" value="'.$vehicle.'" id="'.$vehicle.'" data-vehicle="'.$vehicle.'">Edit</a>
                    </div>
                    </div></div>';
                }
            }
        }
    ?>
    </div>
</div>

Upvotes: 1

Gaur93
Gaur93

Reputation: 695

You are looping original array which has two arrays as you said. What you want is to loop through only first element of your_listings array to get three vehicles

if (!($your_listings[0])) {
  echo '<div class="col-sm"><div class="alert alert-danger" role="alert"><i class="fas fa-exclamation"></i> You do not have any listings active at this time.</div></div>';
}
else {
foreach ($your_listings as $i => $item) {    // should be foreach ($your_listings[0] as $i => $item) {
$make = $item['vehicle_make'];
$model = $item['vehicle_model'];

Upvotes: 1

Related Questions