这是我的代码
<?php
$query= "SELECT album_name FROM gallery group by album_name order by MIN(date_time)";
$stmt = $connection->prepare($query);
$stmt->execute();
$result=$stmt->get_result();
while ($row = $result->fetch_array(MYSQLI_BOTH)) { //First while loop
$album_names =htmlspecialchars($row['album_name']);`
?>
<ul>
<li data-tags="<?php echo $album_names ?>">
直到这一点,一切正常,它显示了我的数据库中所有相册的名称。
我接下来要做的是我希望它显示属于$album_names
的所有图片所以我在上面的代码之后编写了这段代码
// Code continues
<?php
$query2= "SELECT imgage_path,resized_path FROM gallery WHERE album=?";
$stmt = $connection->prepare($query2);
$stmt->bind_param("s",$album_names);
$stmt->execute();
$result2=$stmt->get_result();
while ($row2 = $result2->fetch_array(MYSQLI_BOTH)) { //Second while loop
$thumbnail_path = =htmlspecialchars($row2['resized_path']);
$image_path =htmlspecialchars($row2['image_path']);
?>
<a href="<?php echo $image_path ?>">
<img src="<?php echo $thumbnail_path ?>" alt="Illustration" />
</a>
</li>
</ul>
<?php
} //Second While loop ends
} //First While loop ends
?>
问题是它只显示$image_path
和$thumbnail_path
的第一个结果,但我希望它显示属于该特定相册的所有图像路径。
希望我已经解决了我的问题。
答案 0 :(得分:1)
这看起来像伪代码(因为它没有正确的php标签)。无论如何,问题如下..你的html标签li
安排是错误的。这就是为什么你没有看到其他结果。它必须修复如下
<?php
$query= "SELECT album_name FROM gallery group by album_name order by MIN(date_time)";
$stmt = $connection->prepare($query);
$stmt->execute();
$result=$stmt->get_result();
while ($row = $result->fetch_array(MYSQLI_BOTH)) { //First while loop
$album_names =htmlspecialchars($row['album_name']);`
?>
<ul>
<li data-tags="<?php echo $album_names ?>">
<?php
$query2= "SELECT imgage_path,resized_path FROM gallery WHERE album=?";
$stmt = $connection->prepare($query2);
$stmt->bind_param("s",$album_names);
$stmt->execute();
$result2=$stmt->get_result();
while ($row2 = $result2->fetch_array(MYSQLI_BOTH)) { //Second while loop
$thumbnail_path = =htmlspecialchars($row2['resized_path']);
$image_path =htmlspecialchars($row2['image_path']);
?>
<a href="<?php echo $image_path ?>">
<img src="<?php echo $thumbnail_path ?>" alt="Illustration" />
</a>
<?php
} //Second While loop ends
?>
</li>
</ul>
<?php
} //First While loop ends
?>
答案 1 :(得分:1)
简化为单个查询。这假设image_path和resized path一直都存在于每个图像中(如果它们不容易修复,但这显示了基础)。
<?php
// Query gets one row per album. Row has 3 fields, album name, then all the image_path fields concatentated together, the all the resized_path fields joined together
$query= "SELECT album_name
GROUP_CONCAT(image_path ORDER BY image_path) AS image_path_concat,
GROUP_CONCAT(resized_path ORDER BY image_path) AS resized_path_concat
FROM gallery
GROUP BY album_name
ORDER BY MIN(date_time)";
$stmt = $connection->prepare($query);
$stmt->execute();
$result=$stmt->get_result();
// Start of unordered list of album names
echo "<ul>";
while ($row = $result->fetch_array(MYSQLI_BOTH))
{
$album_names = htmlspecialchars($row['album_name']);
// List item of album name
echo "<li data-tags='$album_names'>";
// Explode out the image paths into an array
$image_path = explode(',', $row['image_path_concat']);
// Explode out the image resized paths into an array
$resized_path = explode(',', $row['resized_path_concat']);
// Start of unordered list of image paths
echo "<ul>";
// Loop around the array of image paths
foreach($image_path AS $key=>$value)
{
// Assuming that the list of image paths always match the array of resized paths then use the key of the current
// image_path for both the image path and also the resized / thumbnail path
$image_path = htmlspecialchars($image_path[$key]);
$thumbnail_path = htmlspecialchars($resized_path[$key]);
echo "<li><a href='".$image_path."'><img src='".$thumbnail_path."' alt='Illustration' /></a></li>";
}
// End of unordered list of image paths
echo "</ul>";
// End of list item of album name
echo "</li>";
} //First While loop ends
// End of unordered list of album names
echo "</ul>";
?>
答案 2 :(得分:0)
我不知道我的回答是否会对你有帮助。
但我认为你的html标签循环有错误。
<?php
$query2= "SELECT imgage_path,resized_path FROM gallery WHERE album=?";
$stmt = $connection->prepare($query2);
$stmt->bind_param("s",$album_names);
$stmt->execute();
$result2=$stmt->get_result();
while ($row2 = $result2->fetch_array(MYSQLI_BOTH)) { //Second while loop
$thumbnail_path = =htmlspecialchars($row2['resized_path']);
$image_path =htmlspecialchars($row2['image_path']);
?>
<a href="<?php echo $image_path ?>">
<img src="<?php echo $thumbnail_path ?>" alt="Illustration" />
</a>
</li> <- you loop the close <li> tag
</ul> <- you loop the close <ul> tag
<?php
} //Second While loop ends
} //First While loop ends
?>
&#13;
您在第二个循环中循环关闭标记(li和ul),但是您没有为li和ul放置开放标记。 我希望这有帮助。三江源