Php 关联数组中的左联接
我想做一个图像库。有一些相册里面有图像,在索引页上我想显示一些相册,当你点击它时,你就会转到那个相册。我可以显示相册名称,但我想用随机函数显示这些相册中的图像。我希望你明白我想做什么。 问题是我无法接触到图像 我有两个关联数组,一个用于相册,一个用于图像。专辑中的那个看起来像这样Php 关联数组中的左联接,php,mysql,Php,Mysql,我想做一个图像库。有一些相册里面有图像,在索引页上我想显示一些相册,当你点击它时,你就会转到那个相册。我可以显示相册名称,但我想用随机函数显示这些相册中的图像。我希望你明白我想做什么。 问题是我无法接触到图像 我有两个关联数组,一个用于相册,一个用于图像。专辑中的那个看起来像这样 function get_albums() { $albums = array(); $albums_query = mysql_query(" SELECT `albums`.`album_id`, `albums
function get_albums() {
$albums = array();
$albums_query = mysql_query("
SELECT `albums`.`album_id`, `albums`.`timestamp`, `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`
")or die(mysql_error());
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']
);
}
return $albums;
}
function get_images($album_id) {
$album_id = (int)$album_id;
$images = array();
$image_query = mysql_query("SELECT `image_id`, `image_name`, `album_id`, `timestamp`, `ext` FROM `images` WHERE `album_id`=$album_id");
while ($images_row = mysql_fetch_assoc($image_query)) {
echo $images_row['image_id'];
$images[] = array(
'id' => $images_row['image_id'],
'img_name' => $images_row['image_name'],
'album' => $images_row['album_id'],
'timestamp' => $images_row['timestamp'],
'ext' => $images_row['ext']
);
}
return $images;
}
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'],
'img_name' => $albums_row['image_name'],
'ext' => $albums_row['ext']
);
$albums = get_albums();
if (isset($albums)) {
foreach ($albums as $album) {
echo '<a href="view_album.php?album_id=', $album["id"], '"><img src="uploads/thumbs/', $album["id"] ,'/', $album['img_name'],'.', $album["ext"],'" />', $album['name'], '</a>';
}
}
对于图像,它看起来是这样的
function get_albums() {
$albums = array();
$albums_query = mysql_query("
SELECT `albums`.`album_id`, `albums`.`timestamp`, `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`
")or die(mysql_error());
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']
);
}
return $albums;
}
function get_images($album_id) {
$album_id = (int)$album_id;
$images = array();
$image_query = mysql_query("SELECT `image_id`, `image_name`, `album_id`, `timestamp`, `ext` FROM `images` WHERE `album_id`=$album_id");
while ($images_row = mysql_fetch_assoc($image_query)) {
echo $images_row['image_id'];
$images[] = array(
'id' => $images_row['image_id'],
'img_name' => $images_row['image_name'],
'album' => $images_row['album_id'],
'timestamp' => $images_row['timestamp'],
'ext' => $images_row['ext']
);
}
return $images;
}
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'],
'img_name' => $albums_row['image_name'],
'ext' => $albums_row['ext']
);
$albums = get_albums();
if (isset($albums)) {
foreach ($albums as $album) {
echo '<a href="view_album.php?album_id=', $album["id"], '"><img src="uploads/thumbs/', $album["id"] ,'/', $album['img_name'],'.', $album["ext"],'" />', $album['name'], '</a>';
}
}
我认为在get_albums函数中,通过左连接函数,我可以通过添加图像名称和扩展名来访问图像,如下所示
function get_albums() {
$albums = array();
$albums_query = mysql_query("
SELECT `albums`.`album_id`, `albums`.`timestamp`, `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`
")or die(mysql_error());
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']
);
}
return $albums;
}
function get_images($album_id) {
$album_id = (int)$album_id;
$images = array();
$image_query = mysql_query("SELECT `image_id`, `image_name`, `album_id`, `timestamp`, `ext` FROM `images` WHERE `album_id`=$album_id");
while ($images_row = mysql_fetch_assoc($image_query)) {
echo $images_row['image_id'];
$images[] = array(
'id' => $images_row['image_id'],
'img_name' => $images_row['image_name'],
'album' => $images_row['album_id'],
'timestamp' => $images_row['timestamp'],
'ext' => $images_row['ext']
);
}
return $images;
}
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'],
'img_name' => $albums_row['image_name'],
'ext' => $albums_row['ext']
);
$albums = get_albums();
if (isset($albums)) {
foreach ($albums as $album) {
echo '<a href="view_album.php?album_id=', $album["id"], '"><img src="uploads/thumbs/', $album["id"] ,'/', $album['img_name'],'.', $album["ext"],'" />', $album['name'], '</a>';
}
}
然后像这样在索引页中使用它
function get_albums() {
$albums = array();
$albums_query = mysql_query("
SELECT `albums`.`album_id`, `albums`.`timestamp`, `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`
")or die(mysql_error());
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']
);
}
return $albums;
}
function get_images($album_id) {
$album_id = (int)$album_id;
$images = array();
$image_query = mysql_query("SELECT `image_id`, `image_name`, `album_id`, `timestamp`, `ext` FROM `images` WHERE `album_id`=$album_id");
while ($images_row = mysql_fetch_assoc($image_query)) {
echo $images_row['image_id'];
$images[] = array(
'id' => $images_row['image_id'],
'img_name' => $images_row['image_name'],
'album' => $images_row['album_id'],
'timestamp' => $images_row['timestamp'],
'ext' => $images_row['ext']
);
}
return $images;
}
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'],
'img_name' => $albums_row['image_name'],
'ext' => $albums_row['ext']
);
$albums = get_albums();
if (isset($albums)) {
foreach ($albums as $album) {
echo '<a href="view_album.php?album_id=', $album["id"], '"><img src="uploads/thumbs/', $album["id"] ,'/', $album['img_name'],'.', $album["ext"],'" />', $album['name'], '</a>';
}
}
$albums=get_albums();
如果(isset($albums)){
foreach($albums作为$album){
回声';
}
}
但结果是这样的;
我做错了什么?为什么不先进行一个大型查询,将所有数据连接起来,然后按自己的意愿显示出来,如下所示:
sql="SELECT * FROM albums a INNER JOIN images i ON a.album_id = i.album_id WHERE a.album_id =" . $album_id;
$result = mysql_query(sql);
while ($row = mysql_fetch_array($result)) {
echo '<a href="view_album.php?album_id=' . $row['album_id'] . '><img src="uploads/thumbs/' . $row['album_id'] . '/' . $row['album_id'] . '.' . $row['album_ext'] . '" />' . $row['album_name'] . '</a>';
}
sql=“从相册中选择*a.album\u id=i.album\u id,其中a.album\u id=”$相册编号;
$result=mysql\u查询(sql);
while($row=mysql\u fetch\u数组($result)){
回声';
}
然后,它应该准确地显示您希望它显示的内容
$albums\u query
没有img\u名称或ext字段。谢谢Patashu,非常简单,但我没有看到它。