Gallery Viewer using Fancybox

Getting Started

I've used CDN of Bootstrap and jQuery so, you need internet connection for them to work. FancyBox CSS and JS is included in the downloadable file of this tutorial.

Creating our Database

1. Open phpMyAdmin. 2. Click databases, create a database and name it as fancybox. 3. After creating a database, click the SQL and paste the below codes. See image below for detailed instruction.
  1. CREATE TABLE `photo` (
  2. `photoid` INT(11) NOT NULL AUTO_INCREMENT,
  3. `description` text NOT NULL,
  4. `location` VARCHAR(150) NOT NULL,
  5. PRIMARY KEY(`photoid`)
  6. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
database sql

index.php

This is our index which contains our images table, modal in adding a new image, and our fancybox viewer.
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>Gallery Viewer using Fancybox</title>
  6. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
  7. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
  8. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  9.  
  10. <!-- fancybox CSS library -->
  11. <link rel="stylesheet" type="text/css" href="fancybox/jquery.fancybox.css">
  12. <!-- JS library -->
  13. <script src="//code.jquery.com/jquery-3.1.1.min.js"></script>
  14. <!-- fancybox JS library -->
  15. <script src="fancybox/jquery.fancybox.js"></script>
  16.  
  17. <script type="text/javascript">
  18. $("[data-fancybox]").fancybox({ });
  19. </script>
  20. </head>
  21. <body>
  22. <div class="container">
  23. <h1 class="page-header text-center">Gallery Viewer using FancyBox</h1>
  24. <h2>
  25. Photo List
  26. <a href="#newphoto" data-toggle="modal" class="pull-right btn btn-primary"><span class="glyphicon glyphicon-plus"></span> Photo</a>
  27. </h2>
  28. <?php
  29.  
  30. if(isset($_SESSION['success'])){
  31. ?>
  32. <div class="alert alert-success text-center">
  33. <?php echo $_SESSION['success']; ?>
  34. </div>
  35. <?php
  36.  
  37. unset($_SESSION['success']);
  38. }
  39.  
  40. if(isset($_SESSION['error'])){
  41. ?>
  42. <div class="alert alert-danger text-center">
  43. <?php echo $_SESSION['error']; ?>
  44. </div>
  45. <?php
  46.  
  47. unset($_SESSION['error']);
  48. }
  49. ?>
  50. <table class="table table-bordered table-striped">
  51. <thead>
  52. <th>Description</th>
  53. <th>Location</th>
  54. <th>Photo</th>
  55. </thead>
  56. <tbody>
  57. <?php
  58. $conn = new mysqli("localhost", "root", "", "fancybox");
  59.  
  60. if ($conn->connect_error) {
  61. die("Connection failed: " . $conn->connect_error);
  62. }
  63.  
  64. $sql="select * from photo";
  65. $query=$conn->query($sql);
  66. while($row=$query->fetch_array()){
  67. ?>
  68. <tr>
  69. <td><?php echo $row['description']; ?></td>
  70. <td><?php echo $row['location']; ?></td>
  71. <td><a href="<?php echo $row['location']; ?>" data-fancybox="group" data-caption="<?php echo $row["description"]; ?>"><img src="<?php echo $row['location']; ?>" height="30px" width="30px"></a></td>
  72. </tr>
  73. <?php
  74. }
  75. ?>
  76. </tbody>
  77. </table>
  78. </div>
  79.  
  80. <!-- Add Photo -->
  81. <div class="modal fade" id="newphoto" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  82. <div class="modal-dialog" role="document">
  83. <div class="modal-content">
  84. <div class="modal-header">
  85. <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
  86. <h4 class="modal-title text-center" id="myModalLabel">Add New Photo</h4>
  87. </div>
  88. <div class="modal-body">
  89. <div class="container-fluid">
  90. <form method="POST" action="upload.php" enctype="multipart/form-data">
  91. <div class="form-group">
  92. <div class="row">
  93. <div class="col-md-2" style="margin-top:7px;">
  94. <label>Description:</label>
  95. </div>
  96. <div class="col-md-10">
  97. <input type="text" class="form-control" name="description" required>
  98. </div>
  99. </div>
  100. </div>
  101. <div class="form-group">
  102. <div class="row">
  103. <div class="col-md-2" >
  104. <label>Photo:</label>
  105. </div>
  106. <div class="col-md-10">
  107. <input type="file" name="image">
  108. </div>
  109. </div>
  110. </div>
  111. </div>
  112. </div>
  113. <div class="modal-footer">
  114. <button type="button" class="btn btn-default" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Cancel</button>
  115. <button type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-floppy-disk"></span> Save</button>
  116. </form>
  117. </div>
  118. </div>
  119. </div>
  120. </div>
  121.  
  122. </body>
  123. </html>

upload.php

Lastly, this is our PHP upload code that uploaded new images in the database.
  1. <?php
  2.  
  3. $conn = new mysqli("localhost", "root", "", "fancybox");
  4. if ($conn->connect_error) {
  5. die("Connection failed: " . $conn->connect_error);
  6. }
  7.  
  8. $description=$_POST['description'];
  9.  
  10. $fileInfo = PATHINFO($_FILES["image"]["name"]);
  11. if (empty($_FILES["image"]["name"])){
  12. $_SESSION['error']="Upload Failed. File empty!";
  13. header('location:index.php');
  14. }
  15. else{
  16. if ($fileInfo['extension'] == "jpg" OR $fileInfo['extension'] == "jpeg" OR $fileInfo['extension'] == "png") {
  17. $newFilename = $fileInfo['filename'] . "_" . time() . "." . $fileInfo['extension'];
  18. move_uploaded_file($_FILES["image"]["tmp_name"], "upload/" . $newFilename);
  19. $location = "upload/" . $newFilename;
  20.  
  21. $sql="insert into photo (description, location) values ('$description', '$location')";
  22. $query=$conn->query($sql);
  23.  
  24. if($query){
  25. $_SESSION['success']="Photo uploaded Successfully!";
  26. }
  27. else{
  28. $_SESSION['error']="Something went wrong. Can't upload Photo!";
  29. }
  30.  
  31. header('location:index.php');
  32.  
  33. }
  34. else{
  35. $_SESSION['error']="Upload Failed. Please upload JPG or PNG photo only!";
  36. header('location:index.php');
  37. }
  38. }
  39.  
  40. ?>
You should now be able to access the fancybox viewer by clicking the photo in our table. fancy click That ends this tutorial. Happy Coding :)

Add new comment