i have two data and each data has 4 images that i want to show one by one, i want to swap the image by clicking button using onclick javascript. but it doesn't work well to all data, just work to end of data. this is my code ;
<?php
include "koneksi.php"; // connect to database
?>
<html>
<head>
<title></title>
<style>
img{
height:100px;
width:100px;
}
</style>
</head>
<body>
<table>
<?php
$result=mysqli_query($koneksi, "SELECT * FROM tbbarang where idbarang='43' or idbarang='44'");
$i= 1;
while ($row=mysqli_fetch_array($result))
{ ?>
<tr>
<!--image 1--> <td><img id="img<?php echo $i?>" src=" <?php echo $row['gmbr1'] ?>"></td> <!-- image that i want to swap with folowing images 1, image 2, image 3, image 4-->
<!--image 2--> <td><img src="<?php echo $row['gmbr2'] ?>"></td>
<!--image 3--> <td><img src="<?php echo $row['gmbr3'] ?>"></td>
<!--image 4--> <td><img src="<?php echo $row['gmbr4'] ?>"></td>
<script>
function changeImage(id) {
var image = document.getElementById("img"+id);
if (image.src.match("<?php echo $row['gmbr1']?>")) {
image.src = "<?php echo $row['gmbr2']?>";
} else if (image.src.match("<?php echo $row['gmbr2']?>")) {
image.src = "<?php echo $row['gmbr3']?>";
} else if (image.src.match("<?php echo $row['gmbr3']?>")) {
image.src = "<?php echo $row['gmbr4']?>";
} else if (image.src.match("<?php echo $row['gmbr4']?>")) {
image.src = "<?php echo $row['gmbr1']?>";
}
}
</script>
<td><button id="ubah" onClick="changeImage(<?php echo $i?>)">Click To Change</button></td>
</tr>
<?php
$i=$i+1;
}
?>
</table>
</body>
</html>
your file may contain no of changeimage functions change your code like this