title: Next/Previous Image by user id and photo_id
body: <p>I have created an next and prev image link which works great. But I'd like them both to loop back to the beginning or the end when the user hits their last uploaded image in their given album. </p>
  
  <p>So I've clicked into my created album which has an album id of '22' and I have my uploaded images - image101, image 202 and image243 uploaded inside, once I hit image243. I'd like it to return to the albums first image image101.</p>
  
  <p>I'd like to do the same with previous, when I hit Image101 I'd like the link to go back to image243. (Self explanitory).</p>
  
  <p>Each User has their own albums which they create with their uploaded images inside with various photo_id's dependant on how many images the given user has uploaded. So its not in a 1,2,3,4 order.
  If I've just uploaded a new image to the website, it will create a photo_id of 244 as that is the next available photo_id and if another user in another account uploads an image straight after me under their account, their photo will have a photo_id of 245. 
  The below code is what I have so far, and the next and prev jumps to the next users uploaded image missing out any uploaded images from other user accounts. I just need the next and previous not to hit a blank page after the last image.</p>
  
  <p>I hope I've explained this clearly without giving anyone a headache. Happy to answer any questions if I can get abit of help/advise or guidence. </p>
  
  <p>Thanks</p>
  
  <p><em><strong>Code used for selecting next/prev user image within the users set album</em></strong></p>
  
  <pre><code> &lt;?php if(isset($_GET['pid'])){ ?&gt;
  &lt;?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']);
  ?&gt;
  &lt;p class="frontpage_description"&gt;&lt;a href="profile.php?username=&lt;?php echo $user['username']; ?&gt;"&gt;
  &lt;?php 
  $id=$_SESSION['id'];
  if($id==$_SESSION['id']){
  echo "&lt;img border=\"0\" src=\"imgs/cropped".$id.".jpg\" onerror='this.src=\"img/no_profile_img.jpeg\"' width=\"40\" &gt;";
  }
              ?&gt;
  
  &lt;?php echo $user['fullusersname'];?&gt;&lt;/a&gt;&lt;/p&gt;
  &lt;?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)){ ?&gt;
  
  &lt;?
  $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 &gt; ($total-1) || $current &lt; 0) {
      echo "Error: nonexistent photo ID.";
      exit;
  }
  echo "&lt;div class='photo_captionholder'&gt;&lt;b&gt;Name&lt;/b&gt; : ".$photo['photo_name']." | &lt;b&gt;Caption&lt;/b&gt; : ".$photo['photo_caption']."&lt;/div&gt;";
      echo "&lt;img class='viewer_image' alt='".$photo['photo_name']."' width='60%'  src='";
      echo "include/media.userimage.php?pid=".$photos[$current];
      echo "'\"&gt;"; // display current photo
  
  $next = ($current+1) % $total; // modulo
  $prev = (($current-1) &lt; 0) ? $total-1 : $current -1;
  
  echo "&lt;a href='photo.php?pid=".$next."'&gt;Next&lt;/a&gt;";
  echo " | &lt;a href='photo.php?pid=".$prev."'&gt;Prev&lt;/a&gt;";
  ?&gt;
  &lt;?php   
  }}}
  ?&gt;
  </code></pre>