下一个图像链接循环

时间:2014-10-15 09:43:01

标签: php mysqli

我正试图让我的下一个图像循环播放,以便它们返回到图像列表的开头。我能够让图像通过列表直到最后一个图像,然后我的最后一个图像显示链接上没有PID。

我有

 <div id="photolistholder">
 <?php if(isset($_GET['pid'])){?>
 <?php
    //Now we'll get the list of the specified users photos
    $var =$_GET['pid'];
    $sql = "SELECT * FROM coverphotos WHERE coverphoto_id='$var'";
    $query = mysqli_query($mysqli,$sql)or die(mysqli_error($mysqli));
    while($photo = mysqli_fetch_array($query)){
       $user=rawfeeds_user_core::getuser($photo['coverphoto_userid']);
 ?>
    <p class="frontpage_description"><a href="profile.php?username=<?php echo $user['username']; ?>">
 <?php 
    $id=$_SESSION['id'];
    if($user['id']){
       echo "<img border=\"0\" src=\"userimages/cropped".$user['id'].".jpg\" onerror='this.src=\"img/no_profile_img.jpeg\"' width=\"40\" ><br/>".$user['fullname']."'s</a> Album: </p>";
    }else{
       echo "<img border=\"0\" src=\"userimages/cropped".$id.".jpg\" onerror='this.src=\"img/no_profile_img.jpeg\"' width=\"40\" ><br/>".$user['fullname']." </a></p>";
    }

    $id=$_SESSION['id'];

    $var = $_GET['pid'] ;
    $photo_sql = "SELECT coverphoto_id FROM coverphotos WHERE (coverphoto_id < ".$var." OR coverphoto_id > ".$var.") AND coverphoto_userid = ".$user['id']." ORDER BY coverphoto_id DESC LIMIT 1";
    $photo_query = mysqli_query($mysqli,$photo_sql)or die(mysqli_error($mysqli));
    $photo_prev=mysqli_fetch_array($photo_query);

    if($photo_prev>1){
        echo"<a href='coverimagephoto.php?pid=".$photo_prev['coverphoto_id']."'>Previous</a> | ";
    }else{
        echo " ";
    }


    $var = $_GET['pid'] ;   
    $photo_sqls = "SELECT coverphoto_id FROM coverphotos WHERE (coverphoto_id > ".$var." OR coverphoto_id < ".$var.") AND coverphoto_userid = ".$user['id']." ORDER BY coverphoto_id ASC LIMIT 1";
    $photo_querys = mysqli_query($mysqli,$photo_sqls)or die(mysqli_error($mysqli));
                    $photo_next=mysqli_fetch_array($photo_querys);

    if($photo_next>1){
       echo "<a href='coverimagephoto.php?pid=".$photo_next['coverphoto_id']."'>Next</a>";  
    }else{
       echo "";
    }

    $user1_id=@$_SESSION['id'];
    $user2_id=$user['id'];
    if($user1_id==@$_SESSION['id']){ 
       echo " | <a href='include/photo_delete.php?pid=".$photo['coverphoto_id']."'>Delete</a>";
    }else{
       echo"";
    }
 ?>
    <center>
 <?php
        //echo "<div class='photo_captionholder'><b>Name</b> : ".$photo['photo_name']." | <b>Caption</b> : ".$photo['photo_caption']." |                
        //<b>Uploaded</b> : ".$photo['photo_datetime']."</div>";
    echo "<img id=\"coverimage\" border=\"0\" src='coverimages/".$photo['coverphoto']."?".time()."' onerror='this.src=\"coverimages/nocover.png\"'>";

    }}
 ?>
        </body>

我尝试使用

$var = $_GET['pid'] ;
    $photo_sql = "SELECT coverphoto_id FROM coverphotos WHERE (coverphoto_id > ".$var." OR coverphoto_id < ".$var.") AND coverphoto_userid = ".$user['id']."";
    $photo_query = mysqli_query($mysqli,$photo_sql)or die(mysqli_error($mysqli));
    $photo_next=mysqli_fetch_array($photo_query);

    if($photo_next>1){
        echo"<a href='coverimagephoto.php?pid=".$photo_prev['coverphoto_id']."'>Next</a> | ";
    }else{
            echo " ";
    }

但链接只是在两张图片之间跳转。如果用户删除图像,图像不在1,2,3,4,5中,它们可以是1,2,4,5,它可以在图像PID 3或5上开始。

2 个答案:

答案 0 :(得分:1)

编码和理解是否更简单,并且从N + 1个查询到1个查询减少查询(往返服务器和返回),如果您刚使用了第一个查询,则N =(照片数量)然后将所有结果提取到一个数组中,然后处理该数组

快速模型:

<div id="photolistholder">
<?php 
   if( isset($_GET['pid']) ) {

       // validate the pid
       $_GET['pid'] = some_validation_please($_GET['pid']);
       $id=$_SESSION['id'];

       $sql = "SELECT * FROM coverphotos WHERE coverphoto_id='$_GET['pid']'";

       $result = mysqli_query($mysqli,$sql) or die(mysqli_error($mysqli));

       $photos = array();

       while($row = mysqli_fetch_array($result)){
          $photos[] = $row;
       }

       foreach( $photos as $idx => $photo ) {

          // add all your image html with the prev and next links
          // using $idx-1 for prev image
          // using $idx+1 for next image
          // and checking if $idx = 0 for first image 
          //   so you can use count($photos)-1 to know you are on last image
          //   so you use photos[0] as the next image to get continuous loop
          // and if $idx == 0 next image is count($photos)-1

       }
 ?>

抱歉,我没有时间从浏览器的内容中挑出骨头,因为我一定会犯一个愚蠢的错误并为之烦恼。

答案 1 :(得分:0)

我找到了一种更好的方法。只需使用UNION

$var = mysqli_escape_string($_GET['pid']) ;
$photo_sql = "(SELECT coverphoto_id FROM coverphotos WHERE  coverphoto_id < ".$var."  AND coverphoto_userid = ".$user['id']." AND album=1 ORDER BY coverphoto_id DESC LIMIT 1)";
$photo_sql.= " UNION (SELECT coverphoto_id FROM coverphotos WHERE  coverphoto_id > ".$var."  AND coverphoto_userid = ".$user['id']." AND album=1 ORDER BY coverphoto_id DESC LIMIT 1)";
$photo_query = mysqli_query($mysqli,$photo_sql)or die(mysqli_error($mysqli));
$photo_prev=mysqli_fetch_array($photo_query);

if($photo_prev>1){
    echo"<a href='coverimagephoto.php?pid=".$photo_prev['coverphoto_id']."'>Previous</a> | ";
}else{
        echo " ";
}



$var = $_GET['pid'] ;   
$photo_sqls = "(SELECT coverphoto_id FROM coverphotos WHERE  coverphoto_id > ".$var." AND coverphoto_userid = ".$user['id']." AND album=1 ORDER BY coverphoto_id ASC LIMIT 1)";
$photo_sqls.= " UNION (SELECT coverphoto_id FROM coverphotos WHERE  coverphoto_id < ".$var."  AND coverphoto_userid = ".$user['id']." AND album=1 ORDER BY coverphoto_id ASC LIMIT 1)";
$photo_querys = mysqli_query($mysqli,$photo_sqls)or die(mysqli_error($mysqli));
                $photo_next=mysqli_fetch_array($photo_querys);

if($photo_next>1){
echo "<a href='coverimagephoto.php?pid=".$photo_next['coverphoto_id']."'>Next</a>"; 
}else{
        echo " ";
}