Skip to main content

PHP - Simple Categorizing Result Using MySQLi

In this tutorial we will create a Simple Categorizing Result using MySQLi. This code will categorize the data that display in the table base on the user selected category. The code use MySQLi SELECT query to categorize the data in the table by providing a POST value in the WHERE clause. This a user-friendly program feel free to modify and use it to your system.

We will be using PHP as a scripting language that interepret in the webserver such as xamp, wamp, etc. It is widely use by modern website application to handle and protect user confidential information.

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server

And, this is the link for the bootstrap that i used for the layout design

Lastly, this is the link for the jquery that i used

Creating Database

Open your database web server then create a database name in it db_result, after that click Import then locate the database file inside the folder of the application then click ok.

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_result");
  3. if(!$conn){
  4. die("Error: Failed to connecto 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 your text editor, then save it as index.php.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charse="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>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="">Sourcecodester</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 Categoring Result Using MySQLi</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <button class="btn btn-success" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add student</button>
  18. <br /><br />
  19. <form class="form-inline" method="POST" action="">
  20. <label>Select Category:</label>
  21. <select class="form-control" name="category">
  22. <option value="All">All</option>
  23. <option value="Math">Math</option>
  24. <option value="Science">Science</option>
  25. <option value="English">English</option>
  26. <option value="MAPEH">MAPEH</option>
  27. </select>
  28. <button class="btn btn-warning" name="change">Change</button>
  29. </form>
  30. <br />
  31. <h4>Display by: <span style="font-weight:bold;"><?php if(ISSET($_POST['change'])){echo $_POST['category'];}else{echo "All";}?></span></h4>
  32. <table class="table table-bordered">
  33. <thead class="alert-info">
  34. <tr>
  35. <th>Category</th>
  36. <th>Student Name</th>
  37. </tr>
  38. </thead>
  39. <tbody>
  40. <?php include'display.php'?>
  41. </tbody>
  42. </table>
  43. </div>
  44. <div class="modal fade" id="form_modal" aria-hidden="true">
  45. <div class="modal-dialog">
  46. <div class="modal-content">
  47. <form method="POST" action="save_student.php">
  48. <div class="modal-header">
  49. <h3 class="modal-title">Add Student</h3>
  50. </div>
  51. <div class="modal-body">
  52. <div class="col-md-2"></div>
  53. <div class="col-md-8">
  54. <div class="form-group">
  55. <label>Subject</label>
  56. <select name="subject" class="form-control" required="required">
  57. <option value="">Select an option</option>
  58. <option value="Math">Math</option>
  59. <option value="Science">Science</option>
  60. <option value="English">English</option>
  61. <option value="MAPEH">MAPEH</option>
  62. </select>
  63. </div>
  64. <div class="form-group">
  65. <label>Student Name</label>
  66. <input type="text" class="form-control" name="stud_name" required="required"/>
  67. </div>
  68. </div>
  69. </div>
  70. <br style="clear:both;"/>
  71. <div class="modal-footer">
  72. <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  73. <button class="btn btn-primary" name="save"><span class="glyphicon glyphicon-save"></span> Save</button>
  74. </div>
  75. </form>
  76. </div>
  77. </div>
  78. </div>
  79. <script src="js/jquery-3.2.1.min.js"></script>
  80. <script src="js/bootstrap.js"></script>
  81. </body>
  82. </html>

Creating the PHP Query

This code contains the php query of the application. This code will store the user data inputs to the database server. To make this just copy and write these block of codes below inside the text editor, then save it as save_student.php.

  1. <?php
  2. require_once'conn.php';
  4. if(ISSET($_POST['save'])){
  5. $subject=$_POST['subject'];
  6. $stud_name=$_POST['stud_name'];
  7. }
  8. mysqli_query($conn, "INSERT INTO `student` VALUES('', '$subject', '$stud_name')") or die(mysqli_error());
  9. header("location: index.php");
  10. ?>

Creating the Main Function

This code contains the main function of the application. This code will categorize the data in the table when the button is clicked. To make this just copy and write these block of codes below inside the text editor, then save it as display.php

  1. <?php
  2. require'conn.php';
  4. if(ISSET($_POST['change'])){
  5. $category=$_POST['category'];
  7. switch($category){
  8. case"Math":
  9. $query=mysqli_query($conn, "SELECT * FROM `student` WHERE `subject`='$category'") or die(mysqli_error());
  10. while($fetch=mysqli_fetch_array($query)){
  11. echo"<tr><td>".$fetch['subject']."</td><td>".$fetch['stud_name']."</td></tr>";
  12. }
  13. break;
  14. case"Science":
  15. $query=mysqli_query($conn, "SELECT * FROM `student` WHERE `subject`='$category'") or die(mysqli_error());
  16. while($fetch=mysqli_fetch_array($query)){
  17. echo"<tr><td>".$fetch['subject']."</td><td>".$fetch['stud_name']."</td></tr>";
  18. }
  19. break;
  20. case"English":
  21. $query=mysqli_query($conn, "SELECT * FROM `student` WHERE `subject`='$category'") or die(mysqli_error());
  22. while($fetch=mysqli_fetch_array($query)){
  23. echo"<tr><td>".$fetch['subject']."</td><td>".$fetch['stud_name']."</td></tr>";
  24. }
  25. break;
  26. case"MAPEH":
  27. $query=mysqli_query($conn, "SELECT * FROM `student` WHERE `subject`='$category'") or die(mysqli_error());
  28. while($fetch=mysqli_fetch_array($query)){
  29. echo"<tr><td>".$fetch['subject']."</td><td>".$fetch['stud_name']."</td></tr>";
  30. }
  31. break;
  32. case"All":
  33. $query=mysqli_query($conn, "SELECT * FROM `student`") or die(mysqli_error());
  34. while($fetch=mysqli_fetch_array($query)){
  35. echo"<tr><td>".$fetch['subject']."</td><td>".$fetch['stud_name']."</td></tr>";
  36. }
  37. break;
  38. }
  39. }else{
  40. $query=mysqli_query($conn, "SELECT * FROM `student`") or die(mysqli_error());
  41. while($fetch=mysqli_fetch_array($query)){
  42. echo"<tr><td>".$fetch['subject']."</td><td>".$fetch['stud_name']."</td></tr>";
  43. }
  44. }
  45. ?>

There you have it we successfully created Simple Categorizing Result using MySQLi. 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!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.


1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.