So, I want to make a calculator that will calculate this formula:
Result = No. of Attendance / (No. of Students * No. of School Days)
but I want the No. of students to
be counted automatically by course and strand (different column) from the database.
This is the initial code:
<?php include 'includes/session.php'; ?>
<?php include 'includes/header.php'; ?>
<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">
<?php include 'includes/navbar.php'; ?>
<?php include 'includes/menubar.php'; ?>
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
<!-- Content Header (Page header) -->
<section class="content-header">
<h1>
Calculator
</h1>
</section>
<!-- Main content -->
<section class="content">
<div class="row">
<div class="col-md-12">
<div class="box box-info">
<div class="box-body">
<form method="post">
<label for="D">No. of Attendance:</label>
<input type="text" id="D" name="D" placeholder="No. of Attendance" value="<?php echo isset($_POST['D']) ? $_POST['D'] : '' ?>"><br>
<label for="B">No. of Students:</label>
<input type="text" id="B" name="B" placeholder="No. of Students" value="<?php echo isset($_POST['B']) ? $_POST['B'] : '' ?>"><br>
<label for="C">No. of School Days:</label>
<input type="text" id="C" name="C" placeholder="No. of School Days" value="<?php echo isset($_POST['C']) ? $_POST['C'] : '' ?>"><br>
<button type="submit" name="submit" value="submit">Calculate</button>
</form>
<h4>
<?php
if (isset($_POST['submit'])) {
// Retrieve user input values for D, B, and C
$D = isset($_POST['D']) ? $_POST['D'] : 0;
$B = isset($_POST['B']) ? $_POST['B'] : 0;
$C = isset($_POST['C']) ? $_POST['C'] : 0;
// Ensure the inputs are numeric
if (is_numeric($D) && is_numeric($B) && is_numeric($C)) {
// Check if B and C are not zero to avoid division by zero error
if ($B != 0 && $C != 0) {
// Calculate A = D / (B * C)
$A = $D / ($B * $C);
// Convert to percentage
$percentage = $A * 100;
echo "Result: A = " . $A . " or " . $percentage . "%";
} else {
echo "Error: Division by zero";
}
} else {
echo "Please enter numeric values for D, B, and C";
}
}
?>
</h4>
</div>
</div>
</div>
</div>
</section>
</div>
<!-- /.content-wrapper -->
</div>
<!-- ./wrapper -->
<?php include 'includes/scripts.php'; ?>
</body>
</html>
This is the code I edited:
<?php include 'includes/session.php'; ?>
<?php include 'includes/header.php'; ?>
<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">
<?php include 'includes/navbar.php'; ?>
<?php include 'includes/menubar.php'; ?>
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
<!-- Content Header (Page header) -->
<section class="content-header">
<h1>
Calculator
</h1>
</section>
<!-- Main content -->
<section class="content">
<div class="row">
<div class="col-md-12">
<div class="box box-info">
<div class="box-body">
<form method="post">
<label for="course">Select Course:</label>
<select id="course" name="course">
<option value="">Select Course</option>
<?php
// Fetch courses from your database and populate the dropdown menu
// Assuming you have a database connection already established
$query = "SELECT * FROM position_college"; // Modify this query according to your database schema
$result = mysqli_query($connection, $query);
if ($result) {
while ($row = mysqli_fetch_assoc($result)) {
echo "<option value='" . $row['description_college'] . "</option>";
}
}
?>
</select><br>
<label for="D">No. of Attendance:</label>
<input type="text" id="D" name="D" placeholder="No. of Attendance" value="<?php echo isset($_POST['D']) ? $_POST['D'] : '' ?>"><br>
<label for="B">No. of Students:</label>
<input type="text" id="B" name="B" placeholder="No. of Students" value="<?php echo isset($_POST['B']) ? $_POST['B'] : '' ?>"><br>
<label for="C">No. of School Days:</label>
<input type="text" id="C" name="C" placeholder="No. of School Days" value="<?php echo isset($_POST['C']) ? $_POST['C'] : '' ?>"><br>
<button type="submit" name="submit" value="submit">Calculate</button>
</form>
<h4>
<?php
if (isset($_POST['submit'])) {
// Retrieve user input values for D, B, C, and the selected course
$D = isset($_POST['D']) ? $_POST['D'] : 0;
$B = isset($_POST['B']) ? $_POST['B'] : 0;
$C = isset($_POST['C']) ? $_POST['C'] : 0;
$selected_course = isset($_POST['course']) ? $_POST['course'] : 0;
// Ensure the inputs are numeric
if (is_numeric($D) && is_numeric($B) && is_numeric($C) && $selected_course) {
// Check if B and C are not zero to avoid division by zero error
if ($B != 0 && $C != 0) {
// Calculate A = D / (B * C)
$A = $D / ($B * $C);
// Convert to percentage
$percentage = $A * 100;
echo "Result: A = " . $A . " or " . $percentage . "%";
} else {
echo "Error: Division by zero";
}
} else {
echo "Please enter numeric values for D, B, C, and select a course";
}
}
?>
</h4>
</div>
</div>
</div>
</div>
</section>
</div>
<!-- /.content-wrapper -->
</div>
<!-- ./wrapper -->
<?php include 'includes/scripts.php'; ?>
</body>
</html>
But this code doesn’t work and it doesn’t seem to provide result the I wanted it to give. These are the tables in the database