First of all, this is one of my first ventures into more extensive JavaScript so bear with me.
I am trying to set up a simple image gallery script. It only displays one full image at a time and it has next and previous buttons on top of it. It pulls the image out of a database and I have it setup where the image name is something like “picture.php?catid=1&num=12”. The num variable is what specifies which picture to pull from the database. So if the page is currently displaying image 12, the next button needs to point to 13 and the previous to 11. Also, if it’s the first picture I need for the previous button to be hidden and the next button hidden if it’s the last picture. What would be the best way to do this?
I’ve tried to set it up with the following code but I’m having problems getting the onclick actions to change on the links:
[CODE]
<div id=”pictures”>
<div id=”buttons”>
<a onClick=”return showPic(0)” href=”#” id=”prevlink”><img src=”images/prevbutton.jpg” border=”0″ id=”prev” style=”visibility:hidden;”></a>
<a onClick=”return showPic(1)” href=”#” id=”nextlink”><img src=”images/nextbutton.jpg” border=”0″ id=”next”></a>
</div>
<p><img src=”picture.php?catid=1&num=0″ id=”imageplaceholder”></p>
</div>
Here’s the JavaScript:
[CODE]
function showPic (whichpic) {
if (document.getElementById) {
document.getElementById(‘imageplaceholder’).src = ‘picture.php?catid=1&num=’ + whichpic;
if (whichpic > 0) {
document.getElementById(‘prev’).style.visibility = ‘visible’;
}
if (whichpic < [php_image_count] ?>) {
document.getElementById(‘next’).style.visibility = ‘visible’;
}
if (whichpic == [php_image_count] ) {
document.getElementById(‘next’).style.visibility = ‘hidden’;
}
if (whichpic == 0) {
document.getElementById(‘prev’).style.visibility = ‘hidden’;
}
var newprev = whichpic – 1;
var newnext = whichpic + 1;
document.getElementById(‘prevlink’).onclick = ‘return showPic(‘ + newprev + ‘)’;
document.getElementById(‘nextlink’).onclick = ‘return showPic(‘ + newnext + ‘)’;
return false;
} else {
return true;
}
}
Thanks for any help!