javascript - How to make a modal popup button for all the images I have uploaded?
560
I want to make a popup modal button for all my images, but only the first works and I know that I have to add someforeach
code, but I don't know how.
<div id="myModal" >
<span >×</span>
<img id="img01">
</div>
<div >
<div >
<?php
require_once 'classes/dbh.php';
if (isset($_GET['entry_id'])) {
$entry_id = $_GET['entry_id'];
$query = mysqli_query($conn, "SELECT * FROM images WHERE entry_id='$entry_id'");
if ($query->num_rows > 0) {
while ($row = $query->fetch_assoc()) {
$dirname = 'uploads/' . $row["file_name"];
echo '<img onclick="showImage(this)" id="myImg" src="' . $dirname . '" />';
}
}
}
?>
</div>
</div>
<script>
var modal = document.getElementById('myModal');
var img = document.getElementById('myImg');
var modalImg = document.getElementById("img01");
img.onclick = function () {
modal.style.display = "block";
modalImg.src = this.src;
}
var span = document.getElementsByClassName("close")[0];
// When the user clicks on <span> (x), close the modal
span.onclick = function () {
modal.style.display = "none";
}
</script>
I expect to work for all with aforeach
code, but I don't know how.
Answer
Solution:
You need to implement the showImage function
Use the bootstrap framework. It will help you to start