Social Networking Site: How to create a Photo Gallery

In this tutorial, I'm going to show you how to create a Photo Gallery using Modal. To start with tutorial, open the “home.php” and on under the first modal, add the following code.
  1. <div class="list-group">
  2.         <a href="gallery.php" class="list-group-item">
  3.           <span class="badge pull-right">
  4.                 <?php $mydb->setQuery("SELECT * FROM photos WHERE `member_id`='{$_SESSION['member_id']}'");
  5.                         $cur = $mydb->executeQuery();
  6.                         echo $row_count = $mydb->num_rows($cur);
  7.                 ?>
  8.         </span>
  9.          View photos
  10.         </a>
  11.  
  12. </div>
The code above will display a list group under the profile picture, and it will also display a number of total number of photos available from the database. And when the user click this link it will be redirected to another PHP page called gallery.php which will be created in later part as we go along in this lesson. And it will look like as shown below. Next, we’re going to add create a new PHP file called “gallery.php”, and add the following code: This code below will display a photo gallery.
  1. <?php  
  2. require_once("includes/initialize.php");       
  3. include 'header.php';
  4. ?>
  5.  
  6. <div class="container">
  7. <div class="rows">
  8.                
  9.  
  10. //thi s PHP code will get all the photos based on the user session ID
  11.                 <?php
  12.                
  13.                 $mydb->setQuery("SELECT * FROM photos WHERE `member_id`='{$_SESSION['member_id']}'");
  14.                 $cur = $mydb->loadResultList();
  15.                 foreach($cur as $object): ?>
  16.                                 <a data-target="#myModal" data-toggle="modal" >
  17.                                 <img src="./uploads/<?php echo $object->filename; ?> " class="img-thumbnail" width="300" height="300" />
  18.                                 </a>
  19.                 <?php endforeach;       ?>
  20.  
  21. //this code we create a modal that later will be used to display a specific photo in a big size format
  22.                  
  23.                 <!-- Modal -->
  24.  
  25.                         <div class="modal fade" id="myModal" tabindex="-1">
  26.                                 <div class="modal-dialog">
  27.                                         <div class="modal-content">
  28.                                                 <div class="modal-body">
  29.                                        
  30.                                                         </div>
  31.  
  32.                                         </div><!-- /.modal-content -->
  33.                                 </div><!-- /.modal-dialog -->
  34.                         </div><!-- /.modal -->
  35.  
  36. </div>
  37.  
  38. </div> <!-- /container -->
  39.  
  40.         <footer>
  41.         <p align="center">&copy; Philsocial 2013</p>
  42.     </footer>
  43.     <!-- Bootstrap core JavaScript
  44.     ================================================== -->
  45.     <!-- Placed at the end of the document so the pages load faster -->
  46.      <script src="js/tooltip.js"></script>
  47.         <script src="assets/js/jquery.js"></script>
  48.     <script src="js/bootstrap.min.js"></script>
  49.          <script src="js/popover.js"></script>
  50.          <script>
  51.                 $(document).ready(function(){
  52.            $('a img').on('click',function(){
  53.                 var src = $(this).attr('src');
  54.                 var image = '<img src="' + src + '" class="img-responsive"/>';
  55.                 $('#myModal').modal();
  56.                 $('#myModal').on('shown.bs.modal', function(){
  57.                     $('#myModal .modal-body').html(image);
  58.                 });
  59.                 $('#myModal').on('hidden.bs.modal', function(){
  60.                     $('#myModal .modal-body').html('');
  61.                 });
  62.            });  
  63.         })
  64.         </script>
  65.        
  66.   </body>
  67. </html>
The output of this code when executed. It will look like as shown below: And we also added below a script code that calls the modal () function when the specific photo is clicked. The the contents of the modal box are grabbed from the two variables we created (src, image) which is the image tag and the original src of the image clicked. And the output of this will look like as shown below. And I have attached the full source code of this tutorial, so that you can modify or customize it to improve it more for your own needs. If you want to see more of my works, new Source Code or Application and Tutorials Just click here.

Add new comment