Tuesday, 13 December 2016

PHP Ajax Crud using OOPS - Select or Fetch Mysql Data



This is one more Webslesson web development tutorial, From this tutorial we have start discussing one more topic on Web Development like how to make simple crud system by using Ajax with PHP. For PHP crud operation we will use principal of Object Oriented Programming concept.

We will use Ajax JQuery method with PHP Object Oriented Programming. So we can Insert or Add, Update or Edit, Delete or remove and Select or fetch records from Mysql database by using PHP object object oriented programming concept with Ajax. We will discussing how can we reuse same code for different crud operation with Ajax. In this system we will also upload image by using Ajax and we also see how can we change uploaded image by using Ajax with PHP programming.

This is first part of this web development tutorial on making of Crud Operation system by using PHP object oriented programming concept with Ajax. In this part we will select or fetch records from mysql database table and displaying that data on web page with update and delete button link in table format.

This is the first part of making of crud system by using principal of Object Oriented programming in PHP language with Ajax method. In this part we have load or fetch data from Mysql table and display on Web page by using Ajax JQuery method with PHP Object Oriented Programming. Here first we have make one class and in that class we have first make database connection and put database connection in constructor magic method, so when we have make new object of this class, it will automatically make database connection. Then after we have make one new function for execute query and then after we have make new method for converting query result data into HTML table format. After making this function we have go to HTML page and on that page we have make Ajax method. This method will load data under table on page load. This Ajax method has use different method of class and display data on web page in table format. In next video will learn how to insert data by using PHP object objected programming concept with Ajax method.




Source Code


index.php


 <?php  
 //index.php  
 include 'crud.php';  
 $object = new Crud();  
 ?>  
 <html>  
      <head>  
           <title>PHP Mysql Ajax Crud using OOPS - Fetch Data</title>  
           <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>  
           <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />  
           <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>  
           <style>  
                body  
                {  
                     margin:0;  
                     padding:0;  
                     background-color:#f1f1f1;  
                }  
                .box  
                {  
                     width:900px;  
                     padding:20px;  
                     background-color:#fff;  
                     border:1px solid #ccc;  
                     border-radius:5px;  
                     margin-top:100px;  
                }  
           </style>  
      </head>  
      <body>  
           <div class="container box">  
                <h3 align="center">PHP Mysql Ajax Crud using OOPS - Fetch Data</h3><br />  
                <br />  
                <div id="user_table" class="table-responsive">  
                </div>  
           </div>  
      </body>  
 </html>  
 <script type="text/javascript">  
      $(document).ready(function(){  
           load_data();  
           function load_data()  
           {  
                var action = "Load";  
                $.ajax({  
                     url:"action.php",  
                     method:"POST",  
                     data:{action:action},  
                     success:function(data)  
                     {  
                          $('#user_table').html(data);  
                     }  
                });  
           }  
      });  
 </script>  

crud.php


 <?php  
 class Crud  
 {  
      //crud class  
      public $connect;  
      private $host = "localhost";  
      private $username = 'root';  
      private $password = '';  
      private     $database = 'crud';  
      function __construct()  
      {  
           $this->database_connect();  
      }  
      public function database_connect()  
      {  
           $this->connect = mysqli_connect($this->host, $this->username, $this->password, $this->database);  
      }  
      public function execute_query($query)  
      {  
           return mysqli_query($this->connect, $query);  
      }  
      public function get_data_in_table($query)  
      {  
           $output = '';  
           $result = $this->execute_query($query);  
           $output .= '  
           <table class="table table-bordered table-striped">  
                <tr>  
                     <th width="10%">Image</th>  
                     <th width="35%">First Name</th>  
                     <th width="35%">Last Name</th>  
                     <th width="10%">Update</th>  
                     <th width="10%">Delete</th>  
                </tr>  
           ';  
           while($row = mysqli_fetch_object($result))  
           {  
                $output .= '  
                <tr>       
                     <td><img src="upload/'.$row->image.'" class="img-thumbnail" width="50" height="35" /></td>  
                     <td>'.$row->first_name.'</td>  
                     <td>'.$row->last_name.'</td>  
                     <td><button type="button" name="update" id="'.$row->id.'" class="btn btn-success btn-xs update">Update</button></td>  
                     <td><button type="button" name="delete" id="'.$row->id.'" class="btn btn-danger btn-xs delete">Delete</button></td>  
                </tr>  
                ';  
           }  
           $output .= '</table>';  
           return $output;  
      }       
 }  
 ?>  

action.php


 <?php  
 //action.php  
 include 'crud.php';  
 $object = new Crud();  
 if(isset($_POST["action"]))  
 {  
      if($_POST["action"] == "Load")  
      {  
           echo $object->get_data_in_table("SELECT * FROM users ORDER BY id DESC");  
      }  
 }  
 ?>  

1 comment:

  1. Why is your video been deleted ? what is really happening?

    ReplyDelete