AngularJS Search Suggestion with PHP/MySQLi and UI-Router

Getting Started

I've used CDN for Bootstrap, Angular JS and UI-Router so you need internet connection for them to work.

Creating our Database

First, we're gonna create our MySQL Database where we get our data. 1. Open phpMyAdmin. 2. Click databases, create a database and name it as angular. 3. After creating a database, click the SQL and paste the below codes. See image below for detailed instruction.
  1. CREATE TABLE `members` (
  2. `memid` int(11) NOT NULL AUTO_INCREMENT,
  3. `firstname` varchar(30) NOT NULL,
  4. `lastname` varchar(30) NOT NULL,
  5. `address` text NOT NULL,
  6. PRIMARY KEY(`memid`)
  1. INSERT INTO `members` (`memid`, `firstname`, `lastname`, `address`) VALUES
  2. (1, 'Neovic', 'Devierte', 'Silay City'),
  3. (2, 'Julyn', 'Divinagracia', 'E.B. Magalona'),
  4. (3, 'Gemalyn', 'Cepe', 'Bohol'),
  5. (4, 'Tintin ', 'Demapanag', 'Talisy City'),
  6. (5, 'Tintin ', 'Devierte', 'Silay City');
database sql

index.html

This is the main view of our app.
  1. <!DOCTYPE html>
  2. <html ng-app="app">
  3. <title>AngularJS Search Suggestion with PHP/MySQLi</title>
  4. <meta charset="utf-8">
  5. <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
  6. <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script>
  7. <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/1.0.3/angular-ui-router.min.js"></script>
  8. <style type="text/css">
  9. .sug-list{
  10. background-color: #4CAF50;
  11. border: 1px solid #FFFFFF;
  12. color: #FFFFFF;
  13. padding: 10px 24px;
  14. cursor: pointer;
  15. width: 100%;
  16. display: block;
  17. margin-top:1px;
  18. }
  19. .sug-list:not(:last-child) {
  20. border-bottom: none;
  21. }
  22. .sug-list a {
  23. text-decoration:none;
  24. color: #FFFFFF;
  25. }
  26. .no-sug{
  27. background-color: #4CAF50;
  28. border: 1px solid #FFFFFF;
  29. color: #FFFFFF;
  30. padding: 10px 24px;
  31. width: 100%;
  32. display: block;
  33. margin-top:1px;
  34. }
  35. </style>
  36. </head>
  37. <div class="container">
  38. <h1 class="page-header text-center">AngularJS Search Suggestion with PHP/MySQLi</h1>
  39. <div ui-view></div>
  40. </div>
  41. <script src="app.js"></script>
  42. </body>
  43. </html>

app.js

This is the main angular js script of our app.
  1. var app = angular.module('app', ['ui.router']);
  2.  
  3. app.config(function($stateProvider, $urlRouterProvider) {
  4.  
  5. $urlRouterProvider.otherwise('/home');
  6.  
  7. $stateProvider
  8.  
  9. .state('home', {
  10. url: '/home',
  11. templateUrl: 'home.html',
  12. controller: 'homeCtrl'
  13. })
  14.  
  15. .state('member', {
  16. url: '/member/{member:json}',
  17. params: {member: null},
  18. templateUrl: 'member.html',
  19. controller: 'memberCtrl'
  20. })
  21. });
  22.  
  23. app.controller('homeCtrl', function($scope, $http) {
  24. $scope.hasMember = false;
  25. $scope.noMember = false;
  26.  
  27. $scope.fetch = function(){
  28. $http.get('fetch.php').success(function(data){
  29. $scope.memberLists = data;
  30. });
  31. }
  32.  
  33. $scope.search = function(){
  34. if($scope.keyword == ''){
  35. $scope.hasMember = false;
  36. $scope.noMember = false;
  37. }
  38. else{
  39. $http.post('search.php', {
  40. 'keyword': $scope.keyword
  41. })
  42. .success(function(data){
  43. if(data.length > 0){
  44. $scope.members = data;
  45. $scope.hasMember = true;
  46. $scope.noMember = false;
  47. }
  48. else{
  49. $scope.noMember = true;
  50. $scope.hasMember = false;
  51. }
  52. });
  53. }
  54. }
  55. });
  56.  
  57. app.controller('memberCtrl', function($scope, $stateParams) {
  58. $scope.details = $stateParams.member;
  59. });

fetch.php

This is our PHP api that fetches data from our MySQL database.
  1. <?php
  2.  
  3. $conn = new mysqli("localhost", "root", "", "angular");
  4.  
  5. $out = array();
  6.  
  7. $sql = "SELECT * FROM members";
  8. $query = $conn->query($sql);
  9.  
  10. while($row=$query->fetch_array()){
  11. $out[] = $row;
  12. }
  13.  
  14. echo json_encode($out);
  15.  
  16. ?>

home.html

As per our ui-router this is treated as our index view.
  1. <div class="row">
  2. <div class="col-sm-6 col-sm-offset-1" ng-init="fetch()">
  3. <h3>Member List</h3>
  4. <table class="table table-bordered table-striped">
  5. <tr>
  6. <th>Member ID</th>
  7. <th>Firstname</th>
  8. <th>Lastname</th>
  9. <th>Address</th>
  10. </tr>
  11. </thead>
  12. <tr ng-repeat="memberList in memberLists">
  13. <td>{{ memberList.memid }}</td>
  14. <td>{{ memberList.firstname }}</td>
  15. <td>{{ memberList.lastname }}</td>
  16. <td>{{ memberList.address }}</td>
  17. </tr>
  18. </tbody>
  19. </table>
  20. </div>
  21. <div class="col-sm-4">
  22. <input type="text" class="form-control" ng-model="keyword" ng-keyup="search()" placeholder="Search for Firstname, Lastname or Address">
  23. <div ng-repeat="member in members" ng-show="hasMember" class="sug-list">
  24. <a ui-sref="member({member: member})">{{ member.firstname + ' ' + member.lastname }}</a>
  25. </div>
  26. <div ng-show="noMember" class="no-sug">
  27. No Suggestion Found
  28. </div>
  29. </div>
  30. </div>

search.php

This is our PHP api that searches our MySQL database and returning the search match.
  1. <?php
  2.  
  3. $conn = new mysqli("localhost", "root", "", "angular");
  4.  
  5. $out = array();
  6.  
  7. $data = json_decode(file_get_contents('php://input'));
  8.  
  9. $keyword = $data->keyword;
  10.  
  11. if(empty($keyword)){
  12. $out = '';
  13. }
  14. else{
  15.  
  16. $sql = "SELECT * FROM members WHERE firstname LIKE '%$keyword%' OR lastname LIKE '%$keyword%' OR address LIKE '%$keyword%'";
  17. $query = $conn->query($sql);
  18.  
  19. while($row=$query->fetch_array()){
  20. $out[] = $row;
  21. }
  22. }
  23.  
  24. echo json_encode($out);
  25.  
  26. ?>

member.html

Lastly, this is where we view the details of our clicked search result.
  1. <h2 class="text-center">Member Details</h1>
  2. <div class="col-sm-4 col-sm-offset-4">
  3. <h3>Member ID: {{ details.memid }}</h3>
  4. <h3>Firstname: {{ details.firstname }}</h3>
  5. <h3>Lastname: {{ details.lastname }}</h3>
  6. <h3>Address: {{ details.address }}</h3>
  7.  
  8. <button type="button" ui-sref="home" class="btn btn-primary">Back</button>
That ends this tutorial. Happy Coding :)

Add new comment