C G
C G

Reputation: 1

PHP gallery album

i am writing a php script that retrieves images from a database and shows them on page inside an album , each album contains several images...

here is an example for the code below :

$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>';
}
?>

this code shows all the albums contained in the database , but what i want to do is that when i click on the album cover , i want to display the images inside it on the same page , but i am not able to pass the album id to another php script on the same page to do this, please if anyone can help thank u in advance

Upvotes: 0

Views: 74

Answers (1)

ThomasK
ThomasK

Reputation: 2220

Have a look at the following code:

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

What we are doing here is gettign all the albums as you allready does. But we introduce a subquery inside the first while-loop to fetch all associated images for each album.
We put these images inside new div-container with a unique album id you can use to show or hide the actual album with your showfunction()
Use CSS and javascript to show or hide accordingly.

OBS! WARNING!! Your mysql queries, and the one I provided, is very very BAD!
Have a look into prepared statement using php PDO or mysqli...

Upvotes: 1

Related Questions