I've got an image gallery with a main image and 5 thumbnails under it, when you click on a thumbnail the image changes and this works fine but I have also put in two arrows either side to scroll through the images as well and it kind of works but it's skipping every second image. I tried putting in alerts to see what is going on and they're being fired twice.
My HTML is as follows:
<tr id="product_main_image">
<td colspan="5">
<img src="product_images/catalog.png" id="1">
<span id="image_left" style="bottom: 233px;"><img src="web/left_arrow.png"></span>
<span id="image_right" style="bottom: 233px;"><img src="web/right_arrow.png"></span>
</td>
</tr>
<tr id="product_thumbnail">
<td><img src="product_images/catalog.png" id="1"></td>
<td><img src="product_images/config.png" id="2"></td>
<td><img src="product_images/customers.png" id="3"></td>
<td><img src="product_images/marketing.png" id="4"></td>
<td><img src="product_images/sales.png" id="5"></td>
</tr>
My JS is as follows:
$("#product_thumbnail img").on({
mouseover: function(){
$(this).css({'cursor': 'pointer'});
},
mouseout: function(){
$(this).css({'cursor': 'default'});
},
click: function(){
var imageURL = $(this).attr('src');
var imageID = $(this).attr('id');
$("#product_main_image > td > img").attr('src', imageURL);
$("#product_main_image > td > img").attr('id', imageID);
}
});
$("#image_left").on({
mouseover: function(){
$(this).css({'cursor': 'pointer'});
},
mouseout: function(){
$(this).css({'cursor': 'default'});
},
click: function(){
var curImageID = $("#product_main_image img").attr('id');
curImageID--;
var imageURL = $("#"+curImageID).attr('src');
alert (imageURL);
$("#product_main_image > td > img").attr('src', imageURL);
$("#product_main_image > td > img").attr('id', curImageID);
}
});
$("#image_right").on({
mouseover: function(){
$(this).css({'cursor': 'pointer'});
},
mouseout: function(){
$(this).css({'cursor': 'default'});
},
click: function(){
var curImageID = $("#product_main_image img").attr('id');
curImageID++;
var imageURL = $("#"+curImageID).attr('src');
alert (imageURL);
$("#product_main_image > td > img").attr('src', imageURL);
$("#product_main_image > td > img").attr('id', curImageID);
}
});
Edit
How my HTML is being generated from php
$sql = mysqli_query($con, "SELECT * FROM product_images WHERE product='$product_id'");
$imageCount = mysqli_num_rows($sql);
if ($imageCount > 0) {
$i = 0;
while($row = mysqli_fetch_array($sql)){
$image = $row["image"];
$i++;
$gallery .= "<td><img src='product_images/$image' data-id='$i'></td>";
}
}
$sql = mysqli_query($con, "SELECT * FROM product_images WHERE product='$product_id' LIMIT 1");
$imageCount = mysqli_num_rows($sql);
if ($imageCount > 0) {
while($row = mysqli_fetch_array($sql)){
$first_image = $row['image'];
$main_image .= "<img src='product_images/$first_image' data-id='1'>";
}
}
My actual HTML
<table id="gallery">
<tr id="product_main_image">
<td colspan='5'>
<?php echo $main_image; ?>
<span id="image_left"><img src="web/left_arrow.png"></span>
<span id="image_right"><img src="web/right_arrow.png"></span>
</td>
</tr>
<tr id="product_thumbnail">
<?php echo $gallery; ?>
</tr>
</table>