我已经创建了一个下一个和前一个图像链接,这是很好的工作。但我希望它们都循环回到开头或结尾。假设我有image101,图像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>";
}
?>
发布于 2012-07-12 13:03:41
为什么不在PHP中这样做呢?您可以只获取照片ID,并使用MySQL:
$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>";
只是为了解释一下,我们使用modulo来获得正确的下一页。获取上一页不言而喻:如果我们低于零,则使用照片的$total数量。
在数据库方面,以这种方式使用它比所有这些大于小于的查询要高效得多。
发布于 2012-07-12 11:28:38
您可以添加指向第一个图像的链接,如下所示:
$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-12 11:32:23
这将为您提供下一个条目,也将提供第一个条目,这意味着如果没有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 ";
然后在前面的do a UNION with ORDER BY photo_id DESC
https://stackoverflow.com/questions/11450736
复制