Simple photos gallery using jquery ,fancybox library and php pdo.
In this tutorial we are going to make simple photos gallery using jquery fancybox and php pdo.
FancyBox is a tool for displaying images, html content and multi-media in a Mac-style "lightbox" that floats overtop of web page.
It was built using the jQuery library. Licensed under both MIT and GPL licenses.
It was built using the jQuery library. Licensed under both MIT and GPL licenses.
Its a free to download and easy to use on your website. you can get different styles of fancybox(download from here).
You Can get various exames to download of fancyboxes like
- Inline-auto detect width and height
- Inline-modal window
- Ajax-passing custom data
- Ifrane
- Swf
- Google maps (iframe)
- Youtube (iframe) and etc..
We have already done a simple method for displaying and uploading images and text in our previous tutorial How To Upload And Display Images And Text Together Using PHP PDO..
Now, At this time we are going to display that images in fancybox format Which can display our images in a modal view. Its really looks awesome for your website.
If you are ready now and have downloaded fancybox library then lets start,
First,Create index.php file and Connect or insert these fanacybox library inside html head tags as like this
<head>
<script src="../js/jquery.min.js"></script>
<script src="../js/source/jquery.fancybox.js"></script>
<link rel="stylesheet" href="../js/source/jquery.fancybox.css" media="screen">
</head>
And Now insert some javascript of fancyfox inside head tags again to your index.php file.
<script type="text/javascript">
$(document).ready(function() {
$('.fancybox').fancybox();
$(document).ready(function() {
$('.fancybox').fancybox();
});
</script>
</script>
And Now Inside the body tags of index.php file copy and paste these php pdo scripts which displays our mysql table records and display images and title in a fancybox modal.
Our database connection is same as we have done already in How To Upload And Display Images And Text Together Using PHP PDO..
To learn how to upload and display images more with php and pdo go here How To Upload And Display Images And Text Together Using PHP PDO..
- <?php
- include('db.php');
- //query for selecting database table
- $results = $db_con->prepare("SELECT * FROM posts ORDER BY post_id");
- $results->execute();
- // this while statement displays rows of database table
- while($row=$results->fetch(PDO::FETCH_ASSOC))
- {
- extract($row);
- ?>
- <div class="col-sm-4">
- <div class="thumbnail text-center">
- <?php echo $row['image_title']; ?>
- <?php echo "
- <a href='uploads/$post_image' class='fancybox' data-fancybox-group='gallery' title='$image_title'><img src='uploads/$post_image' class='img-responsive' alt='kamala' width='200' height='200' style='border:1px solid #333333;'/> </a> ";?> <!--This class='fancybox' data-fancybox-group='gallery' called by javacript above and proceed for fancybox modal -->
- </div>
- </div>
- <?php
- }
- ?>
This is how we created simple method to show images in a fancybox modal using fancybox library and php pdo.
Full index.php file
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title> simple fancybox modal in php pdo.</title>
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" >
- <script src="../js/jquery.min.js"></script>
- <script src="../bootstrap/bootstrap.min.js"></script>
- <!-- Add fancyBox main JS and CSS files -->
- <script src="../js/source/jquery.fancybox.js"></script>
- <link rel="stylesheet" href="../js/source/jquery.fancybox.css" media="screen">
- <script type="text/javascript">
- $(document).ready(function() {
- $('.fancybox').fancybox();
- });
- </script>
- </head>
- <body>
- <div class="container">
- <?php
- include('db.php');
- //query for selecting database table
- $results = $db_con->prepare("SELECT * FROM posts ORDER BY post_id");
- $results->execute();
- // this while statement displays rows of database table
- while($row=$results->fetch(PDO::FETCH_ASSOC))
- {
- extract($row);
- ?>
- <div class="col-sm-4">
- <div class="thumbnail text-center">
- <?php echo $row['image_title']; ?>
- <?php echo "
- <a href='uploads/$post_image' class='fancybox' data-fancybox-group='gallery' title='$image_title'><img src='uploads/$post_image' class='img-responsive' alt='kamala' width='200' height='200' style='border:1px solid #333333;'/> </a> ";?>
- </div>
- </div>
- <?php
- }
- ?>
- </div>
- </body>
- </html>
No comments:
Post a Comment