• We just launched and are currently in beta. Join us as we build and grow the community.

AngularJS Search Suggestion with PHP/MySQLi and UI-Router

leloup94

Cross-Site Scripting Pro
L Rep
0
0
0
Rep
0
L Vouches
0
0
0
Vouches
0
Posts
41
Likes
185
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
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`

    )
  7. )

    ENGINE

    =

    InnoDB

    DEFAULT

    CHARSET

    =

    latin1;

  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_6_55.png

index.html

This is the main view of our app.

  1. <!DOCTYPE html>
  2. <html

    ng-app=

    "app"

    >
  3. <head

    >
  4. <title

    >

    AngularJS Search Suggestion with PHP/MySQLi</

    title

    >
  5. <meta

    charset

    =

    "utf-8"

    >
  6. <link

    href

    =

    "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"

    rel

    =

    "stylesheet"

    >
  7. <script

    src

    =

    "http://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"

    ></

    script

    >
  8. <script

    src

    =

    "https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/1.0.3/angular-ui-router.min.js"

    ></

    script

    >
  9. <style

    type

    =

    "text/css"

    >
  10. .sug-list{
  11. background-color: #4CAF50;
  12. border: 1px solid #FFFFFF;
  13. color: #FFFFFF;
  14. padding: 10px 24px;
  15. cursor: pointer;
  16. width: 100%;
  17. display: block;
  18. margin-top:1px;
  19. }
  20. .sug-list:not(:last-child) {
  21. border-bottom: none;
  22. }
  23. .sug-list a {
  24. text-decoration:none;
  25. color: #FFFFFF;
  26. }
  27. .no-sug{
  28. background-color: #4CAF50;
  29. border: 1px solid #FFFFFF;
  30. color: #FFFFFF;
  31. padding: 10px 24px;
  32. width: 100%;
  33. display: block;
  34. margin-top:1px;
  35. }
  36. </

    style

    >
  37. </

    head

    >
  38. <body

    >
  39. <div

    class

    =

    "container"

    >
  40. <h1

    class

    =

    "page-header text-center"

    >

    AngularJS Search Suggestion with PHP/MySQLi</

    h1

    >
  41. <div

    ui-view></

    div

    >
  42. </

    div

    >
  43. <script

    src

    =

    "app.js"

    ></

    script

    >
  44. </

    body

    >
  45. </

    html

    >

app.js

This is the main angular js script of our app.

  1. var

    app =

    angular.module

    (

    'app'

    ,

    [

    'ui.router'

    ]

    )

    ;

  2. app.config

    (

    function

    (

    $stateProvider,

    $urlRouterProvider)

    {

  3. $urlRouterProvider.otherwise

    (

    '/home'

    )

    ;

  4. $stateProvider

  5. .state

    (

    'home'

    ,

    {
  6. url:

    '/home'

    ,
  7. templateUrl:

    'home.html'

    ,
  8. controller:

    'homeCtrl'
  9. }

    )

  10. .state

    (

    'member'

    ,

    {
  11. url:

    '/member/{member:json}'

    ,
  12. params:

    {

    member:

    null

    }

    ,
  13. templateUrl:

    'member.html'

    ,
  14. controller:

    'memberCtrl'
  15. }

    )
  16. }

    )

    ;

  17. app.controller

    (

    'homeCtrl'

    ,

    function

    (

    $scope,

    $http)

    {
  18. $scope.hasMember

    =

    false

    ;
  19. $scope.noMember

    =

    false

    ;

  20. $scope.fetch

    =

    function

    (

    )

    {
  21. $http.get

    (

    'fetch.php'

    )

    .success

    (

    function

    (

    data)

    {
  22. $scope.memberLists

    =

    data;
  23. }

    )

    ;
  24. }

  25. $scope.search

    =

    function

    (

    )

    {
  26. if

    (

    $scope.keyword

    ==

    ''

    )

    {
  27. $scope.hasMember

    =

    false

    ;
  28. $scope.noMember

    =

    false

    ;
  29. }
  30. else

    {
  31. $http.post

    (

    'search.php'

    ,

    {
  32. 'keyword'

    :

    $scope.keyword
  33. }

    )
  34. .success

    (

    function

    (

    data)

    {
  35. if

    (

    data.length

    >

    0

    )

    {
  36. $scope.members

    =

    data;
  37. $scope.hasMember

    =

    true

    ;
  38. $scope.noMember

    =

    false

    ;
  39. }
  40. else

    {
  41. $scope.noMember

    =

    true

    ;
  42. $scope.hasMember

    =

    false

    ;
  43. }
  44. }

    )

    ;
  45. }
  46. }
  47. }

    )

    ;

  48. app.controller

    (

    'memberCtrl'

    ,

    function

    (

    $scope,

    $stateParams)

    {
  49. $scope.details

    =

    $stateParams.member

    ;
  50. }

    )

    ;

fetch.php

This is our PHP api that fetches data from our MySQL database.

  1. <?php

  2. $conn

    =

    new

    mysqli(

    "localhost"

    ,

    "root"

    ,

    ""

    ,

    "angular"

    )

    ;

  3. $out

    =

    array

    (

    )

    ;

  4. $sql

    =

    "SELECT * FROM members"

    ;
  5. $query

    =

    $conn

    ->

    query

    (

    $sql

    )

    ;

  6. while

    (

    $row

    =

    $query

    ->

    fetch_array

    (

    )

    )

    {
  7. $out

    [

    ]

    =

    $row

    ;
  8. }

  9. echo

    json_encode

    (

    $out

    )

    ;

  10. ?>

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. <thead

    >
  6. <tr

    >
  7. <th

    >

    Member ID</

    th

    >
  8. <th

    >

    Firstname</

    th

    >
  9. <th

    >

    Lastname</

    th

    >
  10. <th

    >

    Address</

    th

    >
  11. </

    tr

    >
  12. </

    thead

    >
  13. <tbody

    >
  14. <tr

    ng-repeat=

    "memberList in memberLists"

    >
  15. <td

    >

    {{ memberList.memid }}</

    td

    >
  16. <td

    >

    {{ memberList.firstname }}</

    td

    >
  17. <td

    >

    {{ memberList.lastname }}</

    td

    >
  18. <td

    >

    {{ memberList.address }}</

    td

    >
  19. </

    tr

    >
  20. </

    tbody

    >
  21. </

    table

    >
  22. </

    div

    >
  23. <div

    class

    =

    "col-sm-4"

    >
  24. <input

    type

    =

    "text"

    class

    =

    "form-control"

    ng-model=

    "keyword"

    ng-keyup=

    "search()"

    placeholder=

    "Search for Firstname, Lastname or Address"

    >
  25. <div

    ng-repeat=

    "member in members"

    ng-show=

    "hasMember"

    class

    =

    "sug-list"

    >
  26. <a

    ui-sref=

    "member({member: member})"

    >

    {{ member.firstname + ' ' + member.lastname }}</

    a

    >
  27. </

    div

    >
  28. <div

    ng-show=

    "noMember"

    class

    =

    "no-sug"

    >
  29. No Suggestion Found
  30. </

    div

    >
  31. </

    div

    >
  32. </

    div

    >

search.php

This is our PHP api that searches our MySQL database and returning the search match.

  1. <?php

  2. $conn

    =

    new

    mysqli(

    "localhost"

    ,

    "root"

    ,

    ""

    ,

    "angular"

    )

    ;

  3. $out

    =

    array

    (

    )

    ;

  4. $data

    =

    json_decode

    (

    file_get_contents

    (

    'php://input'

    )

    )

    ;

  5. $keyword

    =

    $data

    ->

    keyword

    ;

  6. if

    (

    empty

    (

    $keyword

    )

    )

    {
  7. $out

    =

    ''

    ;
  8. }
  9. else

    {

  10. $sql

    =

    "SELECT * FROM members WHERE firstname LIKE '%$keyword

    %' OR lastname LIKE '%$keyword

    %' OR address LIKE '%$keyword

    %'"

    ;
  11. $query

    =

    $conn

    ->

    query

    (

    $sql

    )

    ;

  12. while

    (

    $row

    =

    $query

    ->

    fetch_array

    (

    )

    )

    {
  13. $out

    [

    ]

    =

    $row

    ;
  14. }
  15. }

  16. echo

    json_encode

    (

    $out

    )

    ;

  17. ?>

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. <button

    type

    =

    "button"

    ui-sref=

    "home"

    class

    =

    "btn btn-primary"

    >

    Back</

    button

    >

That ends this tutorial. Happy Coding :)


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,340

323,349

Top