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;
}
Related
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 ";
} ?>
code:
<?php
if($outoff!=0)
{
$grade = ($score/$outoff)*100;
if($grade <= 39)
{
echo '<span class="text-danger">Bad</span>';
}
else if($grade >=74)
{
echo '<span class="text-warning">Average</span>';
}
else if($grade >=100)
{
echo '<span class="text-success">Good</span>';
}
}
else
{
//no comment please
}
?>
Show grade according to:-
0-39 (Bad)
40-74 (Average)
75-100 (Good)
In this question I want to show message bad, average, good according to grade. Suppose if grade is 0-39 then it will show bad similarly if grade is 40-74 then show average like this but the condition I am giving is wrong. So, how can I do it?
Just change greater than to less than.
<?php
if($outoff!=0)
{
$grade = ($score/$outoff)*100;
if($grade <= 39)
{
echo '<span class="text-danger">Bad</span>';
}
else if($grade <=74) //Change to less than here.
{
echo '<span class="text-warning">Average</span>';
}
else if($grade <=100) //Change to less than here.
{
echo '<span class="text-success">Good</span>';
}
}
else
{
//no comment please
}
You need to modify the conditions so that no score is missed out of grade.
So, please define 3 ranges of scores using if and `else if'.
Range 1: 0-39: if ($grade <= 39) {
Range 2: 40-74: else if($grade <=74) {
Range 3: 75-100: else if($grade <=100) {
This way, first if checks if the grade is less than or equal to 39.
If yes, grade is Bad.
Else, if score, does not fit in this range, it will go ahead in next if else for the range: 40-74 and same way to 75-100 if it does not fit.
Corrected code:
if ($outoff!=0) {
$grade = ($score/$outoff)*100;
if ($grade <= 39) { // Score range: 0-39
echo '<span class="text-danger">Bad</span>';
}
// If $score is coming to this else if means it is definitely
// greater than 39: that is 40+
// Score range: 40-74 as it is in else if after if of `39`
else if($grade <=74) {
echo '<span class="text-warning">Average</span>';
}
// Score range: 75-100 as it is in else if after 0 - 39 and 40 - 74
else if($grade <=100) {
echo '<span class="text-success">Good</span>';
}
}
You have to make changes to you code as follow:
<?php
if($outoff!=0)
{
$grade = ($score/$outoff)*100;
if( $grade >= 0 && $grade < 40 ) {
echo '<span class="text-danger">Bad</span>';
}
else if( $grade > 39 && $grade < 75 ) {
echo '<span class="text-warning">Average</span>';
}
else if($grade > 74 && $grade <= 100 ) {
echo '<span class="text-success">Good</span>';
}
}
else
{
//no comment please
}
?>
<?php
if($outoff!=0)
{
$grade = ($score/$outoff)*100;
if( $grade > 0 && $grade <= 39 ) {
echo '<span class="text-danger">Bad</span>';
}
else if( $grade >= 40 && $grade <= 74 ) {
echo '<span class="text-warning">Average</span>';
}
else if($grade >= 75 && $grade <= 100 ) {
echo '<span class="text-success">Good</span>';
}
}
else
{
//no comment please
}
?>
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 ')
This question already has answers here:
Switch doesn't work with numeric comparison cases
(4 answers)
Closed 8 years ago.
I have a function like this:
public static function GetNilaiHuruf($nilai)
{
if($nilai > 4)
{
$nilai = (float) ((float)($nilai) / (float)(100.0) * (float)4.0);
}
switch($nilai)
{
case ($nilai > 3.66 && $nilai <= 4) :
return "A";
break;
case ($nilai > 3.33 && $nilai <= 3.66) :
return "A-";
break;
case ($nilai > 3.00 && $nilai <= 3.33) :
return 'B+';
break;
case ($nilai > 2.66 && $nilai <= 3.00) :
return 'B';
break;
case ($nilai > 2.33 && $nilai <= 2.66) :
return 'B-';
break;
case ($nilai > 2.00 && $nilai <= 2.33) :
return 'C+';
break;
case ($nilai > 1.66 && $nilai <= 2.00) :
return 'C';
break;
case ($nilai > 1.33 && $nilai <= 1.66) :
return 'C-';
break;
case ($nilai > 1.00 && $nilai <= 1.33) :
return 'D+';
break;
case ($nilai >= 0.00 && $nilai <= 1.00) :
return 'D';
break;
}
}
When I call that function with $nilai is 0, the function returns 'A';
When I call that function with $nilai is 0.007 or 'x', the function returns 'D'.
How is that possible?
That is not how switch statements work, they shouldn't be used to do numeric comparisons like that.
However, you can achieve what you want by using boolean true as your switch argument:
$yourvar = -20;
switch(true) {
case ($yourvar > 0 && $yourvar < 5) :
echo 'Hello world!';
break;
case ($yourvar < 0) :
echo 'Hello hell!';
break;
}
// Hello hell!
Please see this article for more info.
There's no need to try to use a switch here. Use if/else if/else:
if ($nilai > 3.66 && $nilai <= 4) {
return "A";
} else if ($nilai > 3.33 && $nilai <= 3.66) {
return "A-";
} ...
A sample of what I'm trying to do will be more explicit:
var_dump($opti_point); //int 0
if ($opti_point>=0 && $opti_point < 25) echo 'good';//echoing good
switch ($opti_point) {
case ($opti_point>= 0 && $opti_point < 25):
$test = 0;
break;
case ($opti_point >= 25 && $opti_point < 50):
echo 'we are in this case'; // This case is called !
$test = 2;
break;
default:
test = 0;
break;
}
Is there a trick here ?
thx
You cannot put comparisons inside "case" unfortunately...
A switch is only used when a value can have one of a limited number of values like so:
switch ( $val ) {
case 1:
echo "Got 1";
break;
case 2:
echo "Got 2";
break;
default:
echo "Got invalid value";
}
A workaround would be to use:
switch (true) {
case ($opti_point>= 0 && $opti_point < 25):
$test = 0;
break;
case ($opti_point >= 25 && $opti_point < 50):
echo 'we are in this case';
$test = 2;
break;
default:
test = 0;
break;
}
Which will work, but is a bit ugly...
Also, you're missing a single quote in echo we are in this case'; which should be echo 'we are in this case';
You should be using an if instead =)
You need to change the switch argument to true of false if you do comparison liek that in the cases.
You are not comparing what you think you are comparing. This is the code I think you want.
var_dump($opti_point); //int 0
if ($opti_point>=0 && $opti_point < 25) {
$test = 0;
echo 'You are now here!';
} elseif ($opti_point >= 25 && $opti_point < 50) {
$test = 2;
} else {
test = 0;
}
In your example, you are comparing the result of the logical statement...
($opti_point>=0 && $opti_point < 25) // true
To the value of $opti_point
0 // false
So PHP is actually converting what you think in an integer into a boolean to compare it with the result of the conditional statement.
I think that's a very bad way to use a switch statement, you should not put conditional sentences in the cases... In fact, I'm sure that that would be illegal in other languages and I'm not sure that it should work in PHP. Use a number of concatenated if-else conditions instead:
if ($i == 0) {
echo "i equals 0";
} elseif ($i == 1) {
echo "i equals 1";
} elseif ($i == 2) {
echo "i equals 2";
}
I'll go true the code with you
var_dump($opti_point); //int 0 , or false --- you should use TRUE
if ($opti_point>=0 && $opti_point < 25) echo 'good';//echoing good
switch ($opti_point) { // chose the case that is $opti_point (0 or false)
case ($opti_point>= 0 && $opti_point < 25): // true, so go to next
$test = 0;
break;
case ($opti_point >= 25 && $opti_point < 50): //false si this is the wan I pick
echo 'we are in this case'; // This case is called !
$test = 2;
break; // ingore the rest
default:
test = 0;
break;
}
you should use TRUE in the switch
if this is the exact code then try this
var_dump($opti_point); //int 0
if ($opti_point>=0 && $opti_point < 25) echo 'good';//echoing good
switch ($opti_point) {
case ($opti_point>= 0 && $opti_point < 25):
$test = 0;
break;
case ($opti_point >= 25 && $opti_point < 50):
echo 'we are in this case'; // This case is called !
$test = 2;
break;
default:
$test = 0;
break;
}
You have a misunderstanding on how switch-case works. Case DOES NOT TEST YOUR EXPRESSION TO BE boolean TRUE!
It compares its value to 'switch' value!
Here is an explanation:
$opti_point>= 0 && $opti_point < 25 evalutes to true which integer representation is 1 and since PHP can deal with types on it's own, it turnes true to 1 and compares it with value in switch which is 0
$opti_point >= 25 && $opti_point < 50 evaluates to false which is 0 as integer, so... that's your case ;)