In this tutorial, we will create a Get Score Percentage using PHP. This code will automatically calculate the score when the user submits the inputted values. The system uses PHP POST method to call a function that will calculate the percentage of the given score using an algebraic formula to compute the numbers. 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 interprets in the webserver such as xamp, wamp, etc. It is widely used by modern website application to handle and protect user confidential information.
Table of Contents
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 jquery that I used in this tutorial https://jquery.com/.
Lastly, this is the link for the bootstrap that I used for the layout design https://getbootstrap.com/.
Creating Database
Open your database web server then create a database name in it db_score, after that click Import then locates 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.
<?php $conn = mysqli_connect("localhost", "root", "", "db_score"); if(!$conn){ die("Error: Failed to connect to database!"); } ?>
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.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/> <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/> </head> <body> <nav class="navbar navbar-default"> <div class="container-fluid"> <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a> </div> </nav> <div class="col-md-3"></div> <div class="col-md-6 well"> <h3 class="text-primary">PHP - Get Score Percentage</h3> <hr style="border-top:1px dotted #ccc;"/> <div class="col-md-4"> <form method="POST" action="add.php"> <div class="form-group"> <label>Name</label> <input type="text" class="form-control" name="name" required="required"/> </div> <div class="form-group"> <label>1st Test</label> <input type="number" min="0" max="100" class="form-control" name="test1" required="required"/> </div> <div class="form-group"> <label>2nd Test</label> <input type="number" min="0" max="100" class="form-control" name="test2" required="required"/> </div> <div class="form-group"> <label>3rd Test</label> <input type="number" min="0" max="100" class="form-control" name="test3" required="required"/> </div> <center><button class="btn btn-primary" name="add">Add</button></center> </form> </div> <div class="col-md-8"> <h5>Note: All test are 100 items</h5> <table class="table table-bordered"> <thead class="alert-info"> <tr> <th>Name</th> <th>1st Test</th> <th>2nd Test</th> <th>3rd Test</th> <th>Average</th> </tr> </thead> <tbody> <?php require 'conn.php'; $query = mysqli_query($conn, "SELECT * FROM `student`") or die(mysqli_error()); while($fetch = mysqli_fetch_array($query)){ $total_score = $fetch['test1'] + $fetch['test2'] + $fetch['test3']; $cal=$total_score/300; $ave=$cal*100; ?> <tr> <td><?php echo $fetch['name']?></td> <td><?php echo $fetch['test1']?></td> <td><?php echo $fetch['test2']?></td> <td><?php echo $fetch['test3']?></td> <td><?php echo number_format($ave, 2)."%"?></td> </tr> <?php } ?> </tbody> </table> </div> </div> </body> </html>
Creating PHP Query
This code contains the php query of the application. This code will store the score to the MySQLi database. To do that just copy and write this block of codes inside the text editor, then save it as add.php.
<?php require_once 'conn.php'; if(ISSET($_POST['add'])){ $name = $_POST['name']; $test1 = $_POST['test1']; $test2 = $_POST['test2']; $test3 = $_POST['test3']; mysqli_query($conn, "INSERT INTO `student` VALUES('', '$name', '$test1', '$test2', '$test3')") or die(mysqli_error()); header("location: index.php"); } ?>
There you have it we successfully created Get Score Percentage using PHP. 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!