How to make a slide gallery with a multidimensional array

hi i am trying to make a slide gallery with a multidimensional array but am finding it difficult to index it. am using for loop. i keep getting an undefined offset error message. here is my code.

 $query= "SELECT property_img FROM `property_img` where pro_id=:pro_id limit 9";
  $params=[':pro_id'=>$pro_id];

  $image=$db->getRows($query,$params);
   $count=Count($image);

<div id="myCarousel" class="carousel slide" data-ride="carousel">
      <div class="carousel-inner">
       <?php 
       var_dump($count);
        for($i=0;$i<=$count;++$i){
          $row=$image;
         
echo "<pre>";
var_dump($row);

      if($i==1){
                    
                        
                $slides = '<div class="item active">
                    <img src="admin/UploadFolder/'.$row[$i]['property_img'].'"/>
                    </div>';

            }
            else{ $slides = '<div class="item">
                    <img src="admin/UploadFolder/'.$row[$i]['property_img'].'"/>
                    </div>';}
                    
    }

thank you for your time.

Try this to see what values and format is returned:

<?php
//
//I  prefer using plural because there maybe more than one
$images=$db->getRows($query,$params);

var_dump($images);


// Edit:
// I prefer using this for loop
for($i=0;$i<$count;$i++) {
echo '<br>' .$i .' : ' .$count;
}

I would recommend using a foreach loop here instead of a for loop, as they’re easier conceptually.

Also please take some time to properly format your code, the way it was formatted makes me dizzy :confused:

Compare to this:

<?php
$images = $db->getRows(
    "SELECT property_img FROM `property_img` where pro_id=:pro_id limit 9",
    [':pro_id'=>$pro_id]
);
?>

<div id="myCarousel" class="carousel slide" data-ride="carousel">
    <div class="carousel-inner">
        <?php foreach ($images as $i => $image): ?>
            <div class="item<?= $i === 1 ? ' active' : ''; ?>">
                <img src="admin/UploadFolder/<?= $image['property_img']; ?>"/>
            </div>
        <?php endforeach; ?>
    </div>
</div>
2 Likes

In your loop you overwrite the varialble $slides on each loop so you will only get the last image.
Are you sure you did not intend the appeand the variable, either as an array ($slides[]) or string ($slides .=)?

it returned $i=0 and $count=the no of items to be looped over.

okay thanks noted.

thanks these printed the else statement .

This doesn’t look correct to me:

for($i=0;$i<=$count;++$i){

Also, I’m not sure why you do this inside the loop

$row=$image;

or at all, really, as you just seem to be copying one array into another, unless you use it for something else that we can’t see.

2 Likes

I am guessing that count =1 anyway try your loop because I think the first array $item is zero will not show because of ++$item iterator.

Incorrect. The ++$item iterator is always applied after the loop. People seem to be using ++$item instead of $item++ now because of some micro performance gain. I don’t see why we’d need to optimize on this level, but ok.

What will go wrong is $i<=$count, that should be $i < $count, otherwise for an array with 3 elements you will loop elements 0, 1, 2 and 3, but there is no element 3 (so: Notice: undefined index).

3 Likes

i fixed that. i made $i< count. thanks.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.