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>
<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
- <?php
- $DBhost = "localhost";
- $DBuser = "root";
- $DBpass = "";
- $DBname = "yourdatabase name here";
- try
- {
- $conn = new PDO("mysql:host={$DBhost};dbname={$DBname}",$DBuser,$DBpass);
- $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- }
- catch(PDOException $e)
- {
- echo $e->getMessage();
- }
- include_once 'class.php';
- $crud = new crud($conn);
- ?>
class.php
- <?php
- class crud
- {
- private $db;
- function __construct($conn)
- {
- $this->db = $conn;
- }
- /* this is for add */
- public function create($name,$email,$address)
- {
- try
- {
- $stmt = $this->db->prepare("INSERT INTO crud(name,email,address) VALUES(:name, :email, :address)");
- $stmt->bindparam(":name",$name);
- $stmt->bindparam(":email",$email);
- $stmt->bindparam(":address",$address);
- $stmt->execute();
- return true;
- }
- catch(PDOException $e)
- {
- echo $e->getMessage();
- return false;
- }
- }
- public function getID($id)
- {
- $stmt = $this->db->prepare("SELECT * FROM crud WHERE id=:id");
- $stmt->execute(array(":id"=>$id));
- $edit=$stmt->fetch(PDO::FETCH_ASSOC);
- return $edit;
- }
- /* this is for edit query process */
- public function update($id,$name,$email,$address)
- {
- try
- {
- $stmt=$this->db->prepare("UPDATE crud SET name=:name,
- email=:email,
- address=:address
- WHERE id=:id ");
- $stmt->bindparam(":name",$name);
- $stmt->bindparam(":email",$email);
- $stmt->bindparam(":address",$address);
- $stmt->bindparam(":id",$id);
- $stmt->execute();
- return true;
- }
- catch(PDOException $e)
- {
- echo $e->getMessage();
- return false;
- }
- }
- /* this is for delete query process */
- public function delete($id)
- {
- $stmt = $this->db->prepare("DELETE FROM crud WHERE id=:id");
- $stmt->bindparam(":id",$id);
- $stmt->execute();
- return true;
- }
- /* this is for Displaying Records */
- public function view($query)
- {
- $stmt = $this->db->prepare($query);
- $stmt->execute();
- if($stmt->rowCount()>0)
- {
- while($row=$stmt->fetch(PDO::FETCH_ASSOC))
- {
- ?>
- <tr>
- <td><?php echo($row['id']); ?></td>
- <td><?php echo($row['name']); ?></td>
- <td><?php echo($row['email']); ?></td>
- <td><?php echo($row['address']); ?></td>
- <td align="center">
- <a href="edit.php?editid=<?php echo($row['id']); ?>" class="btn btn-warning">Edit</a>
- </td>
- <td align="center">
- <a href="#delete<?php echo($row['id']); ?>" class="btn btn-danger" data-toggle="modal" >Delete</i></a>
- </td>
- </tr>
- <!-- Modal for delete -->
- <div class="modal fade" id="delete<?php echo($row['id']); ?>" role="dialog">
- <div class="modal-dialog">
- <!-- Modal content-->
- <div class="modal-content">
- <div class="modal-header">
- <button type="button" class="close" data-dismiss="modal">×</button>
- <h4 class="modal-title">Are You Sure To Delete id no: <?php echo($row['id']); ?></h4>
- </div>
- <div class="modal-body">
- <p><?php echo($row['name']); ?></p>
- <p><?php echo($row['email']); ?></p>
- </div>
- <div class="modal-footer">
- <a href="delete.php?deleteid=<?php echo($row['id']); ?>" class="btn btn-success">Sure To Delete</a>
- <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
- </div>
- </div>
- </div>
- </div>
- <?php
- }
- }
- else
- {
- ?>
- <tr>
- <td>Nothing</td>
- </tr>
- <?php
- }
- }
- public function paging($query,$records_per_page)
- {
- $starting_position=0;
- if(isset($_GET["pagenum"]))
- {
- $starting_position=($_GET["pagenum"]-1)*$records_per_page;
- }
- $query2=$query." limit $starting_position,$records_per_page";
- return $query2;
- }
- public function paginglink($query,$records_per_page)
- {
- $self = $_SERVER['PHP_SELF'];
- $stmt = $this->db->prepare($query);
- $stmt->execute();
- $total_no_of_records = $stmt->rowCount();
- if($total_no_of_records > 0)
- {
- ?><?php
- $total_no_of_pages=ceil($total_no_of_records/$records_per_page);
- $current_page=1;
- if(isset($_GET["pagenum"]))
- {
- $current_page=$_GET["pagenum"];
- }
- if($current_page!=1)
- {
- $previous =$current_page-1;
- echo "<a href='".$self."?pagenum=1'>First</a>";
- echo "<a href='".$self."?pagenum=".$previous."'>Previous</a>";
- }
- for($i=1;$i<=$total_no_of_pages;$i++)
- {
- if($i==$current_page)
- {
- echo "<a href='".$self."?pagenum=".$i."' style='color:red;'>".$i."</a>";
- }
- else
- {
- echo "<a href='".$self."?pagenum=".$i."'>".$i."</a>";
- }
- }
- if($current_page!=$total_no_of_pages)
- {
- $next=$current_page+1;
- echo "<a href='".$self."?pagenum=".$next."'>Next</a>";
- echo "<a href='".$self."?pagenum=".$total_no_of_pages."'>Last</a>";
- }
- ?><?php
- }
- }
- /* paging */
- }
index.php
- <!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>onlinestudy.bramento.com</title>
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
- </head>
- <body>
- <div class="container">
- <a href="http://onlinestudy.bramento.com"class="btn btn-default">WWW.BRAMENTO.COM</a>
- <br></br>
- <?php
- include ('db.php');
- ?>
- <?php
- include_once 'add.php';
- ?>
- <?php
- if(isset($_GET['deleted']))
- {
- ?>
- <div class="alert alert-info"> <p> Successfully Deleted.<b>Done man!</b></p></div>
- <?php
- }
- else
- {
- ?>
- <?php
- }
- ?>
- <br></br>
- <button type="button" class="btn btn-primary" id="myBtnadd" >Add</button>
- <!-- Modal -->
- <div class="modal fade" id="myModal" role="dialog">
- <div class="modal-dialog">
- <div class="modal-content">
- <div class="modal-header">
- <button type="button" class="close" data-dismiss="modal">×</button>
- <h4 class="modal-title">Add From Here</h4>
- </div>
- <div class="modal-body">
- <form method='post' action="index.php" class="form-group">
- <table class='table'>
- <tr>
- <td>Name</td>
- <td><input type='text' name='name' class="form-group" required ></td>
- </tr>
- <tr>
- <td>Email</td>
- <td><input type='text' name='email' class="form-group" required></td>
- </tr>
- <tr>
- <td>Address</td>
- <td><input type='text' name='address' class="form-group" required></td>
- </tr>
- </table>
- </div>
- <div class="modal-footer">
- <input type="submit" name="save" value="submit" class="btn btn-success"> <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
- </div>
- </form>
- </div>
- </div>
- </div>
- <br></br>
- <table class="table table-hover">
- <thead>
- <tr class="info">
- <th>ID</th>
- <th>Name</th>
- <th>Email</th>
- <th>Address</th>
- <th>Edit</th>
- <th>Delete</th>
- </tr>
- </thead>
- <?php
- $query = "SELECT * FROM crud";
- $records_per_page=10;
- $newquery = $crud->paging($query,$records_per_page);
- $crud->view($newquery);
- ?>
- </table>
- <tr><table class="table">
- <td colspan="7" align="center">
- <ul class="pagination">
- <li><?php $crud->paginglink($query,$records_per_page); ?></li>
- </ul>
- </td>
- </tr>
- </table>
- </div>
- <script>
- $(document).ready(function(){
- $("#myBtnadd").click(function(){
- $("#myModal").modal("toggle");
- });
- });
- </script>
- </body>
- </html>
add.php
- <?php
- include_once 'db.php';
- if(isset($_POST['save']))
- {
- $name = $_POST['name'];
- $email = $_POST['email'];
- $address = $_POST['address'];
- if($crud->create($name,$email,$address))
- {
- header("Location: index.php?success");
- }
- else
- {
- header("Location: index.php?failed");
- }
- }
- ?>
- <?php
- if(isset($_GET['success']))
- {
- ?>
- <div class="alert alert-success">
- <strong>Success!</strong> Yeah Its done. Successfully Aded. Check IT Out!
- </div>
- <?php
- }
- else if(isset($_GET['failed']))
- {
- ?>
- <div class="alert alert-warning">
- <strong>Shit Man! failed.</strong> Whats the problem man. Check It Out and Try again!
- </div>
- <?php
- }
- ?>
- edit.php
- <?php
- include_once 'db.php';
- if(isset($_POST['update']))
- {
- $id = $_GET['editid'];
- $name = $_POST['name'];
- $email = $_POST['email'];
- $address = $_POST['address'];
- if($crud->update($id,$name,$email,$address))
- {
- $message = "<div class='alert alert-info'>Yeah Man! Its Done Updating.</div>";
- }
- else
- {
- $message = "<div class='alert alert-warning'>Failed Man!</div>";
- }
- }
- if(isset($_GET['editid']))
- {
- $id = $_GET['editid'];
- extract($crud->getID($id));
- }
- ?>
- <!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>Untitled Document</title>
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
- </head>
- <body>
- <div class="container">
- <?php
- if(isset($message))
- {
- echo $message;
- }
- ?>
- <form method='post'>
- <table class="table">
- <tr>
- <td><input type='hidden' name='id' value="<?php echo $id; ?>" ></td>
- </tr>
- <tr>
- <td>Name</td>
- <td><input type='text' name='name' value="<?php echo $name; ?>" class="form-group" required></td>
- </tr>
- <tr>
- <td>Email</td>
- <td><input type='text' name='email' value="<?php echo $email; ?>" class="form-group" required></td>
- </tr>
- <tr>
- <td>Address</td>
- <td><input type='text' name='address' value="<?php echo $address; ?>" class="form-group" required></td>
- </tr>
- <tr>
- <td>
- <input type='submit' name='update' class="btn btn-primary" value="Update">
- </td>
- <td ><a href="index.php" class="btn btn-default">Go Back!</a></td>
- </tr>
- </table>
- </form>
- </div>
- </body>
- </html>
delete.php
- <?php
- include_once 'db.php';
- $id = $_GET['deleteid'];
- $crud->delete($id);
- header("Location: index.php?deleted");
- ?>
No comments:
Post a Comment