• We just launched and are currently in beta. Join us as we build and grow the community.

Simple Grade Calculator using PHP/MySQLi Tutorial

AlvoErrado2

Tech Trends Analyst
A Rep
0
0
0
Rep
0
A Vouches
0
0
0
Vouches
0
Posts
134
Likes
34
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
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.

2019-03-22_22_42_40-localhost_127.0.0.1_db_grade_phpmyadmin_4.8.3.png


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

    TABLE

    `student`
  9. ADD

    PRIMARY KEY

    (

    `stud_

    id`

    )

    ;

  10. ALTER

    TABLE

    `student`
  11. 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. if

    (

    !

    $conn

    )

    {
  4. die

    (

    "Error: Failed to connect 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 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. $query

    =

    mysqli_query

    (

    $conn

    ,

    "SELECT * FROM `student`"

    )

    or die

    (

    mysqli_error

    (

    )

    )

    ;
  34. while

    (

    $fetch

    =

    mysqli_fetch_array

    (

    $query

    )

    )

    {

  35. $final

    =

    (

    $fetch

    [

    'prelim'

    ]

    +

    $fetch

    [

    'midterm'

    ]

    +

    $fetch

    [

    'endterm'

    ]

    )

    /

    3

    ;
  36. ?>
  37. <tr>
  38. <td><?php

    echo

    $fetch

    [

    'name'

    ]

    ?>

    </td>
  39. <td><?php

    echo

    $fetch

    [

    'prelim'

    ]

    ?>

    </td>
  40. <td><?php

    echo

    $fetch

    [

    'midterm'

    ]

    ?>

    </td>
  41. <td><?php

    echo

    $fetch

    [

    'endterm'

    ]

    ?>

    </td>
  42. <td><?php

    echo

    filter_var

    (

    $final

    ,

    FILTER_VALIDATE_INT)

    ==

    false

    ? number_format

    (

    $final

    ,

    2

    )

    :

    number_format

    (

    $final

    )

    ?>

    </td>
  43. <?php
  44. if

    (

    $final

    >=

    75

    )

    {
  45. echo

    "<td style='background-color:green; color:#fff;'>Pass</td>"

    ;
  46. }

    else

    if

    (

    $final

    <

    75

    )

    {
  47. echo

    "<td style='background-color:red; color:#fff;'>Fail</td>"

    ;
  48. }
  49. ?>
  50. </tr>
  51. <?php
  52. }
  53. ?>
  54. </tbody>
  55. </table>
  56. </div>
  57. <div class="modal fade" id="form_modal" aria-hidden="true">
  58. <div class="modal-dialog">
  59. <div class="modal-content">
  60. <form method="POST" action="save_student.php">
  61. <div class="modal-header">
  62. <h3 class="modal-title">Add Student</h3>
  63. </div>
  64. <div class="modal-body">
  65. <div class="col-md-2"></div>
  66. <div class="col-md-8">
  67. <div class="form-group">
  68. <label>Name</label>
  69. <input type="text" class="form-control" name="name" required="required"/>
  70. </div>
  71. <div class="form-group">
  72. <label>Prelim</label>
  73. <input type="number" min="0" max="100" class="form-control text-right" name="prelim" required="required"/>
  74. </div>
  75. <div class="form-group">
  76. <label>Midterm</label>
  77. <input type="number" min="0" max="100" class="form-control text-right" name="midterm" required="required"/>
  78. </div>
  79. <div class="form-group">
  80. <label>Endterm</label>
  81. <input type="number" min="0" max="100" class="form-control text-right" name="endterm" required="required"/>
  82. </div>
  83. </div>
  84. </div>
  85. <br style="clear:both;"/>
  86. <div class="modal-footer">
  87. <button type="button" data-dismiss="modal" class="btn btn-danger"><span class="glyphicon glyphicon-remove"></span> Close</button>
  88. <button class="btn btn-primary" name="save"><span class="glyphicon glyphicon-save"></span> Save</button>
  89. </div>
  90. </form>
  91. </div>
  92. </div>
  93. </div>
  94. <script src="js/jquery-3.2.1.min.js"></script>
  95. <script src="js/bootstrap.js"></script>
  96. </body>
  97. </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. if

    (

    ISSET

    (

    $_POST

    [

    'save'

    ]

    )

    )

    {
  4. $name

    =

    $_POST

    [

    'name'

    ]

    ;
  5. $prelim

    =

    $_POST

    [

    'prelim'

    ]

    ;
  6. $midterm

    =

    $_POST

    [

    'midterm'

    ]

    ;
  7. $endterm

    =

    $_POST

    [

    'endterm'

    ]

    ;

  8. mysqli_query

    (

    $conn

    ,

    "INSERT INTO `student` VALUES('', '$name

    ', '$prelim

    ', '$midterm

    ', '$endterm

    ')"

    )

    or die

    (

    mysqli_error

    (

    )

    )

    ;
  9. header

    (

    "location: index.php"

    )

    ;
  10. }
  11. ?>

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!


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,340

323,349

Top