0

I'm using this script to move between images:

$photo_sql = "SELECT * FROM images WHERE image_folder_id = $folder_id ORDER BY image_id ASC";
$photo_result = mysql_query($photo_sql) or die(mysql_error());
while ($row = mysql_fetch_array($photo_result)) {
    $photos[] = $row[0];
}
$total = mysql_num_rows($photo_result);
// Testing example:
// $photos = array(200, 202, 206, 211);
// $total = count($photos);

$current = $_GET['image_id']; // whatever your position is in the photo array

if ($current > ($total-1) || $current < 0) {
    echo "Error: nonexistent photo ID.";
    exit;
}

echo '<img src="images/'.$photos[$current].'.png" alt="my image!" />'; // display current photo

$next = ($current+1) % $total; // modulo
$prev = (($current-1) < 0) ? $total-1 : $current -1;

echo "<a href='image.php?type=image&image_id=".$next."'>Next</a>";
echo " | <a href='image.php?type=image&image_id=".$prev."'>Prev</a>";

// print_r($photos);

Array is correct, I have for exemple:

Array ( [0] => 13 [1] => 14 [2] => 15 [3] => 16 )

Problem:

Prev and Next not are correctly displayed.

For Instance:

If I'm on image 13, PREV button move to image 12 (image 12 belong another folder) and NEXT move to image 2 (instead 14)

Where am I going wrong?

Hid Dencum
  • 582
  • 4
  • 21
  • [**Please, don't use `mysql_*` functions in new code**](http://stackoverflow.com/q/12859942). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://uk.php.net/manual/en/function.mysql-connect.php)? Learn about [*prepared statements*](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide which. – Arnold Daniels Oct 18 '14 at 23:12

1 Answers1

0

You're not using the query result $photos for next/prev.

echo '<img src="images/'.$photos[$current].'.png" alt="my image!" />'; // display current photo

$next = ($current+1) % $total; // modulo
$prev = (($current-1) < 0) ? $total-1 : $current -1;

echo "<a href='image.php?type=image&image_id=".$photos[$next]."'>Next</a>";
echo " | <a href='image.php?type=image&image_id=".$photos[$prev]."'>Prev</a>";
Arnold Daniels
  • 16,516
  • 4
  • 53
  • 82