PHP - Get Score Percentage

Submitted by: 
Visitors have accessed this post 334 times.

In this tutorial we will create a Get Score Percentage using PHP. This code will automatically calculate the score when user submit the inputted values. The code use 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 interpret 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 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 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_score");
  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="<a href="">Sourcecodester</a>
  11. " rel="nofollow">">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 - Get Score Percentage</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <div class="col-md-4">
  19. <form method="POST" action="add.php">
  20. <div class="form-group">
  21. <label>Name</label>
  22. <input type="text" class="form-control" name="name" required="required"/>
  23. </div>
  24. <div class="form-group">
  25. <label>1st Test</label>
  26. <input type="number" min="0" max="100" class="form-control" name="test1" required="required"/>
  27. </div>
  28. <div class="form-group">
  29. <label>2nd Test</label>
  30. <input type="number" min="0" max="100" class="form-control" name="test2" required="required"/>
  31. </div>
  32. <div class="form-group">
  33. <label>3rd Test</label>
  34. <input type="number" min="0" max="100" class="form-control" name="test3" required="required"/>
  35. </div>
  36. <center><button class="btn btn-primary" name="add">Add</button></center>
  37. </form>
  38. </div>
  39. <div class="col-md-8">
  40. <h5>Note: All test are 100 items</h5>
  41. <table class="table table-bordered">
  42. <thead class="alert-info">
  43. <tr>
  44. <th>Name</th>
  45. <th>1st Test</th>
  46. <th>2nd Test</th>
  47. <th>3rd Test</th>
  48. <th>Average</th>
  49. </tr>
  50. </thead>
  51. <tbody>
  52. <?php
  53. require 'conn.php';
  55. $query = mysqli_query($conn, "SELECT * FROM `student`") or die(mysqli_error());
  56. while($fetch = mysqli_fetch_array($query)){
  58. $total_score = $fetch['test1'] + $fetch['test2'] + $fetch['test3'];
  59. $cal=$total_score/300;
  60. $ave=$cal*100;
  61. ?>
  62. <tr>
  63. <td><?php echo $fetch['name']?></td>
  64. <td><?php echo $fetch['test1']?></td>
  65. <td><?php echo $fetch['test2']?></td>
  66. <td><?php echo $fetch['test3']?></td>
  67. <td><?php echo number_format($ave, 2)."%"?></td>
  68. </tr>
  69. <?php
  70. }
  71. ?>
  72. </tbody>
  73. </table>
  74. </div>
  75. </div>
  76. </body>
  77. </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.

  1. <?php
  2. require_once 'conn.php';
  4. if(ISSET($_POST['add'])){
  5. $name = $_POST['name'];
  6. $test1 = $_POST['test1'];
  7. $test2 = $_POST['test2'];
  8. $test3 = $_POST['test3'];
  10. mysqli_query($conn, "INSERT INTO `student` VALUES('', '$name', '$test1', '$test2', '$test3')") or die(mysqli_error());
  11. header("location: index.php");
  12. }
  13. ?>

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!

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.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.