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

PHP - Simple Pagination With MySQLi Using AngularJS

luckyguy983

WiFi Breaker
L Rep
0
0
0
Rep
0
L Vouches
0
0
0
Vouches
0
Posts
90
Likes
19
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
In this tutorial we will create a Simple Pagination With MySQLi Using AngularJS. This code will paginate the data in the database by applying AngularJS directives. The AngularJS will process a request to fetch the data in the database server in order to display it in the html table. This is a user-friendly program you can apply and modify this program to your system.

We will be using AngularJS as a comprehensive JavaScript framework that extend the browser capabilities. It handles the heavy lifting of DOM manipulation and AJAX request in a whole.

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html.

And this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Lastly, this is the link for the AngularJS https://angularjs.org/.

Creating Database

Open your database web server then create a database name in it db_page, after that click Import then locate the database file inside the folder of the application then click ok.
2019-07-27_23_56_27-localhost_127.0.0.1_db_page_phpmyadmin_4.8.3.png

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.
  1. <?php
  2. $conn

    =

    mysqli_connect

    (

    "localhost"

    ,

    "root"

    ,

    ""

    ,

    "db_update"

    )

    ;

  3. if

    (

    !

    $conn

    )

    {
  4. die

    (

    "Error: Failed to connect to database!"

    )

    ;
  5. }
  6. ?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into you text editor, then save it as index.php.
  1. <!

    DOCTYPE html>
  2. <

    html lang=

    "en"

    ng-

    app=

    "myModule"

    >
  3. <

    head>
  4. <

    meta charset=

    "UTF-8"

    name=

    "viewport"

    content=

    "width=device-width, initial-scale=1"

    />
  5. <

    link

    rel=

    "stylesheet"

    type=

    "text/css"

    href=

    "css/bootstrap.css"

    />
  6. </

    head>
  7. <

    body ng-

    controller=

    "myController"

    >
  8. <

    nav class

    =

    "navbar navbar-default"

    >
  9. <

    div class

    =

    "container-fluid"

    >
  10. <

    a class

    =

    "navbar-brand"

    href=

    "https://sourcecodester.com"

    >

    Sourcecodster</

    a>
  11. </

    div>
  12. </

    nav>
  13. <

    div class

    =

    "col-md-3"

    ></

    div>
  14. <

    div class

    =

    "col-md-6 well"

    >
  15. <

    h3 class

    =

    "text-primary"

    >

    PHP -

    Simple Pagination With MySQLi Using AngularJS</

    h3>
  16. <

    hr style=

    "border-top:1px dotted #ccc;"

    />
  17. <

    div class

    =

    "col-md-2"

    ></

    div>
  18. <

    div class

    =

    "col-md-8"

    >
  19. <

    table class

    =

    "table table-bordered"

    >
  20. <

    thead class

    =

    "alert-info"

    >
  21. <

    tr>
  22. <

    th ng-

    click=

    "sort('firstname')"

    >

    Firstname
  23. <

    span class

    =

    "glyphicon sort-icon"

    ng-

    show=

    "sortKey=='firstname'"

    ng-

    class

    =

    "{'glyphicon-chevron-up':reverse,'glyphicon-chevron-down':!reverse}"

    ></

    span>
  24. </

    th>
  25. <

    th ng-

    click=

    "sort('lastname')"

    >

    Lastname
  26. <

    span class

    =

    "glyphicon sort-icon"

    ng-

    show=

    "sortKey=='lastname'"

    ng-

    class

    =

    "{'glyphicon-chevron-up':reverse,'glyphicon-chevron-down':!reverse}"

    ></

    span>
  27. </

    th>
  28. <

    th ng-

    click=

    "sort('gender')"

    >

    Gender
  29. <

    span class

    =

    "glyphicon sort-icon"

    ng-

    show=

    "sortKey=='gender'"

    ng-

    class

    =

    "{'glyphicon-chevron-up':reverse,'glyphicon-chevron-down':!reverse}"

    ></

    span>
  30. </

    th>
  31. <

    th ng-

    click=

    "sort('address')"

    >

    Address
  32. <

    span class

    =

    "glyphicon sort-icon"

    ng-

    show=

    "sortKey=='address'"

    ng-

    class

    =

    "{'glyphicon-chevron-up':reverse,'glyphicon-chevron-down':!reverse}"

    ></

    span>
  33. </

    th>
  34. </

    tr>
  35. </

    thead>
  36. <

    tbody>
  37. <

    tr dir-

    paginate=

    "member in members|orderBy:'lastname'|orderBy:sortKey:reverse|itemsPerPage:5"

    >
  38. <

    td>

    {

    {

    member.

    firstname}

    }

    </

    td>
  39. <

    td>

    {

    {

    member.

    lastname}

    }

    </

    td>
  40. <

    td>

    {

    {

    member.

    gender}

    }

    </

    td>
  41. <

    td>

    {

    {

    member.

    address}

    }

    </

    td>
  42. </

    tr>
  43. </

    tbody>
  44. </

    table>
  45. <

    dir-

    pagination-

    controls
  46. direction-

    links=

    "true"
  47. boundary-

    links=

    "true"

    >
  48. </

    dir-

    pagination-

    controls>
  49. </

    div>
  50. </

    div>
  51. <

    script src=

    "js/angular.js"

    >

    </script>
  52. <

    script src=

    "js/dirPagination.js"

    >

    </script>
  53. <

    script src=

    "js/script.js"

    >

    </script>
  54. </

    body>
  55. </

    html>

Creating the Main Function

This code contains the main function of the application. This code will paginate the data using AngularJS directives. To do this just copy and write these block of codes as shown below inside the text editor and save it as shown below.
data.php
  1. <?php
  2. require_once

    'conn.php'

    ;

  3. $data

    =

    array

    (

    )

    ;


  4. $query

    =

    mysqli_query

    (

    $conn

    ,

    "SELECT * FROM `member`"

    )

    or die

    (

    mysqli_error

    (

    )

    )

    ;
  5. while

    (

    $fetch

    =

    mysqli_fetch_array

    (

    $query

    )

    )

    {
  6. $data

    [

    ]

    =

    $fetch

    ;
  7. }

  8. echo

    json_encode

    (

    $data

    )

    ;

  9. ?>

script.js
Note: Make sure you save this file inside the js directory in order the script works.
  1. var

    app =

    angular.module

    (

    'myModule'

    ,

    [

    'angularUtils.directives.dirPagination'

    ]

    )

    ;

  2. app.controller

    (

    'myController'

    ,

    function

    (

    $scope,

    $http)

    {


  3. $http.get

    (

    'data.php'

    )

    .then

    (

    function

    (

    response)

    {
  4. $scope.members

    =

    response.data

    ;
  5. }

    )

    ;

  6. $scope.sort

    =

    function

    (

    keyword)

    {
  7. $scope.sortKey

    =

    keyword;
  8. $scope.reverse

    =

    !

    $scope.reverse

    ;
  9. }
  10. }

    )

    ;

There you have it we successfully created a Simple Pagination With MySQLi using AngularJS. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!


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

452,292

323,341

323,350

Top