I have a database table that stores image details. From this I want to get a exactly 6 images (as strings only) and display the images as background images on figure tags.
Where I am struggling is to get 6 image paths even if there are only 4 in the DB. In this instance I would like to go back around at fetch the first two rows again. Additionally if there are 10 rows from the query, I would like to randomly select only 6.
So far I have...
Functions file
// Slideshow images (Homepage)
function get_slideshow() {
global $conn;
$sql = "SELECT id, caption, image, page_id
FROM slideshow
ORDER BY id DESC";
$stmt = $conn->prepare($sql);
$stmt->execute();
$result = $stmt->get_result();
$stmt->free_result();
$stmt->close();
return $result;
}
Index.php
$slideshow = get_slideshow();
<?php
if ($slideshow->num_rows > 0) {
while($row = $slideshow->fetch_assoc()) {
?>
<figure style="background-image:url(<?php echo '/photos/'.$row['image']; ?>)"></figure>
<?php
}
}
?>
Any help would be great. Thanks in advance :)