PHP MySQL while 循环内部,同时循环查询同一个表


php mysql while loop inside while loop querying same table

这是我的代码

<?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的第一个结果,但我希望它显示属于该特定相册的所有图像路径。

希望我已经解决了我的问题.

这对我来说看起来像伪代码(因为它没有正确的 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
?>

简化为单个查询。这假设每个图像始终存在image_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>";
?>
我不知道

我的答案是否会帮助你。

但我认为您的 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
?>

您在第二个循环中循环关闭标签(li 和 ul),但不为 li 和 ul 放置打开标签。我希望这有所帮助。谢谢