Reputation: 85
I need to create a 'wall' of images. There's my code:
<?php
$q = $db->prepare("SELECT * FROM wall");
$q->execute();
while($row = $q->fetch(PDO::FETCH_ASSOC)) { ?>
<div class="col-4-perc"><img src="<?=$site?>/assets/img/wall/<?=$row['source']?>.png"
alt="Foto wall" class="img-fluid"></div>
<?php } ?>
I would like to repeat the while loop repeating the photos in the database at least 3/4 times but I don't know how to do it.
Upvotes: 0
Views: 94
Reputation: 879
As explained in the comments, you have to use FetchAll
instead of fetch
in a while. Here is a example script for the result you want:
$q = $db->prepare("SELECT * FROM wall");
$q->execute();
$allImage = $q->fetchAll() ;
$limit = 100 ;
$activeKey = 0 ;
for ($i = 0; $i < $limit ;$i++){
if (!isset($allImage[$activeKey]['source'])) {
$activeKey = 0 ;
// if the line does not exist, reset to the first key (0)
}
echo "<div class='col-4-perc'><img src='{$site}/assets/img/wall/{$allImage[$activeKey]['source']}.png'
alt='Foto wall' class='img-fluid'></div>";
$activeKey++ ;
}
Images are saved in $allImage
, a table with numeric indexes. Next we just have to loop with a simple for using $limit
(the max image you want display).
And $activeKey
for the curent displayed image, if $allImage[$activeKey]
is not set, reset to 0
.
Upvotes: 2