I need a little helping hand in my script. As the title suggests it's about parameters. I have this code:
if (isset($_GET['sendit'])) {
$theusersname = $_GET['theusersname'];
$totalmarks = $_GET['totalmarks'];
$mathsobt = $_GET['mathsobt'];
$physicsobt = $_GET['physicsobt'];
$chemistryobt = $_GET['chemistryobt'];
$computerobt = $_GET['computerobt'];
$englishobt = $_GET['englishobt'];
$totalobt = $mathsobt + $physicsobt + $chemistryobt + $computerobt + $englishobt;
$modulo = ($totalobt / $totalmarks) * 100;
$grade = "";
//for grades
switch ($modulo, $grade) {
case 'A-1'://100% ro 80%
if ($modulo >= 79.5 && $modulo <= 100){
$grade = "A-1";
}
break;
case 'A'://79% to 70%
if ($modulo >= 69.5 && $modulo <= 79.4) {
$grade = "A";
}
break;
case 'B'://69 to 60
if ($modulo >= 59.5 && $modulo <= 69.4){
$grade = "B";
}
break;
case 'C'://59 to 50
if ($modulo >= 49.5 && $modulo <= 59.4){
$grade = "C";
}
break;
case 'D'://49 to 40
if ($modulo >= 39.5 && $modulo <= 49.4){
$grade = "D";
}
break;
case 'F'://32 to rest
if ($modulo >= 33 && $modulo <= 39.4){
$grade = "F";
}
default:
if ($modulo >= 0 && $modulo <= 32.9){
$grade = "N/A";
}
break;
}
//for remarks
switch ($grade) {
case '':
break;
default:
break;
}
$query = "INSERT INTO `report`(`name`, `grade`, `total`, `mathsobt`, `physicsobt`, `chemistryobt`, `computerobt`, `englishobt`, `totalobt`, `modulo`, `remarks`) VALUES ('$theusersname', '$grade', '$totalmarks', '$mathsobt', '$physicsobt', '$chemistryobt', '$computerobt', '$englishobt', '$totalobt', '$modulo', '$remarks')";
$result = mysqli_query($mysqliConn, $query);
// if (mysqli_query($mysqliConn, $query)) {
// echo "New record created successfully";
// } else {
// echo "Error: " . $query . "<br>" . mysqli_error($mysqliConn);
$mysqliConn->close();
// }
The grading switch statement doesn't work. Because I just can't get what parameter I need to give. Same goes for the remarks too. The code may be terrible and don't have sanitization (which in this case I don't need). I'm also a little bit confuse with the strings I passed for cases. I mean does it have an effect on overall?
Thank you.
PS: If there is already a similar answer out there, which i unfortunately i didn't find, i'm ready to view the link. Please post a little explanatory comment.
$modulo && $grade is a boolean expression. Its value is either TRUE or FALSE (and not 'A', 'B' or 'C').
But PHP is a loose-type language and it changes the types of its values as needed before using them. Because of this, 'A' == TRUE and '' == FALSE and your code produces only 'A-1' and 'N/A' grades.
A switch doesn't help here. You should use a sequence of cascaded ifs:
if ($modulo >= 79.5) {
$grade = "A-1";
} elseif ($modulo >= 69.5) {
$grade = "A";
} elseif ($modulo >= 59.5) {
$grade = "B";
} elseif ($modulo >= 49.5) {
$grade = "C";
} elseif ($modulo >= 39.5) {
$grade = "D";
} elseif ($modulo >= 33) {
$grade = "F";
} else {
$grade = "N/A";
}
Read how PHP compare values of different types and read how the switch control structure works.
Here you go
$theusersname = 'theusersname';
$totalmarks = 500;
$mathsobt = 87;
$physicsobt = 82;
$chemistryobt = 75;
$computerobt = 79;
$englishobt = 91;
$totalobt = $mathsobt + $physicsobt + $chemistryobt + $computerobt + $englishobt;
$modulo = ($totalobt / $totalmarks) * 100;
switch ($modulo) {
case $modulo >= 33 && $modulo < 39.5:
$grade = "F";
break;
case $modulo >= 39.5 && $modulo < 49.5:
$grade = "D";
break;
case $modulo >= 49.5 && $modulo < 59.5:
$grade = "C";
break;
case $modulo >= 59.5 && $modulo < 69.5:
$grade = "B";
break;
case $modulo >= 69.5 && $modulo < 79.5:
$grade = "A";
break;
case ($modulo >= 79.5 && $modulo <= 100 ):
$grade = "A-1";
break;
default:
$grade = "N/A";
break;
}
switch ($grade) {
case 'A-1':
$remarks = "You have got A-1 ";
break;
case 'A':
$remarks = "You have got A ";
break;
case 'B':
$remarks = "You have got B ";
break;
case 'C':
$remarks = "You have got C ";
break;
case 'D':
$remarks = "You have got D ";
break;
case 'F':
$remarks = "You have got F ";
break;
default:
$remarks = "You have got nothing";
break;
}
echo $query = "INSERT INTO `report`
(`name`, `grade`, `total`, `mathsobt`, `physicsobt`,
`chemistryobt`, `computerobt`, `englishobt`, `totalobt`,
`modulo`, `remarks`)
VALUES ('$theusersname', '$grade', '$totalmarks', '$mathsobt',
'$physicsobt', '$chemistryobt', '$computerobt', '$englishobt',
'$totalobt', '$modulo', '$remarks')";
OUTPUT
INSERT INTO `report`
(`name`, `grade`, `total`, `mathsobt`, `physicsobt`,
`chemistryobt`, `computerobt`, `englishobt`, `totalobt`,
`modulo`, `remarks`)
VALUES ('theusersname', 'A-1', '500', '87', '82', '75', '79',
'91', '414', '82.8', 'You have got A-1 ')
Related
I am trying to generate a report book based on marks scored by a student. In the form I have a checkbox from which a teacher can tick if a student was absent and if that is the case then the value for the MARK must change to AB and COMMENT as ABSENT, while it is able to change the value to "AB", this code is unable to provide a comment that the student was "ABSENT" instead it puts "FAIL" as a comment. What should I change in the code.
if (isset($_POST['absent']) && (empty($mark)))
{
$mark="AB";
$comment="Absent";
}
if($mark < 101 && $mark >=0)
{ $comment="";
if($grade > 9){
$score="";
if($mark >=75 and $mark <= 100 ){
$score=1;
$comment="Distinction";
}else if($mark >= 70 and $mark <=74){
$score=2;
$comment="Distinction";
}else if($mark >= 65 and $mark <= 69 ){
$score=3;
$comment="Merit";
}else if($mark >= 60 and $mark <=64){
$score=4;
$comment="Merit";
}else if($mark >= 55 and $mark <= 59){
$score=5;
$comment="Credit";
}else if($mark >= 50 and $mark <= 54){
$score=6;
$comment="Credit";
}else if($mark >= 45 and $mark <= 49){
$score=7;
$comment="Pass";
}else if($mark >= 40 and $mark <= 44){
$score=8;
$comment="Pass";
}else if($mark >= 0 and $mark <=39){
$score=9;
$comment="Fail";
}else if($mark=="AB"){
$comment="Absent";
}
Else and If used in this repetative way is pretty inefficient. Here is a suggestion to use switch. In PHP 8+ you can possibly also use match. Switch is more efficient because it only checks the value of the testing variable ($mark) once, rather than repeatedly .
$comment=""; //default.
if (isset($_POST['absent']) && (empty($mark)))
{
$mark="AB";
$comment="Absent"; //default.
}
switch((int)$mark < 101 && $grade > 9) {
case in_array($mark, range(75,100)):
$score=1;
$comment="Distinction";
break;
case in_array($mark, range(70,74)):
$score=2;
$comment="Distinction";
break;
case in_array($mark, range(65,69)):
$score=3;
$comment="Merit";
break;
case in_array($mark, range(60,64)):
$score=4;
$comment="Merit";
break;
case in_array($mark, range(55,59)):
$score=5;
$comment="Credit";
break;
case in_array($mark, range(50,54)):
$score=6;
$comment="Credit";
break;
case in_array($mark, range(45,49)):
$score=7;
$comment="Pass";
break;
case in_array($mark, range(40,44)):
$score=8;
$comment="Pass";
break;
case in_array($mark, range(0,39)):
$score=9;
$comment="Fail";
break;
}
I need to change the value of a text field (Agerisk2) to a number based on the selection of a radio field (Age)
In the text field additional attributes I have
onChange="$Agerisk2"
In the text field default value I have:
//<code>
$Age = [];
$Agerisk2 = null;
switch ($Age) {
case $Age "Under 45 years":
$Agerisk2 = "0";
break;
case $Age "45 – 54 years":
$Agerisk2 = "2";
break;
case $Age "55 – 64 years":
$Agerisk2 = "3";
break;
case $Age "64 years or over":
$Agerisk2 = "4";
break;
default:
$Agerisk2 = null;
break;
}
//</code>
Something isn't quite right and I'd really appreciate some help. Thank you
Your switch statement is invalid. Better use match for it.
Also note your variable naming should match the PSR
$age = 47;
$ageRisk = match (true) {
$age < 45 => 0,
$age >= 45 && $age <= 54 => 2,
$age >= 55 && $age <= 64 => 3,
$age >= 64 => 4,
default => null
};
echo $ageRisk;
prints
2
If you are below PHP 8 you can fallback to switch, which does not look so nice.
switch (true) {
case $age < 45:
$ageRisk = 0;
break;
case $age >= 45 && $age <= 54:
$ageRisk = 2;
break;
case $age >= 55 && $age <= 64:
$ageRisk = 3;
break;
case $age >= 64:
$ageRisk = 4;
break;
default:
$ageRisk = null;
break;
}
I've been able to display the number of times each grade appears on a student's report sheet. However, they are not sorted alphabetically.
grade image
The image above shows that the student got 2Bs, 4B+, 3As, and 1E. My code displays it like this.
But I want the grades to be displayed in alphabetical order like this 3As, 2Bs, 4B+, and 1E.
How do I do that?
Here is my code
<?php $i = 1;
$total = 0;
$count = count($subjectScores);
$grades_count = [];
foreach ($subjectScores as $value) { ?>
<?php
if ($value->tot_score >= 90 && $value->tot_score <= 100) {
$grade = 'A+';
$remark = 'DISTINCTION';
} elseif ($value->tot_score >= 80 && $value->tot_score <= 89.99) {
$grade = 'A';
$remark = 'EXCELLENT';
} elseif ($value->tot_score >= 70 && $value->tot_score <= 79.99) {
$grade = 'B+';
$remark = 'VERY GOOD';
} elseif ($value->tot_score >= 60 && $value->tot_score <= 69.99) {
$grade = 'B';
$remark = 'GOOD';
} elseif ($value->tot_score >= 50 && $value->tot_score <= 59.99) {
$grade = 'C';
$remark = 'ABOVE AVERAGE';
} elseif ($value->tot_score >= 45 && $value->tot_score <= 49.99) {
$grade = 'D';
$remark = 'AVERAGE';
} elseif ($value->tot_score >= 40 && $value->tot_score <= 44.99) {
$grade = 'E';
$remark = 'FAIR';
} elseif ($value->tot_score >= 0 && $value->tot_score <= 39.99) {
$grade = 'F';
$remark = 'NEEDS SERIOUS IMPROVEMENT';
}
// declare count for grade initially with 0
if(isset($grades_count[$grade]) === false) {
$grades_count[$grade] = 0;
}
{
// increment count for given grade
$grades_count[$grade]++;
}
?>
<?php foreach ($grades_count as $grade=>$count) {
echo "$count$grade ";
} ?>
$Grade and $Remark is always evaluated at the last value of $total (i.e where ($check_ss=="Ss")) but I want it to get value at every instance of the if conditions. How do I do
Eg at first condition it gets value of grade and remark same for subsequent conditions
or do i haveto include the switch statement inside each if statement
//Logic and Calc
if ($check_en=="En"){
$ot_en = $ent1 + $ent2 + $ent3 + $ent4 + $enexm;
$total = $ot_en;
}
if ($check_ms=="Ms"){
$ot_ms = $mst1 + $mst2 + $mst3 + $mst4 + $msexm;
$total = $ot_ms;
}
if ($check_ss=="Ss"){
$ot_ss = $sst1 + $sst2 + $sst3 + $sst4 + $ssexm;
$total = $ot_ss;
}
$ot= $ot_ms + $ot_ss + $ot_en;
switch ($total) {
case $total > 70:
$grade = "A";
$remark = "Excellent";
break;
case $total >= 60 && $total <= 69:
$grade = "B";
$remark = "Very Good";
break;
case $total >= 50 && $total <= 59:
$grade = "C";
$remark = "Good";
break;
case $total >= 45 && $total <= 49:
$grade = "D";
$remark = "Pass";
break;
case $total >= 40 && $total <= 44:
$grade = "E";
$remark = "Poor";
break;
case $total <= 39:
$grade = "F";
$remark = "Fail";
break;
}
if ($total == 0) {
$grade = "F";
$remark = "Fail";
Make your switch statement into a function and also use an array to hold the grade and remark data. Something like this:
function checkGrade($total)
{
$ret = array();
switch ($total) {
case $total > 70:
$ret['grade'] = "A";
$ret['remark'] = "Excellent";
break;
case $total >= 60 && $total <= 69:
$ret['grade'] = "B";
$ret['remark'] = "Very Good";
break;
case $total >= 50 && $total <= 59:
$ret['grade'] = "C";
$ret['remark'] = "Good";
break;
case $total >= 45 && $total <= 49:
$ret['grade'] = "D";
$ret['remark'] = "Pass";
break;
case $total >= 40 && $total <= 44:
$ret['grade'] = "E";
$ret['remark'] = "Poor";
break;
case $total <= 39:
$ret['grade'] = "F";
$ret['remark'] = "Fail";
break;
}
return $ret;
}
Now you can easily use this within your if() conditions, and get back the grade at each point.
$gradesAlongTheWay = array();
if ($check_en=="En"){
$ot_en = $ent1 + $ent2 + $ent3 + $ent4 + $enexm;
$total = $ot_en;
$gradesAlongTheWay['En'] = checkGrade($total);
}
if ($check_ms=="Ms"){
$ot_ms = $mst1 + $mst2 + $mst3 + $mst4 + $msexm;
$total = $ot_ms;
$gradesAlongTheWay['Ms'] = checkGrade($total);
}
if ($check_ss=="Ss"){
$ot_ss = $sst1 + $sst2 + $sst3 + $sst4 + $ssexm;
$total = $ot_ss;
$gradesAlongTheWay['Ss'] = checkGrade($total);
}
Then you can dump out the gradesAlongTheWay() to find out what the grade was at each specific point. This will be a multi-dimensional array with 3 primary indices:
Array(
'En' => array(
'grade' => 'Some Letter',
'remark' => 'Some notation'
),
'Ms' => array(
'grade' => 'Some Letter',
'remark' => 'Some notation'
),
'Ss' => array(
'grade' => 'Some Letter',
'remark' => 'Some notation'
)
)
Now we can easily access the grades at each point by index and their grade, thussly:
echo $gradesAlongTheWay['En']['grade']; // produces the letter from this check
echo $gradesAlongTheWay['Ss']['remark']; //produces the notation/message from this check
You can also just loop over them.
foreach($gradesAlongTheWay as $type => $gradeArray)
{
echo 'For Check '. $type .' you received an: '. $gradeArray['grade'] .'. '. $gradeArray['remark'];
}
Edit
You're getting SQL errors because of the ' apostrophe in your PHP code being injected into your MySQL code. Instead, you need to use mysqli prepared statements.
$stmt = mysqli_prepare($conn, "INSERT INTO records VALUES (NULL, '?', 'English Language', '?', '?', '?', '?', '?', '?', ?, 'Poor'");
mysqli_stmt_bind_param($stmt, "ssssssss", $sid, $ent1, $ent2, $ent3, $ent4, $enexm, $ot_en, $gradesAlongTheWay['En']['grade']);
mysqli_stmt_execute($stmt);
This will make sure your data is correctly sanitized.
Please read more into mysqli prepared statements here
(sorry for my bad English writing). I've got a problem where I'm stuck with an if and else system that returns the level that you're.
Everything is good, if you have 20 clicks it will tell you that you're level 2, if you have 550 clicks it will tell you're level 6. But when you go up to 1500 clicks it needs to say level 9, but it will still say level 8.
My code:
$levelown = 'Level 1';
function ifElse() {
global $levelown;
global $arrayIP;
if($arrayIP['clicks'] >= 0 && $arrayIP['clicks'] <= 49)
{
$levelown = 'Level 2';
}
/* .... More if and elses with levels */
// This is the problem, this will keep telling me that I'm level 8.
elseif($arrayIP['clicks']>=3000)
{
$levelown = 'Level ' . floor(($score['clicks']/1000)+8);
}
and you are <strong><?php echo $levelown; ?></strong>
Thank you for helping!
The problem that i saw from your current code is that you have
elseif($arrayIP['clicks']>=3000)
{
$levelown = 'Level ' . floor(($score['clicks']/1000)+8);
}
which i think should be
elseif($arrayIP['clicks']>=3000)
{
$levelown = 'Level ' . floor(($arrayIP['clicks']/1000)+8);
}
since $score['clicks'] isn't present you will always end up with level 8
It's probably easier and more readable to make it a switch/case statement like so:
$highscore = $mysqli->query("SELECT id,name,clicks,ip,factory FROM highscore ORDER BY clicks DESC LIMIT 0,50 ");
$ipquery = $mysqli->query("SELECT id,name,clicks,ip,factory FROM highscore WHERE ip = '".$_SERVER['REMOTE_ADDR']."'");
$arrayIP = $ipquery->fetch_array();
$levelown = 1;
function ifElse() {
global $levelown;
global $arrayIP;
switch (true) {
case $arrayIP['click'] > 3000:
$levelown = floor(($arrayIP['click']/1000)+8);
break;
case $arrayIP['click']== 3000:
$levelown = 11;
break;
case $arrayIP['click'] >= 2000:
$levelown = 10;
break;
case $arrayIP['click'] >= 1500:
$levelown = 9;
break;
case $arrayIP['click'] >= 1000:
$levelown = 8;
break;
case $arrayIP['click'] >= 750:
$levelown = 7;
break;
case $arrayIP['click'] >= 500:
$levelown = 6;
break;
case $arrayIP['click'] >= 350:
$levelown = 5;
break;
case $arrayIP['click'] >= 200:
$levelown = 4;
break;
case $arrayIP['click'] >= 50:
$levelown = 3;
break;
case $arrayIP['click'] >= 0:
$levelown = 2;
break;
}
$levelown = "Level " . $levelown;
}