PHP,从专辑中检索特定图像作为该特定专辑的缩略图

时间:2013-07-29 02:05:44

标签: php html css

  $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"/>
    '
    ;
    }

上面的“img”应该从相册内部显示一个图像,作为循环播放的相册的缩略图。然而$ images ['album'],$ mages ['id']等似乎都是一个未定义的索引。我已经包含了必要的连接文件。 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;
}

和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;
}

我不知道我在哪里做错了,因为我已经在get_images_gallery($ album_id)中传递了有效的$ album_id参数。包含$ album ['event']的行正常工作,直到它到达img标记并且我得到一个未定义的索引错误。提前致谢

1 个答案:

答案 0 :(得分:1)

我告诉你出了什么问题。返回包含具有get_images_gallery函数的数组的数组。 您使用foreach并仅使用albums覆盖albums数组,以便使代码正常工作:

</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"/>

如果这不能提供所需的输出,则必须再次对albums循环内的图像进行迭代