angularcode.com-Product Inventory management using AngularJS MySQL and PHP RESTful API
angularcode.com-Product Inventory management using AngularJS MySQL and PHP RESTful API
com/product-inventory-management-using-angularjs-mysql-and-php-restful-api/
Last tutorial we created a database helper class for PHP RESTful API . In this tutorial we will use that database
helper class to build a simple inventory/product manager application. We will add, update, delete, activate,
deactivate products from inventory. Some AngularJS directives will make our job easy.
Features
How to use
Download the project file from the download link provided above
Import the database file “products.sql” into MySQL database
Add your database settings to the file “config.php”
In this project I ignored the security part of the web application. Please do add some security features before
implementing into production.
Modules used
AngularJS Bootstrap UI modal (for product edit)
underscore.js (for some helper javascript functions)
PHP Slim to create our data provider / API
Requirement Specification
Application Structure
app
css
– bootstrap.min.css
– custom.css
– font-awesome.min.css
– angular.min.js
– angular-route.min.js
– angular-animate.min.js
– bootstrap.min.js
– jquery.min.js
– ui-bootstrap-tpls-0.11.2.min.js
– underscore.min.js
index.php [ This page is called when our application starts and everything starts from here ]
Let’s use our database helper function and create the API for database interactions
api/v1/dbHelper.php – Database helper functions created in previous tutorial for database access
api/v1/config.php – Database configurations [userid, password, server name]
api/v1/index.php – Select, Add, update, delete products
api/v1/index.php
require '.././libs/Slim/Slim.php';
require_once 'dbHelper.php';
\Slim\Slim::registerAutoloader();
$app =new \Slim\Slim();
=
$app \Slim\Slim::getInstance();
$db =new dbHelper();
()
$app->get('/products',function{
global $db;
$rows =$db->select("products",
"id,sku,name,description,price,mrp,stock,image,packing,status",array());
echoResponse(200,$rows);
});
)
$app->post('/products',function()use ($app {
=
$data json_decode( $app->request->getBody());
$mandatory =array('name');
global $db;
$rows =$db->insert("products",$data,$mandatory);
if($rows["status"]=="success")
] "Product added
$rows["message" = successfully." ;
echoResponse(200,$rows);
});
)
$app->put('/products/:id',function($id)use ($app {
=
$data json_decode( $app->request->getBody());
$condition =array('id'=>$id);
$mandatory =array();
global $db;
$rows =$db->update("products",$data,$condition,$mandatory);
if($rows["status"]=="success")
] "Product information updated
$rows["message" = successfully." ;
echoResponse(200,$rows);
});
)
$app->delete('/products/:id',function($id{
global $db;
$rows =$db->delete("products",array('id'=>$id));
if($rows["status"]=="success")
] "Product removed
$rows["message" = successfully." ;
echoResponse(200,$rows);
});
)
function echoResponse($status_code,$response {
global $app;
$app->status($status_code);
$app->contentType('application/json');
echo json_encode($response,JSON_NUMERIC_CHECK);
}
$app->run();
?>
Now our API is ready. Lets start building our application. In our index.html (start page) of our application we will
put the follwing div where all the partial pages will be served.
partials/products.html
Now we will display the list of products and add some simple animations for product change, the angular way
"c in products | filter:filterProduct | orderBy:'-
<tr ng-repeat=id'" id="{{c.id}}"
'c.packing + c.stock + c.price +
animate-on-change=c.description' ng-animate=
"
'animate'" >
<td>{{c.id}}</td><td>{{c.name}}</td><td>{{c.price}}</td><td>{{c.stock}}</
td><td>{{c.packing}}</td><td>{{c.description}}</td>
<td>
"{Active:'btn-success',
<button class="btn" ng-class=Inactive:''}1"
ng-click="changeProductStatus(c);">{{c.status}}</button>
</td>
<td style="width:100px">
<div class="btn-group">
"btn btn-default fa fa-
<button type="button" class=edit" ng-click=
"open(c);"></button>
"btn btn-danger fa fa-trash-
<button type="button" class=o" ng-click=
"deleteProduct(c);"></button>
</div>
</td>
</tr>
partials/productEdit.html
We are successful displaying the products which has got activate/deactivate and delete button. We are going to
embed our products into textboxes for editing
<div class="space"></div>
<div class="space-4"></div>
<div class="modal-footer">
<form-element label="">
<div class="text-right">
<a class="btn btn-sm" ng-click="cancel()"><i class=
"ace-icon fa fa-
reply" ></i>Cancel</a>
<button ng-click="saveProduct(product);"
ng-disabled=
"product_form.$invalid ||
enableUpdate"
class="btn btn-sm btn-primary"
type="submit">
"ace-icon fa fa-
<i class=check" ></i>{{buttonText}}
</button>
</div>
</form-element>
</div>
</form>
</div>
All the above partial pages will be controlled by our controller “productsCtrl”. Here we put the business logic of
adding, deleting, updating products as well as the user interactions
$scope.columns =
[
{text:"ID",predicate:"id",sortable:true,dataType:"number"},
{text:"Name",predicate:"name",sortable:true},
{text:"Price",predicate:"price",sortable:true},
{text:"Stock",predicate:"stock",sortable:true},
{text:"Packing",predicate:"packing",reverse:true,sortable:
true,dataType:"number"},
{text:"Description",predicate:"description",sortable:true},
{text:"Status",predicate:"status",sortable:true},
{text:"Action",predicate:"",sortable:false}
];
});
$scope.product =
angular.copy(item);
$scope.cancel ()
= function {
$modalInstance.dismiss('Close');
};
$scope.title = (item.id > 0) 'Edit 'Add
? Product' :Product' ;
$scope.buttonText = (item.id > 0) 'Update 'Add New
? Product' :Product' ;
original =
var item;
$scope.isClean ()
= function{
angular.equals(original,
return $scope.product);
}
$scope.saveProduct (product)
= function {
product.uid =
$scope.uid;
(product.id > 0)
if{
+product.id, (result)
Data.put('products/'product).then( function {
(result.status
if!= 'error'){
x =
var angular.copy(product);
x.save
= 'update';
$modalInstance.close(x);
}else{
console.log(result);
}
});
}else{
product.status
= 'Active';
, (result)
Data.post('products'product).then( function {
(result.status
if!= 'error'){
x =
var angular.copy(product);
x.save
= 'insert';
x.id =
result.data;
$modalInstance.close(x);
}else{
console.log(result);
}
});
}
};
});