Jump to content

[SOLVED] Simple photo gallery - Next / Prev buttons...


jsschmitt

Recommended Posts

The code I am using automatically builds the list based on a folder and it's contents. The issue is, the current next/prev buttons allow you to go to pages that don't exist. I have poked and proded at the code, but can't figure out what i did wrong...

 

To see the code in action: http://anomymage.com/tests/do/dep/galleries/engagements/index.php

This is the code I have:

 

        <?php
		$images = glob('*.jpg');
		$images = array_reverse($images);

		$imageIndex = isset($_REQUEST['index']) ? $_REQUEST['index'] : count($images)-1;
		echo '<img src="'.$images[$imageIndex].'" width="85%" alt="" /><br/><br/><br/><br/><br/>';
	?> 
        </div>
        
        <div id="leftnavgal">
        	<?php 
			 echo '<a href="?index='.($imageIndex+1).'">';

	?>
			Prev</a>
	</div>
    	<div id="rightnavgal">
        	<?php 
			echo '<a href="?index='.($imageIndex-1).'">';
		?>   
			Next</a>
	</div> 
</body>
</html>

 

Any help?

Your code does no checking to see if the image actually exists before displaying the previous/next buttons which is why you can navigate to images that don't exist.

 

Try putting in something that checks to see if the value being processed falls within the index range before showing the navigation buttons.

 

Solved! Thanks for the poke in the right direction!

      
<div id="leftnavgal">
<?php 
	if (count($images)-1 > $imageIndex){
	  echo '<a href="?index="'.($imageIndex+1).'">Prev</a>';}	
	else {
	  echo '';};
?>
</div>
<div id="rightnavgal">
       	<?php 
	if ($imageIndex > 0){
	  echo '<a href="?index='.($imageIndex-1).'">Next</a>';}
	else {
	  echo '';};
?>   
</div>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.