ShanjayG
ShanjayG

Reputation: 1023

PHP, retrieving specific image from an album as a thumbnail for that particular album

  $albums = get_albums_gallery();  
    foreach($albums as $album){
    $album_id = $album['id'];
    echo $album_id;   

    $images = get_images_gallery($album_id);
    echo '<p>',$album['event'],'(', $album['count'],'images) <br>
    ',$album['description'],'...<br>
    / 
    </p><img src="admin/uploads/',
$images['album'],'/',$images['id'],'.',
$images['ext'],'"title="Uploaded',
date(' DM Y/h:i',$images['timestamp']),
'"alt="hello" height="100px" width="100px"/>
    '
    ;
    }

The "img" above is supposed to display one image from inside an album as a thumbnail for that album that is looped. However the $images['album'],$mages['id'] etc all seemed to result as an undefined index. I have already included the necessary connection files. Code for get_albums_gallery()

function get_albums_gallery(){

$albums = array();

$albums_query = mysql_query("
SELECT `albums`.`album_id`, `albums`.`timestamp`,`albums`.`event`,`albums`.`name`,        LEFT(`albums`.`description`,50) as `description`, COUNT(`images`.`image_id`) as   `       image_count`
FROM `albums`
LEFT JOIN `images` 
ON `albums`.`album_id` = `images`.`album_id`
GROUP BY `albums`.`album_id`
");     

while($albums_row = mysql_fetch_assoc($albums_query)){
$albums[] = array(
'id' => $albums_row['album_id'],
'timestamp' => $albums_row['timestamp'],
'name' => $albums_row['name'],
'description' => $albums_row['description'],
'count' => $albums_row['image_count'],
'event' => $albums_row['event']
);
}  
return $albums;
}

and code for get_images_gallery()

function get_images_gallery($album_id){
$album_id = (int)$album_id;
$images = array();

$image_query = mysql_query("SELECT `image_id`, `album_id`,`timestamp`,`ext` FROM      `images` WHERE `album_id`=$album_id");
while($images_row = mysql_fetch_assoc($image_query)){
$images[] = array(
'id' =>   $images_row['image_id'],
'album'=>  $images_row['album_id'],
'timestamp'=> $images_row['timestamp'],
'ext' =>  $images_row['ext']
);
}
return $images;
}

I dont know where I did wrong as I've already passed a valid $album_id parameter in get_images_gallery($album_id). the line containing $album['event'] is working fine until it reaches the img tag and I get an undefined index error. Thanks in advance

Upvotes: 0

Views: 190

Answers (1)

Soundz
Soundz

Reputation: 1300

I tell you what went wrong. You return an array containing arrays with your get_images_gallery function. You itterate over the albums array with your foreach and ONLY over the albums so what you could do to make your code work is this:

</p><img src="admin/uploads/',
$images[0]['album'],'/',$images[0]['id'],'.',
$images[0]['ext'],'"title="Uploaded',
date(' DM Y/h:i',$images[0]['timestamp']),
'"alt="hello" height="100px" width="100px"/>

If this isn't giving you the desired output you have to itterate over the images inside the albums loop again

Upvotes: 1

Related Questions