PHP - Simple Sort MySQLi Data Using AngularJS

In this tutorial we will create a Simple Sort MySQLi Data Using AngularJS. This code will sort the MySQLi data in the table when user submit the keyword. The code use angular directives that enable you to sort the MySQLi table by using orderBy with the assign value of a keyword. This a user-friendly program feel free to modify and use it to your system. We will be using AngularJS as a framework which has additional custom HTML attributes embedded into it. It can interprets those attributes as directives to bind inputted parts of the page to a model that represent as a standard JavaScript variables.

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 bootstrap that i used for the layout design https://getbootstrap.com/. Lastly, this is the link for the AngularJS https://angularjs.org/.

Creating Database

Open your database web server then create a database name in it db_angular_insert, 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_angular_insert");
  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 you text editor, then save it index.php.
  1. <!DOCTYPE html>
  2. <html lang="en" ng-app="myModule">
  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 ng-controller="myController">
  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 Sort MySQLi Data Using AngularJS</h3>
  16.                 <hr style="border-top:1px dotted #ccc;">
  17.                 <div class="col-md-8">
  18.                         <div class="form-inline">
  19.                                 <label>Order By:</label>
  20.                                 <select class="form-control" ng-model="sort">
  21.                                         <option ng-repeat="x in keyword">{{x}}</option>
  22.                                 </select>
  23.                                 <button class="btn btn-primary" ng-click="sortBy()">Sort</button>
  24.                         </div>
  25.                         <br />
  26.                         <table class = "table table-bordered">
  27.                                 <thead class="alert-info">
  28.                                         <tr>
  29.                                                 <th>Firstname</th>
  30.                                                 <th>Lastname</th>
  31.                                                 <th>Address</th>
  32.                                         </tr>
  33.                                 </thead>
  34.                                 <tbody>
  35.                                         <tr ng-repeat = "member in members | orderBy: sortKey">
  36.                                                 <td>{{member.firstname}}</td>
  37.                                                 <td>{{member.lastname}}</td>
  38.                                                 <td>{{member.address}}</td>
  39.                                         </tr>
  40.                                 </tbody>
  41.                         </table>
  42.                 </div>
  43.                 <div class="col-md-4">
  44.                         <form>
  45.                                 <div class="form-inline">
  46.                                         <input type="text" class="form-control" ng-model="firstname" placeholder="Firstname"/>
  47.                                         <input type="text" class="form-control" ng-model="lastname" placeholder="Lastname"/>
  48.                                         <input type="text" class="form-control" ng-model="address" placeholder="Address"/>
  49.                                         <br /><br />
  50.                                         <center><button type = "button" class = "btn btn-primary" ng-click = "insertData()" ><span class = "glyphicon glyphicon-save"></span> Insert</button></center>
  51.                                 </div>
  52.                         </form>
  53.                 </div>
  54.         </div>
  55. <script src = "js/angular.js"></script>
  56. <script src = "js/script.js"></script>
  57. </body>
  58. </html>

Creating the Main Function

This code contains the main function of the application. This code will sort the MySQLi data when the button is clicked. To do this just copy and write these block of codes as shown below inside the text editor and save it as shown below. insert.php
  1. <?php
  2.         require_once 'conn.php';
  3.         $data = json_decode(file_get_contents("php://input"));
  4.        
  5.         $firstname = $data->firstname;
  6.         $lastname = $data->lastname;
  7.         $address = $data->address;
  8.  
  9.        
  10.         mysqli_query($conn, "INSERT INTO `member` VALUES('', '$firstname', '$lastname', '$address')") or die(mysqli_error());
  11. ?>
data.php
  1. <?php
  2.         require_once 'conn.php';
  3.        
  4.         $data = array();
  5.        
  6.         $query=mysqli_query($conn, "SELECT * FROM `member`") or die(mysqli_error());
  7.        
  8.         while($fetch = mysqli_fetch_array($query)){
  9.                 $data[] = $fetch;
  10.         }
  11.        
  12.         echo json_encode($data);
  13.  
  14. ?>
script.js Note: Make sure you save this file inside the js directory in order the script works.
  1. var app = angular.module("myModule", [])
  2.                                 .controller("myController", function($scope, $http, $timeout){
  3.                                         $http.get('data.php').then(function(response){
  4.                                                 $scope.members = response.data;
  5.                                         });
  6.                                        
  7.                                         $scope.insertData = function(){
  8.                                                 if($scope.firstname == null || $scope.lastname == null || $scope.address == null){
  9.                                                         alert("Please complete the required field");
  10.                                                 }else{
  11.                                                         $http.post("insert.php", {firstname: $scope.firstname, lastname: $scope.lastname, address: $scope.address})
  12.                                                         .then(function(){
  13.                                                                 $scope.firstname = "";
  14.                                                                 $scope.lastname = "";
  15.                                                                 $scope.address = "";
  16.                                                                 $scope.getData();
  17.                                                         });    
  18.                                                 }
  19.                                                
  20.                                         }
  21.                                        
  22.                                         $scope.getData = function(){
  23.                                                 $http.get('data.php').then(function(response){
  24.                                                         $scope.members = response.data;
  25.                                                 });
  26.                                         }
  27.                                        
  28.                                         var keyword = ["firstname", "lastname", "address"];
  29.                                                
  30.                                         $scope.keyword = keyword;
  31.                                        
  32.                                         $scope.sortBy =  function(){
  33.                                                 $scope.sortKey = $scope.sort;
  34.                                         }
  35.                                 });
There you have it we successfully created a Simple Sort MySQLi Data using AngularJS. 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