0

我创建了一个很好的下一个和上一个图像链接。但我希望他们在用户点击他们给定相册中最后上传的图片时都循环回到开头或结尾。

因此,我点击了我创建的专辑,其专辑 ID 为“22”,并且在点击 image243 后,我将上传的图片 - image101、image 202 和 image243 上传到里面。我希望它返回相册的第一张图片 image101。

我想对以前做同样的事情,当我点击 Image101 时,我希望链接返回到 image243。(自我解释)。

每个用户都有自己的相册,他们使用上传的图片创建相册,里面有各种 photo_id,具体取决于给定用户上传的图片数量。所以它不是1,2,3,4的顺序。如果我刚刚向网站上传了一张新图片,它将创建一个 244 的 photo_id,因为这是下一个可用的 photo_id,如果另一个帐户中的另一个用户在他们的帐户下直接在我之后上传了一张图片,他们的照片将有一个 photo_id 245。下面的代码是我到目前为止的代码,下一个和上一个跳转到下一个用户上传的图像,错过了其他用户帐户上传的任何图像。我只需要下一张和上一张不要在最后一张图片之后点击空白页。

我希望我已经清楚地解释了这一点,而不会让任何人头疼。如果我能得到帮助/建议或指导,很乐意回答任何问题。

谢谢

用于在用户设置相册中选择下一个/上一个用户图像的代码

 <?php if(isset($_GET['pid'])){ ?>
<?php
//Now we'll get the list of the specified users photos

$sql = "SELECT * FROM userphotos WHERE photo_id=".$_GET['pid'];
$query = mysql_query($sql)or die(mysql_error());
while($photo = mysql_fetch_array($query)){
$user=rawfeeds_user_core::getuser($photo['photo_ownerid']);
?>
<p class="frontpage_description"><a href="profile.php?username=<?php echo $user['username']; ?>">
<?php 
$id=$_SESSION['id'];
if($id==$_SESSION['id']){
echo "<img border=\"0\" src=\"imgs/cropped".$id.".jpg\" onerror='this.src=\"img/no_profile_img.jpeg\"' width=\"40\" >";
}
            ?>

<?php echo $user['fullusersname'];?></a></p>
<?php
//Now we'll get the list of the specified users photos

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


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

<?
$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);
print_r($photos);
// Testing example:
// $photos = array(200, 202, 206, 211);
// $total = count($photos);

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

if($current > ($total-1) || $current < 0) {
    echo "Error: nonexistent photo ID.";
    exit;
}
echo "<div class='photo_captionholder'><b>Name</b> : ".$photo['photo_name']." | <b>Caption</b> : ".$photo['photo_caption']."</div>";
    echo "<img class='viewer_image' alt='".$photo['photo_name']."' width='60%'  src='";
    echo "include/media.userimage.php?pid=".$photos[$current];
    echo "'\">"; // display current photo

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

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

3 回答 3

1

It is not recommended to ask the same question twice, if nothing has changed in your situation. You have documented your question better here, however, so here is a clearer answer for you, with an example, and showing how you have to handle it:

$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);

// Testing example:
// $photos = array(200, 202, 206, 211);
// $total = count($photos);

$current = $_GET['pid']; // 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='photo.php?pid=".$next."'>Next</a>";
echo " | <a href='photo.php?pid=".$prev."'>Prev</a>";
于 2012-07-13T09:11:55.453 回答
1

解释:

您的“下一个”查询获取当前图像之后的记录。(即记录 3 将得到 4,5,6)

您的代码从该结果中获得第一条记录。(即4)

将 UNION 查询添加到 PREVIOUS 记录然后将该记录放在查询结果的末尾。(即 4,5,6,1,2) 注意:暂时忽略 LIMIT

如果第一个 SELECT 没有返回记录,那么第一条记录实际上将位于结果的顶部(即,对于记录 6,next 为空,然后是 1,2,因此下一条记录为 1)

下一个

$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_sql.= " UNION (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_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_sql.= " UNION (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_id 检查 > 变为 <

于 2012-07-13T09:18:28.047 回答
0

为此,我写了一个查询。看看能不能根据自己的要求改

select
  users_id as ID,
  (select  users_id from users where users_id > ID limit 1) as NextID,
  (select  users_id from users where users_id < ID ORDER BY users_id DESC limit 1) AS PreviousID
from users
where users_id = 1
于 2012-07-13T09:15:08.697 回答