Retrieving Multiple Entries Consecutively from DB

This has been driving me crazy, I need to select a specific row then select the next two rows with specific conditions.

What I have is a photo gallery, with photo ablums. Each photo album has an ID and each photo within the album has an ID as well. I want to show the first three photos within an album as a preview for the album. The problem is that I’m getting the same image for all three previews.

Also I redoing someone elses code which makes this seem harder.

Here’s the function that calls the photo:

public function getPhotos($album_id)

			{

				$sql = 'SELECT COUNT(photo_id) AS total_photos, photo_id, photo_ext, s_width, s_height,'.

						' photo_server_url FROM '.$this->CFG['db']['tbl']['photo'].

						' WHERE photo_album_id='.$this->dbObj->Param('photo_album_id').

						' AND photo_status='Ok' GROUP BY photo_album_id';

						

				$stmt = $this->dbObj->Prepare($sql);

				$rs = $this->dbObj->Execute($stmt, array($album_id));

				    if (!$rs)

					    trigger_error($this->dbObj->ErrorNo().' '.$this->dbObj->ErrorMsg(), E_USER_ERROR);

				

				$this->total_photos = 0;

				if($row = $rs->FetchRow())

					{				

						$this->photo_id = $row['photo_id'];

						$this->photo_ext = $row['photo_ext'];

						$this->s_width = $row['s_width'];

						$this->s_height = $row['s_height'];

						$this->photo_server_url = $row['photo_server_url'];

						$this->total_photos = $row['total_photos'];

						return true;

					}

			}

Ok now here’s where it’s called:

public function showAlbum()

			{

				$photos_folder = $this->CFG['media']['folder'].'/'.$this->CFG['admin']['photos']['folder'].'/';		

				while($row = $this->fetchResultRecord())

					{

?>

<tr>


<?php

			$photo_path = $this->photo_server_url.$photos_folder.getImageName($this->photo_id).$this->CFG['admin']['photos']['small_name'].'.'.$this->photo_ext;
			
            //$photo_id is what I want to increment by 1 to the next photo in the album with my specifications
            // Below is the skeleton of what I've been messing around with, I've tried while loops as well but haven't been able to come up with anything decent
?>

			<td id="PhotoGallery">
                   
                
                
               	<img src="<?php echo $photo_path;?>"/>
                
                
                <img src="<?php echo $photo_path;?>"/>
                
                 
                 <img src="<?php echo $photo_path;?>"/>                 
             </td>
</tr>

<?php

					}
     		}                    
 ?>

Sorry for the long post, but this has been driving me mad. If anyone can help it would be greatly appreciated.

Thanks,
Vijay

I can write a code that will do this, but since you are using another code to do this, I have no idea. I can see the problem though. It is with “Ok now here’s where it’s called:” code. It is in the echo requests where you are echoing the image source ($photo_path). That is the same variable and you never change that variable, over and over again so naturally it is going to show the same photo.

                  <img src="<?php echo $photo_path;?>"/>
                
                
                <img src="<?php echo $photo_path;?>"/>
                
                 
                 <img src="<?php echo $photo_path;?>"/>    

Yeah I know, I just left it like that to kind of show everybody what I want, I probably should have put a ++, so it made more sense. But if you know how to select one entry then the next with a condition I would like to see, because maybe I can adapt it to my code.

<?php
  $sql = "SELECT * FROM `photo_table` WHERE `AlbumID`='$album_id' ORDER BY `PhotoID` ASC LIMIT 3";
  $result = mysql_query($sql, $conn);

  $photo_source[0];
  $photo_source[1];
  $photo_source[2];

  $LoopCount = 0;
  while ($newArray = mysql_fetch_array($result)) {
    $photo_source[$LoopCount] = $newArray['photo_src'];
    $LoopCount++;
  }

  $LoopCount = 0;
  while ($LoopCount < 3) {
    echo "<img src="$photo_source[$LoopCount]" />";
    $LoopCount++;
  } 
?>

That will post the first three photos in the album.

Thank you very much, I’m going to integrate this into my code. I’ll let you know how it goes.

Sponsor our Newsletter | Privacy Policy | Terms of Service