PHP AngularJS CRUD With Search and Pagination Example From Scratch
PHP AngularJS CRUD With Search and Pagination Example From Scratch
In first step we should create database and items table. so let's create database i did create "learn"
database and "items" table inside that database. so you can create database as you want but you
have to craete "items" table if you are doing from scratch. so create "items" table using following
mysql query:
Ok, let's proceed this way, we are doing from scratch so we require to create database
configration file that way we can use that file in many other file. so let's craete db_config.php file
in your root directory and put bellow code:
db_config.php
Ok, now we also require to create index.php file in our root directory. so let's create index.php
file and put bellow content in that file.
index.php
<html lang="en">
<head>
<link rel="stylesheet"
href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css">
<script
src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/twitter-
bootstrap/3.3.1/js/bootstrap.min.js"></script>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></s
cript>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular-
route.min.js"></script>
<script src="app/packages/dirPagination.js"></script>
<script src="app/routes.js"></script>
<script src="app/services/myServices.js"></script>
<script src="app/helper/myHelper.js"></script>
<script src="app/controllers/ItemController.js"></script>
</head>
<body ng-app="main-App">
<div class="container">
<ng-view></ng-view>
</div>
</body>
</html>
we will create angularJS code in this step, so first we have to create "app" folder in your root
directory. we will do whole angular JS code in this folder that way we can eaily re-use this code.
Next create routes.js file inside app folder. we will create all angular js route in routes.js file. so
create routes.js file and put bellow code in routes.js file.
app/routes.js
app.config(['$routeProvider',
function($routeProvider) {
$routeProvider.
when('/', {
templateUrl: 'templates/items.html',
controller: 'ItemController'
});
}]);
now we also require to create controller for angular route. so create another directory as
controller(app/controller) in app folder and also create
ItemController.js(app/controller/ItemController.js) for manage route request. So, let's put
bellow code in that file.
app/controller/ItemController.js
app.controller('ItemController', function(dataFactory,$scope,$http){
$scope.data = [];
$scope.libraryTemp = {};
$scope.totalItemsTemp = {};
$scope.totalItems = 0;
$scope.pageChanged = function(newPage) {
getResultsPage(newPage);
};
getResultsPage(1);
function getResultsPage(pageNumber) {
if(! $.isEmptyObject($scope.libraryTemp)){
dataFactory.httpRequest(URL + '/api/getData.php?search='+
$scope.searchText+'&page='+pageNumber).then(function(data) {
$scope.data = data.data;
$scope.totalItems = data.total;
});
}else{
dataFactory.httpRequest(URL + '/api/getData.php?
page='+pageNumber).then(function(data) {
$scope.data = data.data;
$scope.totalItems = data.total;
});
$scope.searchDB = function(){
if($.isEmptyObject($scope.libraryTemp)){
$scope.libraryTemp = $scope.data;
$scope.totalItemsTemp = $scope.totalItems;
$scope.data = {};
getResultsPage(1);
}else{
if(! $.isEmptyObject($scope.libraryTemp)){
$scope.data = $scope.libraryTemp ;
$scope.totalItems = $scope.totalItemsTemp;
$scope.libraryTemp = {};
$scope.saveAdd = function(){
dataFactory.httpRequest(URL + '/api/add.php','POST',{},
$scope.form).then(function(data) {
$scope.data.push(data);
$(".modal").modal("hide");
});
$scope.edit = function(id){
dataFactory.httpRequest(URL + '/api/edit.php?id='+id).then(function(data)
{
console.log(data);
$scope.form = data;
});
}
$scope.saveEdit = function(){
dataFactory.httpRequest(URL + '/api/update.php?id='+$scope.form.id,'POST',
{},$scope.form).then(function(data) {
$(".modal").modal("hide");
$scope.data = apiModifyTable($scope.data,data.id,data);
});
$scope.remove = function(item,index){
if (result) {
dataFactory.httpRequest(URL + '/api/delete.php?
id='+item.id,'DELETE').then(function(data) {
$scope.data.splice(index,1);
});
});
next we need to create helper(app/helper) folder in add directory. we are creating helper folder
because we store all helper function we can manage in this folder. so let's create also
myHelper.js(app/helper/myHelper.js) file in helper folder. now i did just one helper for modify
table but you can add more helper function for your angulerJS application.
app/helper/myHelper.js
function apiModifyTable(originalData,id,response){
if(item.id == id){
originalData[key] = response;
}
});
return originalData;
Now create another folder "services"(app/services) in your app directory. in this folder we will
store our service file for example i did use app.factory of angularjs. that way we can manage it
proper way. so let's create myServices.js(app/services/myServices.js) file and put bellow code
in that file.
app/services/myServices.js
app.factory('dataFactory', function($http) {
var myService = {
httpRequest: function(url,method,params,dataPost,upload) {
passParameters.url = url;
passParameters.method = 'GET';
}else{
passParameters.method = method;
passParameters.params = params;
passParameters.data = dataPost;
passParameters.upload = upload;
}
var promise = $http(passParameters).then(function (response) {
if(response.data.substr('loginMark')){
location.reload();
return;
$.gritter.add({
title: 'Application',
text: response.data
});
return false;
if(response.data.jsMessage){
$.gritter.add({
title: response.data.jsTitle,
text: response.data.jsMessage
});
return response.data;
},function(){
$.gritter.add({
title: 'Application',
});
});
return promise;
}
};
return myService;
});
Ok, now create one another folder "packages", we are creating packages folder because we can
store all package file store in this folder. now i use package for pagination, so create file
dirPagination.js(app/packages/dirPagination.js) and put code of following link:
app/packages/dirPagination.js
In this step we will create template files. when we call angular js route then it will fetch one
template file for layout so we have to create "templates" directory in your root file.
First craete itesms.html file for items module layout so carete items.html file and put bellow
code.
templates/items.html
<div class="row">
<div class="pull-left">
</div>
<div class="input-group">
<span class="input-group-addon">Search</span>
</div>
</div>
<button class="btn btn-success" data-toggle="modal" data-
target="#create-user">Create New</button>
</div>
</div>
</div>
<thead>
<tr>
<th>No</th>
<th>Title</th>
<th>Description</th>
<th width="220px">Action</th>
</tr>
</thead>
<tbody>
<td>
</td>
</tr>
</tbody>
</table>
<div class="modal-content">
<div class="modal-header">
</div>
<div class="modal-body">
<div class="container">
<div class="row">
<strong>Title : </strong>
<div class="form-group">
</div>
</div>
<strong>Description : </strong>
</textarea>
</div>
</div>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
<div class="modal-content">
<div class="modal-header">
</div>
<div class="modal-body">
<div class="container">
<div class="row">
<div class="form-group">
</div>
</div>
<div class="form-group">
<textarea ng-model="form.description"
class="form-control" required>
</textarea>
</div>
</div>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
Last craete dirPagination.html file for pagination layout and put following code in that file.
templates/dirPagination.html
</li>
</li>
</li>
</li>
</li>
</ul>
api/getData.php
require '../db_config.php';
$num_rec_per_page = 5;
if (!empty($_GET["search"])){
}else{
$result = $mysqli->query($sql);
while($row = $result->fetch_assoc()){
$json[] = $row;
$data['data'] = $json;
$result = mysqli_query($mysqli,$sqlTotal);
$data['total'] = mysqli_num_rows($result);
echo json_encode($data);
api/add.php
require '../db_config.php';
$post = file_get_contents('php://input');
$post = json_decode($post);
VALUES ('".$post->title."','".$post->description."')";
$result = $mysqli->query($sql);
$result = $mysqli->query($sql);
$data = $result->fetch_assoc();
echo json_encode($data);
api/edit.php
require '../db_config.php';
$id = $_GET["id"];
$result = $mysqli->query($sql);
$data = $result->fetch_assoc();
echo json_encode($data);
api/update.php
require '../db_config.php';
$id = $_GET["id"];
$post = file_get_contents('php://input');
$post = json_decode($post);
,description = '".$post->description."'
WHERE id = '".$id."'";
$result = $mysqli->query($sql);
$result = $mysqli->query($sql);
$data = $result->fetch_assoc();
echo json_encode($data);
api/delete.php
require '../db_config.php';
$id = $_GET["id"];
$result = $mysqli->query($sql);
echo json_encode([$id]);