Simple Grade Calculator using PHP/MySQLi Tutorial

In this tutorial we will create a Simple Grade Calculator using PHP. This code will automatically calculate the student grade when the user inputs some value. The code itself uses a sum formula to calculate the grade of the individual student. This is a user-friendly program feel free to modify and use it in your system.

We will be using PHP as a scripting language that interprets in the web server such as XAMPP, WAMP, and etc. It is widely used by modern website application to handle and protect user confidential information.

Getting Started:

Creating Database

Open your database web server then create a database name in it db_grade. After that, click Import then locate the database file inside the "db" folder of the source code then click ok.

tut1

You may also create a table programmatically in SQL by using the script below.

  1.         CREATE TABLE `student` (
  2.           `stud_id` int(11) NOT NULL,
  3.           `name` varchar(100) NOT NULL,
  4.           `prelim` int(10) NOT NULL,
  5.           `midterm` int(10) NOT NULL,
  6.           `endterm` int(10) NOT NULL
  7.         ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  8.  
  9.         ALTER TABLE `student`
  10.         ADD PRIMARY KEY (`stud_id`);
  11.  
  12.         ALTER TABLE `student`
  13.         MODIFY `stud_id` int(11) NOT NULL AUTO_INCREMENT;

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_grade");
  3.        
  4.         if(!$conn){
  5.                 die("Error: Failed to connect to database!");
  6.         }
  7. ?>

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 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>
  8.         <nav class="navbar navbar-default">
  9.                 <div class="container-fluid">
  10.                         <a class="navbar-brand" href="https://sourcecodester.com">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 Grade Calculator</h3>
  16.                 <hr style="border-top:1px dotted #ccc;"/>
  17.                 <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add student</button>
  18.                 <br /><br />
  19.                 <table class="table table-bordered">
  20.                         <thead class="alert-info">
  21.                                 <tr>
  22.                                         <th>Name</th>
  23.                                         <th>Prelim</th>
  24.                                         <th>Midterm</th>
  25.                                         <th>Endterm</th>
  26.                                         <th>Final Grade</th>
  27.                                         <th>Status</th>
  28.                                 </tr>
  29.                         </thead>
  30.                         <tbody>
  31.                                 <?php
  32.                                         require 'conn.php';
  33.                                        
  34.                                         $query = mysqli_query($conn, "SELECT * FROM `student`") or die(mysqli_error());
  35.                                         while($fetch = mysqli_fetch_array($query)){
  36.                                        
  37.                                         $final = ($fetch['prelim'] + $fetch['midterm'] + $fetch['endterm']) / 3;
  38.                                 ?>
  39.                                 <tr>
  40.                                         <td><?php echo $fetch['name']?></td>
  41.                                         <td><?php echo $fetch['prelim']?></td>
  42.                                         <td><?php echo $fetch['midterm']?></td>
  43.                                         <td><?php echo $fetch['endterm']?></td>
  44.                                         <td><?php echo filter_var($final, FILTER_VALIDATE_INT) == false ? number_format($final,2) : number_format($final) ?></td>
  45.                                         <?php
  46.                                                 if($final >=75){
  47.                                                         echo "<td style='background-color:green; color:#fff;'>Pass</td>";
  48.                                                 }else if($final < 75){
  49.                                                         echo "<td style='background-color:red; color:#fff;'>Fail</td>";
  50.                                                 }
  51.                                         ?>
  52.                                 </tr>
  53.                                 <?php
  54.                                         }
  55.                                 ?>
  56.                         </tbody>
  57.                 </table>
  58.         </div>
  59. <div class="modal fade" id="form_modal" aria-hidden="true">
  60.         <div class="modal-dialog">
  61.                 <div class="modal-content">
  62.                         <form method="POST" action="save_student.php">
  63.                                 <div class="modal-header">
  64.                                         <h3 class="modal-title">Add Student</h3>
  65.                                 </div>
  66.                                 <div class="modal-body">
  67.                                         <div class="col-md-2"></div>
  68.                                         <div class="col-md-8">
  69.                                                 <div class="form-group">
  70.                                                         <label>Name</label>
  71.                                                         <input type="text" class="form-control" name="name" required="required"/>
  72.                                                 </div>
  73.                                                 <div class="form-group">
  74.                                                         <label>Prelim</label>
  75.                                                         <input type="number" min="0" max="100" class="form-control text-right" name="prelim" required="required"/>
  76.                                                 </div>
  77.                                                 <div class="form-group">
  78.                                                         <label>Midterm</label>
  79.                                                         <input type="number" min="0" max="100" class="form-control text-right" name="midterm" required="required"/>
  80.                                                 </div>
  81.                                                 <div class="form-group">
  82.                                                         <label>Endterm</label>
  83.                                                         <input type="number" min="0" max="100" class="form-control text-right" name="endterm" required="required"/>
  84.                                                 </div>
  85.                                         </div>
  86.                                 </div>
  87.                                 <br style="clear:both;"/>
  88.                                 <div class="modal-footer">
  89.                                         <button type="button" data-dismiss="modal" class="btn btn-danger"><span class="glyphicon glyphicon-remove"></span> Close</button>
  90.                                         <button class="btn btn-primary" name="save"><span class="glyphicon glyphicon-save"></span> Save</button>
  91.                                 </div>
  92.                         </form>
  93.                 </div>
  94.         </div>
  95. </div> 
  96. <script src="js/jquery-3.2.1.min.js"></script> 
  97. <script src="js/bootstrap.js"></script>
  98. </body>
  99. </html>

Creating PHP Query

This code contains the php query of the application. This code will store the student grade to the MySQLi database. To do that just copy and write this block of codes 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.                 $name = $_POST['name'];
  6.                 $prelim = $_POST['prelim'];
  7.                 $midterm = $_POST['midterm'];
  8.                 $endterm = $_POST['endterm'];
  9.                
  10.                 mysqli_query($conn, "INSERT INTO `student` VALUES('', '$name', '$prelim', '$midterm', '$endterm')") or die(mysqli_error());
  11.                 header("location: index.php");
  12.         }
  13. ?>

There you have it we successfully created Simple Grade Calculator using PHP. I hope that this simple tutorial helps you to what you are looking for. For more updates and tutorials just kindly visit and explore this site.

Enjoy Coding!

Add new comment