Get Score Percentage In PHP | CampCodes

Get Score Percentage in PHP

June 4, 2020
Get Score Percentage in PHP

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

And this is the link for the jquery that I used in this tutorial

Lastly, this is the link for the bootstrap that I used for the layout design

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.

get score percentage in php

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.

    $conn = mysqli_connect("localhost", "root", "", "db_score");
        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">
        <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
        <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
    <nav class="navbar navbar-default">
        <div class="container-fluid">
            <a class="navbar-brand" href="">Sourcecodester</a>
    <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">
                    <input type="text" class="form-control" name="name" required="required"/>
                <div class="form-group">
                    <label>1st Test</label>
                    <input type="number" min="0" max="100" class="form-control" name="test1" required="required"/>
                <div class="form-group">
                    <label>2nd Test</label>
                    <input type="number" min="0" max="100" class="form-control" name="test2" required="required"/>
                <div class="form-group">
                    <label>3rd Test</label>
                    <input type="number" min="0" max="100" class="form-control" name="test3" required="required"/>
                <center><button class="btn btn-primary" name="add">Add</button></center>
        <div class="col-md-8">
            <h5>Note: All test are 100 items</h5>
            <table class="table table-bordered">
                <thead class="alert-info">
                        <th>1st Test</th>
                        <th>2nd Test</th>
                        <th>3rd Test</th>
                        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'];
                        <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>

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.

    require_once 'conn.php';
        $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");

This is a free education portal. You can use every source code in your project without asking permission to the author. Share our website to everyone to make our community of programmers grow more.

Leave a Reply

Your email address will not be published. Required fields are marked *