【问题标题】:Next/Previous Image by user id and photo_id下一张/上一张图片(按用户 ID 和 photo_id)
【发布时间】:2012-07-13 08:55:33
【问题描述】:

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

所以我点击了我创建的专辑,其专辑 ID 为“22”,并且在点击 image243 后,我上传了图片 - image101、图片 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   
}}}
?>

【问题讨论】:

  • 再次问好同样的问题。根据我之前的回答,为您的 prev 和 next 查询添加一个 UNION 查询,以便提取的第一条记录是下一条,如果没有下一条,那么首先。
  • 我没有回答你..他们的代码有错误..它没有用!如果你回复了,我就不用再问了。以为我会让我的问题更深入!对你“Waygood”一点也不反对

标签: php mysql arrays image image-gallery


【解决方案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 检查 > 变为

【讨论】:

  • 你的下一个我刚刚得到你的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 '1UNION SELECT photo_id FROM userphotos WHERE photo_id
  • 啊! UNION 前需要一个空格
  • 刚刚测试过了。你需要 (SELECT...) UNION (SELECT...)
  • 先生,您是天才。我谢谢你。有用。就这么简单,需要什么!非常感谢您的所有时间和耐心。
【解决方案2】:

如果您的情况没有任何改变,不建议重复问同一个问题。但是,您已经在此处更好地记录了您的问题,因此这里有一个更清晰的答案,并附有示例,并展示了您必须如何处理它:

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

【讨论】:

  • 好的道指。我会先给这个。谢谢你。我知道发布两次不是最佳做法。所以我就继续这个。我很抱歉!我感谢您的所有帮助。你应该得到比你更多的代表。
  • photo.php?pid=0 给你那个错误?在这种情况下,试试 print_r($photos) 看看里面有没有东西。
  • 这是它打印的内容 -> 数组 ( [0] => 200 [1] => 201 [2] => 202 )
  • 在这种情况下, photo.php?pid=0 无法打印该错误。您做错了什么,或者总体上误解了 PHP。
  • 你能不能把它跑过去。看看是否一切都在正确的地方,它应该是怎样的?我已经用其他代码更新了顶部的代码!
【解决方案3】:

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

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

【讨论】:

    猜你喜欢
    • 2015-11-20
    • 2013-12-19
    • 1970-01-01
    • 1970-01-01
    • 2013-07-27
    • 1970-01-01
    • 1970-01-01
    • 2016-09-07
    • 2013-04-14
    相关资源
    最近更新 更多