PHP - Simple Categorizing Result Using MySQLi

Submitted by: 
Language: 
Visitors have accessed this post 438 times.

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 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 jquery that i usedhttps://jquery.com/.

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

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


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.