获取上一个和下一个按钮ID

时间:2014-10-18 22:57:27

标签: php

我正在使用此脚本在图片之间移动:

$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 ( [0] => 13 [1] => 14 [2] => 15 [3] => 16 )

问题:

上一页和下一页没有正确显示。

For Instance:

如果我在图像13上,PREV按钮移动到图像12(图像12属于另一个文件夹),NEXT移动到图像2(而不是14)

我哪里错了?

1 个答案:

答案 0 :(得分:0)

您没有将查询结果$photos用于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>";