<!-- Second form --> <?php if (isset($_GET['numassignments']) && $_GET['numassignments'] > 0) : ?> <div class="alert alert-info mb-5"> <h2 class="form-text fs-4 mb-4">Next, for each assignment, add the grade that you received and the assignment's overall weight.</h2> <!-- PHP_SELF is the name of the current file WITHOUT any query string info. To retain the query string info, we can use REQUEST_URI --> <form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="post"> <fieldset> <?php
$desired = isset($_POST["desired"]) ? $_POST["desired"] : 0;
for ($i = 1; $i <= $numassignments; $i++) { echo "<div class=\"row\">"; echo "\n<div class=\"col-md-6\">"; //grade input echo "\n<div class=\"mb-3\">"; echo "\n<label for=\"keep-numassignments{$i}\" class=\"form-label\">Assignment " . $i . " Grade:</label>"; echo "\n<input type=\"number\" class=\"form-control\" name=\"keep-numassignments[$i]\" id=\"keep-numassignments{$i}\" min=\"0\" max=\"100\" value=\"$keep_numassignments\" required>"; echo "\n</div>"; echo "\n</div>"; echo "\n<div class=\"col-md-6\">"; //weight input echo "\n<div class=\"mb-3\">"; echo "\n<label for=\"weightnum{$i}\" class=\"form-label\">Assignment " . $i . " Weight:</label>"; echo "\n<input type=\"number\" class=\"form-control\" name=\"weightnum[$i]\" id=\"weightnum{$i}\" min=\"1\" max=\"100\" value=\"$weightnum\" required>"; echo "\n</div>"; echo "\n</div>"; echo "\n</div>"; } ?> <p class="form-text">Finally, if you would like to calculate what you will need to get on the remainder of your coursework in order to receive a certain grade, enter your desired grade below.</p> <div class="mb-3"> <label for="desired" class="form-label">Desired Final Grade (Optional):</label> <input type="number" class="form-control" name="desired" id="desired" min="0" max="100" value="<?php echo $desired; ?>"> </div> </fieldset> <div class="mb-3"> <input type="submit" name="gradesubmit" value="Calculate Grade" class="btn btn-primary"> </div> </form> </div> <?php endif; ?>
<?php
if (isset($_POST['gradesubmit'])) { // Retrieve the values from the form $grades = isset($_POST["keep-numassignments"]) ? $_POST["keep-numassignments"] : []; $weights = isset($_POST["weightnum"]) ? $_POST["weightnum"] : [];
$desired = isset($_POST["desired"]) ? $_POST["desired"] : 0;
// Calculate the weighted average of completed assignments $weightedSum = 0; $totalWeight = 0;
for ($i = 0; $i < count($grades); $i++) { $weightedSum += ($weights[$i] * $grades[$i]); $totalWeight += $weights[$i]; }
$currentAverage = ($totalWeight > 0) ? ($weightedSum / $totalWeight) : 0;
// Calculate the required grade on remaining assessments if ($totalWeight < 100 && $desired > 0) { $remainingWeight = 100 - $totalWeight; $requiredGrade = ($desired - (1 - ($remainingWeight / 100)) * $currentAverage) / $remainingWeight; } else { $requiredGrade = 0; }
// Display the results echo "<div class=\"alert alert-success mb-5\">"; echo "\n<p>Current Class Average: " . number_format($currentAverage, 2) . "%</p>"; if ($desired > 0) { echo "\n<p>Required Grade on Remaining Assessments: " . number_format($requiredGrade, 2) . "%</p>"; } echo "\n</div>"; }
?> </div>
</body> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script>
</html> enter image description here |