Script to move to next image

I am writing a script to display images on my site. The images are stored on the server and the source (path) is on a mySQL database. I want to be able to move to the next (or previous picture) by invoking two php functions I have made to do so. The code I have written so far is:

<?php require "db_connection.php"; $query="SELECT source FROM photos"; $result=mysql_query($query); $count=0; $numberofpics=mysql_num_rows($result); $image=mysql_result($result, $count, "source"); $num=1; function slideshowForward() { $num=$num+1; if($num==($numberofpics+1)) { $num=1; } $count=$count+1; $image=mysql_result($result, $count, "source"); } function slideshowBack() { $num=$num-1; if ($num==0) { $num=$numberofpics; } $count=$count-1; $image=mysql_result($result, $count, "source"); } ?>

The html portion to display the images is:

I'm pretty sure that the php script is incrementing/decrementing the count currently for the image, but I think the problem might be because the html (specifically img src="....") part is not re-evaluated when the count of the image increases?

Hi, first of all, please use the # or php buttons to put your code inside of code tags. This is written in the IMPORTANT: message that you used to create your thread.

When you create a variable in PHP, it will not be kept on the next page load. To get around this you could specify the $count inside of the URL:

[php]if(isset($_GET[‘pic’])) {
$count = $_GET[‘pic’];
} else {
$count = 0;
}[/php]

Then your links would be based upon count:

[php]

[/php]

But a more efficient way of doing this would be to request that particular image from MySQL:

[php]if(isset($_GET[‘pic’])) {
$count = mysql_real_escape_string($_GET[‘pic’]);
} else {
$count = 0;
}

// This line gets the photo at a certain position
$query = "SELECT source FROM photos LIMIT " . $count . “,1”;

$result = mysql_query($query);

// Check a picture was found at that position
if(mysql_num_rows($query) == 1) {
$image = mysql_result($result, 0, “source”);
} else {
die(‘Image not found!’);
}[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service