PHP - Simple Image Slider Using Jquery

In this tutorial we will create a Simple Image Slider Using Jquery. PHP is a server-side scripting language designed primarily for web development. Using PHP, you can let your user directly interact with the script and easily to learned its syntax. It is mostly used by a newly coders for its user friendly environment. So Let's do the coding...

Getting started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. And this is the link for the jquery that i used in this tutorial https://jquery.com/. Lastly, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating Database

Open your database web server then create a database name in it db_slider, after that click Import then locate the database file inside the folder of the application then click ok. tut1

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.
  1. <?php
  2.         $conn = mysqli_connect('localhost', 'root', '', 'db_slider');
  3.        
  4.         if(!$conn){
  5.                 die('Error: Failed to connect to database');
  6.         }
  7. ?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6.         </head>
  7. <body>
  8.         <nav class="navbar navbar-default">
  9.                 <div class="container-fluid">
  10.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11.                 </div>
  12.         </nav>
  13.         <div class="col-md-3"></div>
  14.         <div class="col-md-6 well">
  15.                 <h3 class="text-primary">PHP - Simple Image Slider</h3>
  16.                 <hr style="border-top:1px dotted #ccc;" />
  17.                 <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#form_modal">Add Image</button>
  18.                 <br /><br />
  19.                 <?php include 'slider.php'?>
  20.         </div>
  21.         <div class="modal fade" id="form_modal" tabindex="-1" role="dialog" aria-hidden="true">
  22.                 <div class="modal-dialog">
  23.                         <form action="save_image.php" method="POST" enctype="multipart/form-data">
  24.                                 <div class="modal-content">
  25.                                         <div class="modal-body">
  26.                                                 <div class="col-md-3"></div>
  27.                                                 <div class="col-md-6">
  28.                                                         <div class="form-group">
  29.                                                                 <label>Filename:</label>
  30.                                                                 <input type="file" name="image" class="form-control" required="required"/>
  31.                                                         </div>
  32.                                                 </div>
  33.                                         </div>
  34.                                         <div style="clear:both;"></div>
  35.                                         <div class="modal-footer">
  36.                                                 <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  37.                                                 <button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button>
  38.                                         </div>
  39.                                 </div>
  40.                         </form>
  41.                 </div>
  42.         </div>
  43. <script src="js/jquery-3.2.1.min.js"></script>
  44. <script src="js/bootstrap.js"></script>
  45. </body>
  46. </html>

Creating PHP Query

This code contains the php query of the application. This code will upload the images and then can remove at the same time, To do that just copy and write this block of codes inside the text editor, then save it as shown below. save_image.php
  1. <?php
  2.         date_default_timezone_set('Asia/Manila');
  3.         require_once 'conn.php';
  4.        
  5.         if(ISSET($_POST['save'])){
  6.                 $file_name = $_FILES['image']['name'];
  7.                 $file_temp = $_FILES['image']['tmp_name'];
  8.                 $file_size = $_FILES['image']['size'];
  9.                
  10.                 $query = mysqli_query($conn, "SELECT * FROM `image`") or die(mysqli_error());
  11.                 $count = mysqli_num_rows($query);
  12.                
  13.                 if($count < 5){
  14.                         if($file_size < 2000000){
  15.                                 $file = explode('.', $file_name);
  16.                                 $end = end($file);
  17.                                 $ext = array('jpg', 'png', 'jpeg');
  18.                                 if(in_array($end, $ext)){
  19.                                         $name = time().date("Ymd");
  20.                                         $location = 'upload/'.$name.".".$end;
  21.                                         if(move_uploaded_file($file_temp, $location)){
  22.                                                 mysqli_query($conn, "INSERT INTO `image` VALUES('', '$name', '$location')") or die(mysqli_error());
  23.                                                 echo "<script>alert('File uploaded')</script>";
  24.                                                 echo "<script>window.location = 'index.php'</script>";
  25.                                         }
  26.                                 }else{
  27.                                         echo "<script>alert('Wrong image format')</script>";
  28.                                         echo "<script>window.location = 'index.php'</script>";
  29.                                 }
  30.                                
  31.                         }else{
  32.                                 echo "<script>alert('File too large to upload')</script>";
  33.                                 echo "<script>window.location = 'index.php'</script>";
  34.                         }
  35.                 }else{
  36.                         echo "<script>alert('Only 5 images can upload')</script>";
  37.                         echo "<script>window.location = 'index.php'</script>";
  38.                 }
  39.         }
  40. ?>
delete.php
  1. <?php
  2.         require_once 'conn.php';
  3.        
  4.         if(ISSET($_REQUEST['image_id']) && ISSET($_REQUEST['image_name'])){
  5.                 if(unlink($_REQUEST['image_name'])){
  6.                         mysqli_query($conn, "DELETE FROM `image` WHERE `image_id` = '$_REQUEST[image_id]'");
  7.                         header('location: index.php');
  8.                 }      
  9.         }
  10. ?>

Creating the Main Function

This code contains the main function of the application. This code will create a slideshow and display it with effect, using the images from the database. To do this just copy and write the code below inside the text editor, then save it as slider.php.
  1. <div id="myCarousel" class="carousel slide container-fluid" data-ride="carousel">
  2.                 <ol class="carousel-indicators">
  3.                         <?php
  4.                                 require 'conn.php';
  5.                                
  6.                                 $query = mysqli_query($conn, "SELECT * FROM `image` ORDER BY `image_id` ASC") or die(mysqli_error());
  7.                                 $count = mysqli_num_rows($query);
  8.                                 if($count > 0){
  9.                                         for($i=0; $i < $count; $i++){
  10.                         ?>
  11.                                                 <li data-target="#myCarousel" data-slide-to="<?php echo $i?>" <?php if($i===0){echo "class='active'";}?>></li>
  12.                         <?php
  13.                                         }
  14.                         ?>
  15.                 </ol>
  16.                 <div style = "margin:auto;" class="carousel-inner" role="listbox">
  17.                         <?php
  18.                                         $x = 0;
  19.                                         while($fetch = mysqli_fetch_array($query)){
  20.                         ?>
  21.                                         <div class="item <?php if($x === 0){echo "active";}?>">
  22.                                                 <img src="<?php echo $fetch['location']?>" style = "width:100%; height:300px;" />
  23.                                         </div>
  24.                         <?php
  25.                                         $x++;
  26.                                         }
  27.                         ?>
  28.                        
  29.                 </div>
  30.                 <a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
  31.                         <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
  32.                         <span class="sr-only">Previous</span>
  33.                 </a>
  34.                 <a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
  35.                         <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
  36.                         <span class="sr-only">Next</span>
  37.                 </a>   
  38. </div>
  39. <br />
  40. <div class="col-md-6">
  41.         <ul class="list-group"/><h4>Images</h4>
  42.                 <?php
  43.                                 $image_query = mysqli_query($conn, "SELECT * FROM `image` ORDER BY `image_id` ASC");
  44.                                 while($image_fetch = mysqli_fetch_array($image_query)){
  45.                 ?>
  46.                                 <li class='list-group-item'><?php echo $image_fetch['image_name']?><a href="delete.php?image_id=<?php echo $image_fetch['image_id']?>&image_name=<?php echo $image_fetch['location']?>" class="text-danger"><span class="glyphicon glyphicon-trash pull-right"></span></a></li>
  47.                 <?php
  48.                                 }
  49.                 ?>
  50.                
  51.         </ul>
  52. </div>
  53.         <?php
  54.                         }
  55.         ?>
There you have it we successfully created Simple Image Slider Using Jquery. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Add new comment