Its part of a school management system built with php where the teacher selects a subject and a class. That generates a table where the teacher inserts the class score and exam score, the program then computes to get the total score, class average, grade, position and remarks for all students in the class. Part of the computing is handled by javascript and some by php. The results are sent to php to insert in the database.
I made different versions one with only php with countless errors after fixing them all no more errors but nothing dumps in the database.
With this version however some data gets dumped in the database like the class score and exam score but the total score, class average, position and grade have part of the script tag getting inserted.
I just need to get the computed data dumped in the database by any means necessary 🙂
“;
echo “”;
echo “Student IDStudent NameClass Score (30%)Exam Score (70%)Total Score (100%)Class AverageGradePositionRemarks”;
while ($row = $result->fetch_assoc()) {
$sid = $row[“sid”];
$fname = $row[“fname”];
$lname = $row[“lname”];
echo “$sid$fname $lname”;
echo “”;
echo “”;
echo “”;
echo “”;
echo “”;
echo “”;
echo “”;
echo “”;
}
/*Am thinking of adding a javascript id to get the values to be displayed in the table when a button is clicked*/
echo “”;
// Add a submit button to submit the form
echo “”;
echo “”;
echo “”;
echo “”;
echo “”;
echo “”;
?>
<?php
// If the form has been submitted, process the data
if (isset($_POST["classscore"]) && isset($_POST["examscore"])) {
$classscore = $_POST["classscore"];
$examscore = $_POST["examscore"];
$classroom = $_POST["classroom"];
$subject = $_POST["subject"];
$year = $_POST["year"];
$term = $_POST["term"];
/* this is where the computing goes down ;)*/
// Calculate the total score for each student
$totalscore = array();
foreach ($classscore as $sid => $score) {
// Add JavaScript code to calculate the total score
echo "<script>var totalscore_$sid = $score + $examscore[$sid] * 0.7;</script>";
$totalscore[$sid] = "<script>document.write(totalscore_$sid);</script>";
}
/*This outputs errors js code on the page and doesn't pass to the php*/
// Calculate the grades and remarks for each student based on their total score
$grade = array();
$remarks = array();
foreach ($totalscore as $sid => $score) {
// Add JavaScript code to calculate the grade and remarks
echo "<script>
var score_$sid = $score;
if (score_$sid >= 90 && score_$sid <= 100) {
var grade_$sid = 1;
var remarks_$sid = 'Distinction';
} else if (score_$sid >= 80 && score_$sid <= 89.99) {
var grade_$sid = 1;
var remarks_$sid = 'Excellent';
} else if (score_$sid >= 75 && score_$sid <= 79.99) {
var grade_$sid = 2;
var remarks_$sid = 'Very Good';
} else if (score_$sid >= 70 && score_$sid <= 74.99) {
var grade_$sid = 3;
var remarks_$sid = 'Good';
} else if (score_$sid >= 65 && score_$sid <= 69.99) {
var grade_$sid = 4;
var remarks_$sid = 'Credit';
} else if (score_$sid >= 60 && score_$sid <= 64.99) {
var grade_$sid = 5;
var remarks_$sid = 'Credit';
} else if (score_$sid >= 55 && score_$sid <= 59.99) {
var grade_$sid = 6;
var remarks_$sid = 'Credit';
} else if (score_$sid >= 50 && score_$sid <= 54.99) {
var grade_$sid = 7;
var remarks_$sid = 'Pass';
} else if (score_$sid >= 40 && score_$sid <= 49.99) {
var grade_$sid = 8;
var remarks_$sid = 'Weak Pass';
} else {`your text`
var grade_$sid = 9;
var remarks_$sid = 'Fail';
}
</script>";
$grade[$sid] = "<script>document.write(grade_$sid);</script>";
$remarks[$sid] = "<script>document.write(remarks_$sid);</script>";
}
echo "<script>
const average_$sid = totalscore.reduce((accumulator, currentValue) => accumulator + currentValue, 0) / totalscore.length;
</script>";
// Calculate the class average
$average = array_sum($totalscore) / count($totalscore);
// Output the class average
echo "Class Average: " . $average . "<br>";
/*In the database you see the positions as 1 throughout*/
// Rank the students by total score
arsort($totalscore);
$position = 1;
$prevscore = null;
foreach ($totalscore as $sid => $score) {
if ($score != $prevscore) {
$position = array_search($score, $totalscore) + 1;
}
/*had to comment this and use an unsecure version instead, couldn't bind etc. Will work on later*/
/*$stmt = $conn->prepare("INSERT INTO exam (sid, year, term, classroom, subject, classscore, examscore, totalscore, average, grade, position, remarks) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
$stmt->bind_param("sssssssssss", $sid, $year, $term, $classroom, $subject, $classscore[$sid], $examscore[$sid], $totalscore, $average, $grade[$sid], $position, $remarks[$sid]);
$stmt->execute();*/
// $conn->query($sql);
//$prevscore = $score;
$sql = "INSERT INTO exam (sid,year,term,classroom,subject,classscore,examscore,totalscore,average,grade,position,remarks) VALUES ('".$sid."', '".$year."', '".$term."','".$classroom."','".$subject."','".$classscore[$sid]."','".$examscore[$sid]."','".$totalscore[$sid]."','".$average."','".$grade[$sid]."','".$position."','".$remarks[$sid]."')";
$conn->query($sql);
$prevscore = $score;
}
}
?>
/*At a glance it seems to be outputting to the html instead of Javascript.
I used an unsecure query to see if the data gets dumped that had something in there alright just not the computed data at least the position seems to work.