Monday, March 27, 2017

CRUD OPERATIONS IN OOP AND PHP PDO USING BOOTSTRAP FRAMEWORK.

CRUD OPERATIONS IN  OOP AND PHP PDO USING BOOTSTRAP FRAMEWORK.


Hello Guys! This Tutorial Is About CRUD OPERATIONS IN  OOP AND PHP PDO USING BOOTSTRAP FRAMEWORK WITH PAGINATION.
This tutorial is same like our previous tutorial that we have done simply just for fast learning of php pdo and oop menthod for add, edit and delete.

For Database Creation and  previous tutorial, go to this http://mokeynepal.blogspot.com/2017/03/add-edit-delete-and-display-using-php.html.
In this tutorial i have just added bootstrap table for displaying mysql data, bootstrap modal for Add and delete operations,bootstrap forms and inputs. And Successfull and warning messages  alert in bootstrap alerts, while adding deleting and updating operations are done, This looks Very Nice and flexible bt not like before.
Bootstrap Framework is a free framework with css and jquery combination. Its a Top framework for mobile friendly responsive web design layouts.
You Can download Bootstrap from here :getbootstrap.com 
I am not going to describe whole coding of php pdo and oop here in this tutorial.
After downloading bootstrap simply include these files as like below inside head tags of your php framework... 
<head> 
<link rel="stylesheet" href="bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="libs/jquery/3.1.1/jquery.min.js"></script>
  <script src="bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
CHANGES OF CRUD OPERATIONS IN  OOP AND PHP PDO NOW  USING BOOTSTRAP FRAMEWORK FILES ARE HERE LIKE THIS.
db.php
  1. <?php
  2. $DBhost = "localhost";
  3. $DBuser = "root";
  4. $DBpass = "";
  5. $DBname = "yourdatabase name here";
  6. try
  7. {
  8.  $conn = new PDO("mysql:host={$DBhost};dbname={$DBname}",$DBuser,$DBpass);
  9.  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  10. }
  11. catch(PDOException $e)
  12. {
  13.  echo $e->getMessage();
  14. }
  15. include_once 'class.php';
  16. $crud = new crud($conn);
  17. ?>
class.php
  1. <?php
  2. class crud
  3. {
  4.  private $db;
  5.  
  6.  function __construct($conn)
  7.  {
  8.   $this->db = $conn;
  9.  }
  10.   /* this is for add */
  11.  public function create($name,$email,$address)
  12.  {
  13.   try
  14.   {
  15.    $stmt = $this->db->prepare("INSERT INTO crud(name,email,address) VALUES(:name, :email, :address)");
  16.    $stmt->bindparam(":name",$name);
  17.    $stmt->bindparam(":email",$email);
  18.    $stmt->bindparam(":address",$address);
  19.    $stmt->execute();
  20.    return true;
  21.   }
  22.   catch(PDOException $e)
  23.   {
  24.    echo $e->getMessage(); 
  25.    return false;
  26.   }
  27.   
  28.  }
  29.  
  30.  public function getID($id)
  31.  {
  32.   $stmt = $this->db->prepare("SELECT * FROM crud WHERE id=:id");
  33.   $stmt->execute(array(":id"=>$id));
  34.   $edit=$stmt->fetch(PDO::FETCH_ASSOC);
  35.   return $edit;
  36.  }
  37.   /* this is for edit query process */
  38.  public function update($id,$name,$email,$address)
  39.  {
  40.   try
  41.   {
  42.    $stmt=$this->db->prepare("UPDATE crud SET name=:name, 
  43.                                                  email=:email, 
  44.                 address=:address 
  45.              WHERE id=:id ");
  46.    $stmt->bindparam(":name",$name);
  47.    $stmt->bindparam(":email",$email);
  48.    $stmt->bindparam(":address",$address);
  49.    $stmt->bindparam(":id",$id);
  50.    $stmt->execute();
  51.    
  52.    return true; 
  53.   }
  54.   catch(PDOException $e)
  55.   {
  56.    echo $e->getMessage(); 
  57.    return false;
  58.   }
  59.  }
  60.   /* this is for delete query process */
  61.  public function delete($id)
  62.  {
  63.   $stmt = $this->db->prepare("DELETE FROM crud WHERE id=:id");
  64.   $stmt->bindparam(":id",$id);
  65.   $stmt->execute();
  66.   return true;
  67.  }
  68.  
  69.  /* this is for Displaying Records */
  70.  
  71.  public function view($query)
  72.  {
  73.   $stmt = $this->db->prepare($query);
  74.   $stmt->execute();
  75.  
  76.   if($stmt->rowCount()>0)
  77.   {
  78.    while($row=$stmt->fetch(PDO::FETCH_ASSOC))
  79.    {
  80.     ?>
  81.                 <tr>
  82.                 <td><?php echo($row['id']); ?></td>
  83.                 <td><?php echo($row['name']); ?></td>
  84.                 <td><?php echo($row['email']); ?></td>
  85.                 <td><?php echo($row['address']); ?></td>
  86.                 <td align="center">
  87.                 <a href="edit.php?editid=<?php echo($row['id']); ?>" class="btn btn-warning">Edit</a>
  88.                 </td>
  89.                 <td align="center">
  90.                 <a href="#delete<?php echo($row['id']); ?>" class="btn btn-danger" data-toggle="modal" >Delete</i></a>
  91.                 </td>
  92.                 </tr>
  93.                 <!-- Modal for delete -->
  94.   <div class="modal fade" id="delete<?php echo($row['id']); ?>" role="dialog">
  95.     <div class="modal-dialog">
  96.     
  97.       <!-- Modal content-->
  98.       <div class="modal-content">
  99.         <div class="modal-header">
  100.           <button type="button" class="close" data-dismiss="modal">&times;</button>
  101.           <h4 class="modal-title">Are You Sure To Delete id no: <?php echo($row['id']); ?></h4>
  102.         </div>
  103.         <div class="modal-body">
  104.           <p><?php echo($row['name']); ?></p>
  105.           <p><?php echo($row['email']); ?></p>
  106.         </div>
  107.         <div class="modal-footer">
  108.          <a href="delete.php?deleteid=<?php echo($row['id']); ?>" class="btn btn-success">Sure To Delete</a>
  109.           <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
  110.         </div>
  111.       </div>
  112.       
  113.     </div>
  114.   </div>
  115.                 <?php
  116.    }
  117.   }
  118.   else
  119.   {
  120.    ?>
  121.             <tr>
  122.             <td>Nothing</td>
  123.             </tr>
  124.             <?php
  125.   }
  126.   
  127.  }
  128.  
  129.  public function paging($query,$records_per_page)
  130.  {
  131.   $starting_position=0;
  132.   if(isset($_GET["pagenum"]))
  133.   {
  134.    $starting_position=($_GET["pagenum"]-1)*$records_per_page;
  135.   }
  136.   $query2=$query." limit $starting_position,$records_per_page";
  137.   return $query2;
  138.  }
  139.  
  140.  public function paginglink($query,$records_per_page)
  141.  {
  142.   
  143.   $self = $_SERVER['PHP_SELF'];
  144.   
  145.   $stmt = $this->db->prepare($query);
  146.   $stmt->execute();
  147.   
  148.   $total_no_of_records = $stmt->rowCount();
  149.   
  150.   if($total_no_of_records > 0)
  151.   {
  152.    ?><?php
  153.    $total_no_of_pages=ceil($total_no_of_records/$records_per_page);
  154.    $current_page=1;
  155.    if(isset($_GET["pagenum"]))
  156.    {
  157.     $current_page=$_GET["pagenum"];
  158.    }
  159.    if($current_page!=1)
  160.    {
  161.     $previous =$current_page-1;
  162.     echo "<a href='".$self."?pagenum=1'>First</a>";
  163.     echo "<a href='".$self."?pagenum=".$previous."'>Previous</a>";
  164.    }
  165.    for($i=1;$i<=$total_no_of_pages;$i++)
  166.    {
  167.     if($i==$current_page)
  168.     {
  169.      echo "<a href='".$self."?pagenum=".$i."' style='color:red;'>".$i."</a>";
  170.     }
  171.     else
  172.     {
  173.      echo "<a href='".$self."?pagenum=".$i."'>".$i."</a>";
  174.     }
  175.    }
  176.    if($current_page!=$total_no_of_pages)
  177.    {
  178.     $next=$current_page+1;
  179.     echo "<a href='".$self."?pagenum=".$next."'>Next</a>";
  180.     echo "<a href='".$self."?pagenum=".$total_no_of_pages."'>Last</a>";
  181.    }
  182.    ?><?php
  183.   }
  184.  }
  185.  
  186.  /* paging */
  187.  
  188. }
index.php
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>onlinestudy.bramento.com</title>
  6. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  7.   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  8.   <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  9. </head>
  10. <body>
  11. <div class="container">
  12. <a href="http://onlinestudy.bramento.com"class="btn btn-default">WWW.BRAMENTO.COM</a>
  13. <br></br>
  14. <?php
  15. include ('db.php');
  16. ?>
  17. <?php
  18. include_once 'add.php'; 
  19. ?>
  20. <?php
  21.  if(isset($_GET['deleted']))
  22.  {
  23.   ?>
  24.        <div class="alert alert-info"> <p> Successfully Deleted.<b>Done man!</b></p></div>
  25.         <?php
  26.  }
  27.  else
  28.  {
  29.   ?>
  30.     
  31.   
  32.         <?php
  33.  }
  34.  ?> 
  35.   
  36. <br></br>
  37. <button type="button" class="btn btn-primary" id="myBtnadd" >Add</button>
  38.  
  39. <!-- Modal -->
  40.   <div class="modal fade" id="myModal" role="dialog">
  41.     <div class="modal-dialog">
  42. <div class="modal-content">
  43.         <div class="modal-header">
  44.           <button type="button" class="close" data-dismiss="modal">&times;</button>
  45.           <h4 class="modal-title">Add From Here</h4>
  46.          
  47.         </div>
  48.         <div class="modal-body">
  49.           <form method='post' action="index.php" class="form-group">
  50.  
  51.     <table class='table'>
  52.  
  53.         <tr>
  54.             <td>Name</td>
  55.             <td><input type='text' name='name' class="form-group" required ></td>
  56.         </tr>
  57.  
  58.         <tr>
  59.             <td>Email</td>
  60.             <td><input type='text' name='email' class="form-group" required></td>
  61.         </tr>
  62.  
  63.         <tr>
  64.             <td>Address</td>
  65.             <td><input type='text' name='address' class="form-group" required></td>
  66.         </tr>
  67.  
  68.     </table>
  69.      
  70.         </div>
  71.         <div class="modal-footer">
  72.          <input type="submit"  name="save" value="submit"  class="btn btn-success"> <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
  73.         </div>
  74.         </form>
  75.       </div>
  76.       </div>
  77.       </div>
  78. <br></br>
  79.      <table class="table table-hover">
  80.      <thead>
  81.      <tr class="info">
  82.      <th>ID</th>
  83.      <th>Name</th>
  84.      <th>Email</th>
  85.      <th>Address</th>
  86.      <th>Edit</th>
  87.      <th>Delete</th>
  88.      </tr>
  89.      </thead>
  90.      <?php
  91.       $query = "SELECT * FROM crud"; 
  92.    $records_per_page=10;
  93.   $newquery = $crud->paging($query,$records_per_page);       
  94.   $crud->view($newquery);
  95.   ?>
  96.    
  97. </table>
  98. <tr><table class="table">
  99.         <td colspan="7" align="center">
  100.     <ul class="pagination">
  101.             <li><?php $crud->paginglink($query,$records_per_page); ?></li>
  102.         </ul>
  103.         </td>
  104.     </tr>
  105.     </table>
  106.     
  107. </div>
  108. <script>
  109. $(document).ready(function(){
  110.     $("#myBtnadd").click(function(){
  111.         $("#myModal").modal("toggle");
  112.     });
  113.     
  114. });
  115. </script>
  116. </body>
  117. </html>
add.php 
  1. <?php
  2. include_once 'db.php';
  3. if(isset($_POST['save']))
  4. {
  5.  $name = $_POST['name'];
  6.  $email = $_POST['email'];
  7.  $address = $_POST['address'];
  8.  
  9.  
  10.  if($crud->create($name,$email,$address))
  11.  {
  12.   header("Location: index.php?success");
  13.  }
  14.  else
  15.  {
  16.   header("Location: index.php?failed");
  17.  }
  18. }
  19. ?>
  20. <?php
  21. if(isset($_GET['success']))
  22. {
  23.  ?>
  24.     <div class="alert alert-success">
  25.   <strong>Success!</strong> Yeah Its done. Successfully Aded. Check IT Out!
  26. </div>
  27.     <?php
  28. }
  29. else if(isset($_GET['failed']))
  30. {
  31.  ?>
  32.    <div class="alert alert-warning">
  33.   <strong>Shit Man! failed.</strong> Whats the problem man. Check It Out and Try again!
  34. </div>
  35.     <?php
  36. }
  37. ?>
  • edit.php
  1. <?php
  2. include_once 'db.php';
  3. if(isset($_POST['update']))
  4. {
  5.  $id = $_GET['editid'];
  6.  $name = $_POST['name'];
  7.  $email = $_POST['email'];
  8.  $address = $_POST['address'];
  9.  if($crud->update($id,$name,$email,$address))
  10.  {
  11.   $message = "<div class='alert alert-info'>Yeah Man! Its Done Updating.</div>";
  12.  }
  13.  else
  14.  {
  15.   $message = "<div class='alert alert-warning'>Failed Man!</div>";
  16.  }
  17. }
  18. if(isset($_GET['editid']))
  19. {
  20.  $id = $_GET['editid'];
  21.  extract($crud->getID($id)); 
  22. }
  23. ?>
  24. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  25. <html xmlns="http://www.w3.org/1999/xhtml">
  26. <head>
  27. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  28. <title>Untitled Document</title>
  29. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  30.   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  31.   <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  32. </head>
  33. <body>
  34. <div class="container">
  35. <?php
  36. if(isset($message))
  37. {
  38.  echo $message;
  39. }
  40. ?>
  41.      <form method='post'>
  42.     <table class="table">
  43.          <tr>
  44.          <td><input type='hidden' name='id' value="<?php echo $id; ?>" ></td>
  45.         </tr>
  46.         <tr>
  47.             <td>Name</td>
  48.             <td><input type='text' name='name' value="<?php echo $name; ?>" class="form-group" required></td>
  49.         </tr>
  50.         <tr>
  51.             <td>Email</td>
  52.             <td><input type='text' name='email'  value="<?php echo $email; ?>" class="form-group" required></td>
  53.         </tr>
  54.         <tr>
  55.             <td>Address</td>
  56.             <td><input type='text' name='address'  value="<?php echo $address; ?>" class="form-group" required></td>
  57.         </tr>
  58.         <tr>
  59.         <td>
  60.             <input type='submit' name='update' class="btn btn-primary" value="Update">
  61.           </td>
  62.           <td ><a href="index.php"  class="btn btn-default">Go Back!</a></td>
  63.         </tr>
  64.     </table>
  65.     </form>
  66.     </div>
  67.     </body>
  68.     </html>
 delete.php
  1. <?php
  2. include_once 'db.php';
  3.  $id = $_GET['deleteid'];
  4.  $crud->delete($id);
  5.  header("Location: index.php?deleted"); 
  6. ?>
     

No comments:

Post a Comment