Wednesday, March 9, 2016

Sample application with AngularJS, PHP and Mysql

AngularJS sample application with PHP and MYSQL

Lets start by creating a sample database and a table to have some values which can be used for listing the data.

AngularJS PHP MYSQL



CREATE TABLE `movies` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `title` VARCHAR(255) NOT NULL, `director` VARCHAR(15) NOT NULL, `year` int(10) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=INNODB DEFAULT CHARSET=utf8;


Then some default entries to the table

INSERT INTO `movies` (`id`, `title`, `director`, `year`) VALUES (1, 'Mad Max: Fury Road', 'George Miller', '2015'), (2, 'Captain Phillips', 'Paul Greengrass', '2013'), (3, 'Room', 'Lenny Abrahamson', '2015') , (4, 'London Has Fallen', 'Babak Najafi', '2016') , (5, 'Deadpool', 'Tim Miller', '2016');


Now we have the database ready to be used, shall start from building the html pages, js and relative php file.

index.html

<!DOCTYPE html>

<html ng-app="customMovApp">

<head>

 <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.min.js"></script>

 <script type="text/javascript" src="js/app.js"></script>

</head>

        <body ng-controller="movieController">

                <div ng-include src=" 'views/movie.html' "></div>

        </body>

</html>

In angularjs based applications we use attribute names with ng keyword. As you can see in the html tag we have an attribute ng-app been set to a value 'customMovApp' and this is to initialize the html page with anuglarjs app which we will be defining on the app.js file. Within the body tag we have also initialized to controller to be used for this page with ng-controller attribute. Then we have a content loading div section which uses another view to be included when the index page is loaded. Here, note that the source url used within the ng-include attribute is enclosed within single quotes.

Now we shall define the app.js file which will be having the core of angular js application.

app.js

var app = angular.module('customMovApp',[]);

app.controller('movieController',  function($scope, $http) {

 getMovie();

 function getMovie() {

  $http.post("getMovie.php").success(function(data) {

   console.log(data);

   $scope.movies = data;

  });

 }

});


Here from the 1st line angular module is been initialized as 'customMovApp' and then with that we have created a new controller named 'movieController' and it has a function 'getMovie' which is in turn defined to call the php file getMovie.php and set the returned json object to 'movies' variable.

We already have a database and a table with some sample entries, we have the php script to connect to the database and returns the response as json object. For this we have a sample script which can be used.

getMovie.php

// mysqli connection object initialization
$mysqli = new mysqli('host', 'username', 'password', 'dbname');

$query = "select * from movies";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
$arr     = array();

if($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
                $arr[] = $row;
        }

}

// JSON-encoded response
echo $json_response = json_encode($arr);


Now we need a view page to render the response from the controller action of angular module.

movie.html

<table ng-controller="movieController">

<thead><tr><th>Title</th><th>Director</th><th>Released Year</th></tr></thead>

<tbody>
 <tr ng-repeat="movie in movies">

 <td>{{movie.title}}</td>
 <td>{{movie.director}}</td>
 <td>
    <div ng-if="movie.year == '2016'"> <span>Current Year</span> </div>
 <div ng-if="movie.year != '2016'"> <span>{{movie.year}}</span> </div>
 </td>

 </tr>

</tbody> </table>

Here we are using ng-repeat to loop through all the movie records from the response object.

No comments:

Post a Comment