0

我创建了一个很好的下一个和上一个图像链接。但我希望他们都循环回到开头或结尾。所以说我有 image101、image 202 和 image243,一旦我点击 image243,它就会回到 image101。

我想和以前一样,当我点击 Image101 时,我希望链接返回到 image243。(自我解释)。

我该怎么做呢?

<?php   $sql = "SELECT id FROM albums WHERE user_id=".$user['id']." ORDER BY id ASC LIMIT 1";   $query = mysql_query($sql)or die(mysql_error());    

while($album = mysql_fetch_array($query)){ ?>


                    <?php
                $photo_sql = "SELECT photo_id FROM userphotos WHERE photo_id > ".$_GET['pid']." AND photo_ownerid = ".$user['id']." AND album_id=".$album['id']." ORDER BY photo_id ASC LIMIT 1 ";

                $photo_query = mysql_query($photo_sql)or die(mysql_error());
                $photo_next=mysql_fetch_array($photo_query);



echo "<a href='photo.php?pid=".$photo_next['photo_id']."'>Next</a>";



                $photo_sql = "SELECT photo_id FROM userphotos WHERE photo_id < ".$_GET['pid']." AND photo_ownerid = ".$user['id']." AND album_id=".$album['id']." ORDER BY photo_id DESC LIMIT 1 ";

                $photo_query = mysql_query($photo_sql)or die(mysql_error());
                $photo_prev=mysql_fetch_array($photo_query);

                    echo " | <a href='photo.php?pid=".$photo_prev['photo_id']."'>Prev</a>";

}


?>
4

3 回答 3

1

为什么不从 PHP 中做到这一点?无需使用所有这些多余的 MySQL 查询,您只需获取照片 ID 并使用 PHP:

$photo_sql = "SELECT photo_id FROM userphotos WHERE photo_ownerid = ".$user['id']." AND album_id=".$album['id']." ORDER BY photo_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);

$current = 2; // whatever your position is in the photo array

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

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

echo "<a href='photo.php?pid=".$next."'>Next</a>";
echo " | <a href='photo.php?pid=".$prev."'>Prev</a>";

只是为了解释一下,我们使用模数来获得正确的下一页。获取上一页不言自明:如果我们低于零,请使用 $total 数量的照片。

在数据库方面,以这种方式使用它比所有这些大于小于查询的查询更有效。

于 2012-07-12T13:03:40.843 回答
0

您可以像这样添加到第一张图片的链接:

$photo_sql = "SELECT MIN(photo_id) FROM userphotos WHERE photo_ownerid = ".$user['id']
$photo_query = mysql_query($photo_sql)or die(mysql_error());
$photo_first =mysql_fetch_array($photo_query);
echo "<a href='photo.php?pid=".$photo_first['photo_id']."'>First</a>";

当然,您可以使用 MAX 获取最后一张照片。

于 2012-07-12T11:28:37.957 回答
0

这将为您提供下一个条目,但也是第一个条目,这意味着如果没有 NEXT,它将给出第一个条目。

$photo_sql = "SELECT photo_id FROM userphotos WHERE photo_id > ".$_GET['pid']." AND photo_ownerid = ".$user['id']."  ORDER BY photo_id ASC LIMIT 1 ";
$photo_sql. = "UNION SELECT photo_id FROM userphotos WHERE photo_ownerid = ".$user['id']." ORDER BY photo_id ASC LIMIT 1 ";

然后在之前用 ORDER BY photo_id DESC 做一个 UNION

于 2012-07-12T11:32:23.497 回答