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

PHP Selecting MySQL Row by Category using jQuery

AxelGamingR0

Digital Collaboration Strategist
A Rep
0
0
0
Rep
0
A Vouches
0
0
0
Vouches
0
Posts
39
Likes
88
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 500 XP
In this tutorial, I'm going to show you how to create a simple select of MySQL row by category using jQuery. This tutorial features a select tag that whenever we change the value of a category the page or the data from table automatically change based on the chosen category or value of select tag. So, let's get started.

Creating our Database

First step is to create our database.
1. Open phpMyAdmin.
2. Click databases, create a database and name it as "category".
3. After creating a database, click the SQL and paste the below code. See image below for detailed instruction.

  1. CREATE

    TABLE

    IF

    NOT

    EXISTS

    `category`

    (
  2. `categoryid`

    INT

    (

    11

    )

    NOT

    NULL

    AUTO_INCREMENT

    ,
  3. `cat_description`

    VARCHAR

    (

    30

    )

    NOT

    NULL

    ,
  4. PRIMARY

    KEY

    (

    `categoryid`

    )
  5. )

    ENGINE=

    InnoDB AUTO_INCREMENT

    =

    5

    DEFAULT

    CHARSET=

    latin1;

  1. CREATE

    TABLE

    IF

    NOT

    EXISTS

    `material`

    (
  2. `materialid`

    INT

    (

    11

    )

    NOT

    NULL

    AUTO_INCREMENT

    ,
  3. `categoryid`

    INT

    (

    11

    )

    NOT

    NULL

    ,
  4. `material_name`

    VARCHAR

    (

    30

    )

    NOT

    NULL

    ,
  5. PRIMARY

    KEY

    (

    `materialid`

    )
  6. )

    ENGINE=

    InnoDB AUTO_INCREMENT

    =

    9

    DEFAULT

    CHARSET=

    latin1;

database_6_8_1.png

Inserting Sample Data into our Database

Next is to insert sample data into the database that we have created.
1. Click "category" database that we have created.
2. Click SQL and paste the code below.

  1. INSERT

    INTO

    `category`

    (

    `categoryid`

    ,

    `cat_description`

    )

    VALUES
  2. (

    1

    ,

    'Masonry'

    )

    ,
  3. (

    2

    ,

    'Carpentry'

    )

    ,
  4. (

    3

    ,

    'Wood Works'

    )

    ,
  5. (

    4

    ,

    'Steel Works'

    )

    ;

  1. INSERT

    INTO

    `material`

    (

    `materialid`

    ,

    `categoryid`

    ,

    `material_name`

    )

    VALUES
  2. (

    1

    ,

    1

    ,

    'Deformed Bar 10mm x 6m'

    )

    ,
  3. (

    2

    ,

    1

    ,

    'Deformed Bar 12mm x 6m'

    )

    ,
  4. (

    3

    ,

    2

    ,

    'Concrete Hollow Blocks, 4'

    )

    ,
  5. (

    4

    ,

    2

    ,

    'Portland Cement'

    )

    ,
  6. (

    5

    ,

    3

    ,

    'Mixing Sand'

    )

    ,
  7. (

    6

    ,

    3

    ,

    'Washed Sand'

    )

    ,
  8. (

    7

    ,

    4

    ,

    'G.I. Tie Wire'

    )

    ,
  9. (

    8

    ,

    4

    ,

    'C.W. Nail'

    )

    ;

Creating our Connection

Next step is to create a database connection and save it as "conn.php". This file will serve as our bridge between our form and our database. To create the file, open your HTML code editor and paste the code below after the tag.

  1. <?php
  2. $conn

    =

    mysqli_connect

    (

    "localhost"

    ,

    "root"

    ,

    ""

    ,

    "category"

    )

    ;

  3. if

    (

    !

    $conn

    )

    {
  4. die

    (

    "Connection failed: "

    .

    mysqli_connect_error

    (

    )

    )

    ;
  5. }
  6. ?>

Creating our Sample Table

Lastly, we create our sample table with our category selector. We name this as "index.php".

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>PHP Selecting MySQL Row by Category using jQuery</title>
  5. <link href="css/bootstrap.css" rel="stylesheet">
  6. <script src="js/bootstrap.js"></script>
  7. <script src="js/jquery.js"></script>
  8. </head>
  9. <body>
  10. <nav class="navbar navbar-default">
  11. <div class="container-fluid">
  12. <div class="navbar-header">
  13. <a class="navbar-brand" href="https://www.sourcecodester.com/user/224918/track">sourcecodester.com || nurhodelta_17</a>
  14. </div>
  15. </div>
  16. </nav>
  17. <div class="container">
  18. <div class="row">
  19. <div class="col-lg-2">
  20. </div>
  21. <div class="col-lg-8">
  22. <h2>Sample Table</h2>
  23. </div>
  24. <div class="col-lg-2">
  25. </div>
  26. </div>

  27. <div style="height:10px;"></div>
  28. <div class="row">
  29. <div class="col-lg-2">
  30. </div>
  31. <div class="col-lg-8">
  32. <select id="catList" class="btn btn-default">
  33. <option value="0">All Category</option>
  34. <?php
  35. include

    (

    'conn.php'

    )

    ;
  36. $c

    =

    mysqli_query

    (

    $conn

    ,

    "select * from category"

    )

    ;
  37. while

    (

    $row

    =

    mysqli_fetch_array

    (

    $c

    )

    )

    {
  38. $catid

    =

    isset

    (

    $_GET

    [

    'category'

    ]

    )

    ? $_GET

    [

    'category'

    ]

    :

    0

    ;
  39. $selected

    =

    (

    $catid

    ==

    $row

    [

    'categoryid'

    ]

    )

    ? " selected"

    :

    ""

    ;
  40. echo

    "<option$selected

    value="

    .

    $row

    [

    'categoryid'

    ]

    .

    ">"

    .

    $row

    [

    'cat_description'

    ]

    .

    "</option>"

    ;
  41. }
  42. ?>
  43. </select>
  44. </div>
  45. <div class="col-lg-2">
  46. </div>
  47. </div>
  48. <div style="height:10px;"></div>
  49. <div class="row">
  50. <div class="col-lg-2">
  51. </div>
  52. <div class="col-lg-8">
  53. <table class="table table-bordered table-striped">
  54. <thead class="alert alert-success">
  55. <th>Category Name</th>
  56. <th>Material Name</th>
  57. </thead>
  58. <tbody>
  59. <?php
  60. include

    (

    'conn.php'

    )

    ;
  61. $where

    =

    ""

    ;
  62. if

    (

    isset

    (

    $_GET

    [

    'category'

    ]

    )

    )
  63. {
  64. $catid

    =

    $_GET

    [

    'category'

    ]

    ;
  65. $where

    =

    " WHERE material.categoryid = $catid

    "

    ;
  66. }

  67. $result

    =

    mysqli_query

    (

    $conn

    ,

    "select * from material left join category on category.categoryid=material.categoryid $where

    "

    )

    ;
  68. while

    (

    $row1

    =

    mysqli_fetch_array

    (

    $result

    )

    )
  69. {
  70. ?>
  71. <tr>
  72. <td><?php

    echo

    $row1

    [

    'cat_description'

    ]

    ;

    ?>

    </td>
  73. <td><?php

    echo

    $row1

    [

    'material_name'

    ]

    ?>

    </td>
  74. </tr>
  75. <?php
  76. }
  77. ?>
  78. </tbody>
  79. </table>
  80. </div>
  81. <div class="col-lg-2">
  82. </div>
  83. </div>
  84. </div>
  85. <script type="text/javascript">
  86. $(document).ready(function(){
  87. $("#catList").on('change', function(){
  88. if($(this).val() == 0)
  89. {
  90. window.location = 'index.php';
  91. }
  92. else
  93. {
  94. window.location = 'index.php?category='+$(this).val();
  95. }
  96. });
  97. });
  98. </script>
  99. </body>
  100. </html>

And that ends our tutorial. If you have any question or suggestions regarding the tutorial, feel free to comment below or message me here at sourcecodester.com. Hope this tutorial helps.


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

449,708

322,558

322,567

Top