php - PHP图库相册
问题描述
我正在编写一个 php 脚本,它从数据库中检索图像并在相册内的页面上显示它们,每个相册包含几个图像......
这是以下代码的示例:
$sql = "SELECT * FROM tbl_album where status='process' ORDER BY albumid DESC LIMIT $start_from, 12";
<?php
$rs_result = mysql_query ($sql,$con);
while ($row = mysql_fetch_assoc($rs_result))
{
$aid=$row['albumid'];
$aimage=$row['image'];
$aname=$row['name'];
$astatus=$row['status'];
echo '<div class="col-lg-4 col-md-6 col-sm-6 agile_gallery_grid">';
echo '<div class="hover ehover14">';
?>
<?php
echo"<a href='#details?d=$aid' onclick='showfunction()'>";
echo "<img src='admin/acatch/$aimage' class='img-responsive' alt='$aname' width='200' height='200' data-picture_id='$aid'>";
echo'</a>';
}
?>
此代码显示数据库中包含的所有专辑,但我想要做的是,当我单击专辑封面时,我想在同一页面上显示其中的图像,但我无法传递专辑 ID到同一页面上的另一个 php 脚本执行此操作,如果有人可以帮助,请提前谢谢你
解决方案
看看下面的代码:
<?php
$sql = "SELECT * FROM tbl_album where status='process' ORDER BY albumid DESC LIMIT $start_from, 12";
$rs_result = mysql_query($sql, $con);
while($row = mysql_fetch_assoc($rs_result)):
$aid=$row['albumid'];
$aimage=$row['image'];
$aname=$row['name'];
$astatus=$row['status'];
?>
<div class="col-lg-4 col-md-6 col-sm-6 agile_gallery_grid">
<div class="hover ehover14">
<a href='#details?d=$aid' onclick='showfunction()'>
<img src="admin/acatch/<?=$aimage?>" class="img-responsive" alt="<?=$aname?>" width="200" height="200" data-picture_id="<?=$aid?>">
</a>
</div>
</div>
<div class="gallery_album" id="gallery_album_<?=$row['albumid']?>">
<?php
/**
* now collect all images associated with each album by using $aid
*/
$sql_images = "SELECT * FROM tbl_album_images WHERE fk_album_id = $aid";
$images_result = mysql_query($sql_images, $con);
while($image = mysql_fetch_assoc($images_result)):
?>
<div class="gallery_album_image"><img src="<?=$image['dir_and_filename']?>"></div>
<?php
endwhile;
?>
</div>
<?php endwhile; ?>
我们在这里所做的就是像你们一样gettign所有的专辑。但是我们在第一个循环中引入了一个子查询while
来获取每个专辑的所有相关图像。
我们将这些图像放入div
带有唯一专辑 ID 的新容器中,您可以showfunction()
使用 CSS 和 javascript 来显示或隐藏实际专辑,从而相应地显示或隐藏。
OBS!警告!!您的 mysql 查询,以及我提供的查询,非常非常糟糕!
查看使用 phpPDO
或mysqli
...