Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php mysql while loop inside while loop querying same table

This is my code

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

Till this point everything works fine , It shows me the names of all the albums that are there in my db . What I want to do next is I want it to show all the pics which belong to $album_names so I wrote this code after above code

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

?>

Problem is that it only shows me the first result of $image_path and $thumbnail_path but I want it to show all the image paths belonging to that particular album.

Hope I have cleared my question .

like image 963
MR-4O4 Avatar asked Apr 25 '26 04:04

MR-4O4


1 Answers

This looks like pseudo-code to me (as it is not having proper php tags). Anyway, Problem is as follows.. Your html tags li arrangements are wrong. That is why you are not seeing the other results. It has to be fixed as below

<?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

?>
like image 53
Thanga Avatar answered Apr 27 '26 20:04

Thanga