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 ";
} ?>
Related
I have placed results of two functions in two different columns of a table. In the third column I want to multiply the two results. Multiplication shows zero. First result is 6 and second is 3 - addition shows 63 (like string addition).
<tr>
<td>Economics</td><td><?php echo $e; ?></td><!--Economics marks-->
<td><?php $gra = sg($e); ?></td><!--Economics grade using function sg()-->
<td><?php $grap = sgp($e); ?></td><!--Economics grade points using sgp() function.-->
<td><?php echo $ec; ?></td><!--Economics credits-->
<td><?php $ce = ce3($e); ?></td><!--Economics credits earned using function ce3()-->
<td><?php echo $ce * $grap; ?></td><!--multiplication of two function results - this should be 18 - 3 x 6 but shows 3.-->
</tr>
Function to calculate earned credits:-
function ce3($marks) {
if ($marks == "A" || $marks == "CC") {
$marks = "---";
echo $marks;
} else {
$marks = 3;
echo $marks;
}
}
Function to calculate grade points:-
function sgp ($marks) {
if ($marks == "A" || $marks =="CC") {
$marks = "---";
echo $marks;
} elseif ($marks < 40) {
$marks = 0;
echo $marks;
} elseif ($marks >= 40 && $marks < 45) {
$marks = 4;
echo $marks;
} elseif ($marks >= 45 && $marks < 50) {
$marks = 5;
echo $marks;
} elseif ($marks >= 50 && $marks < 55) {
$marks = 6;
echo $marks;
} elseif ($marks >= 55 && $marks < 60) {
$marks = 7;
echo $marks;
} elseif ($marks >= 60 && $marks < 70) {
$marks = 8;
echo $marks;
} elseif ($marks >= 70 && $marks < 80) {
$marks = 9;
echo $marks;
} elseif ($marks >= 80) {
$marks = 10;
echo $marks;
}
}
you're missing return statements in your function...
function ce3($marks) {
if ($marks == "A" || $marks == "CC") {
$marks = "---";
echo $marks;
}
else {$marks = 3;
echo $marks;
}
return $marks;
}
your sgp() function also.
Just wondering what is the problem with my below php, I am trying to insert into two tables marks and results. For the first type of exams I want it to be inserted in the two tables. Second type of exams to be inserted to marks and then update results table, that is to take the marks of the previous exams and add together the assign grade. When I post the first marks, it goes well. Now the second marks is the there is problem; marks table will be inserted successfully but updating results table gives different results, result column either become 0 or 1, and grade remains the same. Can anyone study my codes below and tell me where there is problem please or any other way to do this! I will appreciated. Thank you.
<?php
include('../database/dbcon.php');
$tearcher_post_id = $_POST['tearcher_id'];
$year_id = $_POST['year_id'];
$term_id = $_POST['term_id'];
$subject_post_code = $_POST['subject_code'];
$student_no = $_POST['student_no'];
$exam_id = $_POST['exam_id'];
$class_id = $_POST['class_id'];
$marks = $_POST['marks'];
$grade = '';
if($marks >=0 && $marks<= 19){
$grade = 'E';
}else if($marks > 19 && $marks <= 29){
$grade = 'D-';
}else if($marks > 29 && $marks <=34){
$grade = 'D';
}else if($marks > 34 && $marks <=39){
$grade = 'D+';
}else if($marks > 39 && $marks <=44){
$grade = 'C-';
}else if($marks > 44 && $marks <= 49){
$grade = 'C';
}else if($marks > 49 && $marks <= 54){
$grade = 'C+';
}else if($marks > 54 && $marks <= 59){
$grade = 'B-';
}else if($marks > 59 && $marks <= 64){
$grade = 'B';
}else if($marks > 64 && $marks <= 69){
$grade = 'B+';
}else if($marks > 69 && $marks <= 79){
$grade = 'A-';
}else if($marks > 79 && $marks <= 100){
$grade = 'A';
}
$query_details = mysql_query(
"SELECT staff.staff_id, students.student_id, subjects.subject_id
FROM staff, students, subjects
WHERE staff.id_number='$tearcher_post_id'
AND students.admission_no='$student_no'
AND subjects.subject_code='$subject_post_code'")or die(mysql_error());
$row_details = mysql_fetch_array($query_details);
$num_details = mysql_num_rows($query_details);
if($num_details > 0){
$staff_id= $row_details['staff_id'];
$student_id= $row_details['student_id'];
$subject_id= $row_details['subject_id'];
$query_marks = mysql_query("SELECT *
FROM marks
WHERE student_id='$student_id'
AND year_id='$year_id'
AND exam_type_id='$exam_id'
AND subject_id='$subject_id'")or die(mysql_error());
$num_marks = mysql_num_rows($query_marks);
if($num_marks > 0){
echo 'marks_excist';
}else{
$query = mysql_query("INSERT
INTO marks
(student_id,
staff_id,
term_id,
year_id,
exam_type_id,
subject_id,
class_id,
marks)
values(
'$student_id',
'$staff_id',
'$term_id',
'$year_id',
'$exam_id',
'$subject_id',
'$class_id',
'$marks' )")or die(mysql_error());
if($query){
$query_results = mysql_query("SELECT *
FROM results
WHERE student_id='$student_id'
AND term_id='$term_id'
AND year_id='$year_id'
AND subject_id='$subject_id'")or die(mysql_error());
$row_results = mysql_fetch_array($query_results);
$num_results = mysql_num_rows($query_results);
if($num_results > 0){
$id= $row_results['result_id'];
$results = $row_results['results'];
$total = $results + $marks;
$grade = '';
if($total >=0 && $total<= 19){
$grade = 'E';
}else if($total > 19 && $total <= 29){
$grade = 'D-';
}else if($total > 29 && $total <=34){
$grade = 'D';
}else if($total > 34 && $total <=39){
$grade = 'D+';
}else if($total > 39 && $total <=44){
$grade = 'C-';
}else if($total > 44 && $total <= 49){
$grade = 'C';
}else if($total > 49 && $total <= 54){
$grade = 'C+';
}else if($total > 54 && $total <= 59){
$grade = 'B-';
}else if($total > 59 && $total <= 64){
$grade = 'B';
}else if($total > 64 && $total <= 69){
$grade = 'B+';
}else if($total > 69 && $total <= 79){
$grade = 'A-';
}else if($total > 79 && $total <= 100){
$grade = 'A';
}
mysql_query("UPDATE results
SET results = '$total' AND grade = '$grade'
WHERE result_id = '$id'") or die(mysql_error());
echo 'true_marks';
}else{
mysql_query("INSERT
INTO results
(student_id,
year_id,
class_id,
term_id,
subject_id,
results,
grade)
values(
'$student_id',
'$year_id',
'$class_id',
'$term_id',
'$subject_id',
'$marks',
'$grade' )")or die(mysql_error());
}
}else{
echo 'false_marks';
}
}
}else{
echo 'details_dont_excist';
}
?>
I'm kind of new to PHP and I'm trying to write a script that counts points after a user inputs values.
Here's the code:
<?php
$pla = $_GET['players'];
$plu = $_GET['plugins'];
$type = $_GET['type'];
$location = $_GET['location'];
totalPoints();
function typePoints($type) { //Returns points depending on server type
switch (strtolower($type)) { //Switch the type of server between all types and see how many points to return :)
case "standard minecraft": return 1;
break;
case "snapshot": return 2;
break;
case "craftbukkit": return 2;
break;
case "bungeecord": return 1;
break;
case "spigot": return 2;
break;
case "paperspigot": return 2;
break;
case "feed the beast": return 3;
break;
case "technic": return 3;
break;
case "pixelmon": return 3;
break;
default: echo 'Incorrect Server Type!';
exit;
break;
}
}
function playerPoints($players) { //Returns points depending on amount o players
if ($players >= 2 && $players <= 5) {
return 2;
} elseif ($players >= 6 && $players <= 10) {
return 4;
} //Between 6-10, return 4 points... AND SO ON...
elseif ($players >= 11 && $players <= 16) {
return 8;
} elseif ($players >= 17 && $players <= 25) {
return 12;
} elseif ($players >= 26 && $players <= 30) {
return 16;
} elseif ($players >= 31 && $players <= 36) {
return 18;
} elseif ($players >= 37 && $players <= 45) {
return 20;
} elseif ($players >= 46 && $players <= 50) {
return 22;
} elseif ($players >= 51 && $players <= 56) {
return 24;
} elseif ($players >= 57 && $players <= 65) {
return 26;
} elseif ($players >= 66 && $players <= 70) {
return 28;
} elseif ($players >= 71 && $players <= 76) {
return 30;
} elseif ($players >= 77 && $players <= 85) {
return 32;
} elseif ($players >= 86 && $players <= 90) {
return 34;
} elseif ($players >= 91 && $players <= 96) {
return 36;
} elseif ($players >= 97 && $players <= 105) {
return 38;
} elseif ($players >= 106 && $players <= 110) {
return 40;
} elseif ($players > 110) {
return 44;
}
}
function pluginPoints($plugins) {
if ($plugins == 0) {
return 0;
} elseif ($plugins >= 1 && $plugins <= 3) {
return 2;
} //Between 1-3, return 2 points.... AND SO ON...
elseif ($plugins >= 4 && $plugins <= 15) {
return 6;
} elseif ($plugins >= 16 && $plugins <= 30) {
return 10;
} elseif ($plugins >= 31 && $plugins <= 40) {
return 14;
} elseif ($plugins >= 41 && $plugins <= 50) {
return 20;
} elseif ($plugins > 50) {
return 24;
}
}
function locationPoints($location) {
switch (strtolower($location)) { //Switch between locations, easy to add a new one later in the future. :)
case "montreal": return 1;
break;
case "france": return 1;
break;
default: echo "Incorrect Location!";
exit;
break;
}
}
function totalPoints() { //Call this function to get the amount of points finally.
$totalPoints = typePoints($type) + playerPoints($pla) + pluginPoints($plu) + locationPoints($location);
echo 'Total points: ' . $totalPoints;
}
?>
The problem is the switch statement in function typePoints($type) always returns the default: echo 'Incorrect Server Type!'; exit; break;.
Also, function playerPoints($players) returns 0 even though I put in a number like 37.
I used this:
http://website.com/planpicker.php?players=121&location=france&plugins=80&type=technic
Can anyone tell me why?
This is not a duplicate question, the "marked duplicate" talks about different files, but this is within the same file.
The 4 variables are not accessible from totalPoints function. You should pass those parameters to function. You should call function as :
totalPoints($type,$pla,$plu,$location);
And define the function as
function totalPoints($type,$pla,$plu,$location)
It's because you call your functions in another function without passing the values in parameter.
The totalPoints function is not aware of the value of $type or $pla for example as they are not GLOBALS.
You have to pass the variables to the function you want to call :
function totalPoints($type, $pla, $plu, $location) //Call this function to get the amount of points finally.
{
$totalPoints = typePoints($type) + playerPoints($pla) + pluginPoints($plu) + locationPoints($location);
echo 'Total points: '.$totalPoints;
}
And then call the function like this :
totalPoints($type,$pla,$plu,$location);
The variables that were decared outside of the functions need to be made accessible to the totalPoints function. The easiest way is to use global
function totalPoints() {
global $pla;
global $plu;
global $type;
global $location;
$totalPoints = typePoints($type) + playerPoints($pla) + pluginPoints($plu) + locationPoints($location);
echo 'Total points: '.$totalPoints;
}
How can I optimize this if-statement?
if ($min && $max && $value && ($min <= $max) && ($min <= $value) && ($value <= $max)) {
// do anything
}
What it should do:
I got three values (min, max and value). First of all, all values should be != 0. Second, min <= value <= max.
Valid:
min = 1; max = 3; value = 2;
min = 2; max = 2; value = 2;
this:
if ( 0 < $min && $min <= $value && $value <= $max ){
echo 'good';
}
The answer is:
if(isset($min , $max , $value ) && ($min <= $value) && ($value <= $max)){
//Insert your code here
}
I think this prevents any value from being a 0 and makes sure value is inside the min and max range.
if ( ( $min > 0 && $max >= $min ) && ( $value >= $min && $value <= $max ) ) {
echo "Good";
} else {
echo "Bad";
}
Here is my problem. I upload a csv file containing two columns (student Number and Score). But when i form an HTML table from the data, i'll like to have a column display a grade according to the score uploaded... Also all the courses that have a score >40 should all be put into another table. Below is the loop i'm trying to make work, but it's not getting me anywhere near it.
Thanks for the help. I most appreciate it. Thanks
while ($row4 = mysql_fetch_assoc($query4)) {
if ($row4['score'] >= 70) {
$grade = A;
}
elseif ($row4['score'] >= 60) {
$grade = B;
}
elseif ($row4['score'] >= 50) {
$grade = C;
}
elseif ($row4['score'] >= 45) {
$grade = D;
}
elseif($row4['score'] >= 40) {
$grade = E;
}
elseif($row4['score'] >= 40) {
$grade = F;
}else {
$grade = AR;
}
}
If I understand correctly, the following should do what you are looking for. I assume that your Course ID and Student ID are in the database row, but you will need to update those if not.
<?php
define('GRADE_A_MIN', 70);
define('GRADE_B_MIN', 60);
define('GRADE_C_MIN', 50);
define('GRADE_D_MIN', 45);
define('GRADE_E_MIN', 40);
define('GRADE_F_MIN', 35);
$table_one_values = array(); // Used to create table for students with scores 40+
$table_two_values = array();
while ($row4 = mysql_fetch_assoc($query4)) {
$score = $row4['score'];
$destination_table = 'two';
if ( $score >= GRADE_A_MIN ) {
$grade = 'A';
$destination_table = 'one';
} elseif ( $score >= GRADE_B_MIN ) {
$grade = 'B';
$destination_table = 'one';
} elseif ( $score >= GRADE_C_MIN ) {
$grade = 'C';
$destination_table = 'one';
} elseif ( $score >= GRADE_D_MIN ) {
$grade = 'D';
$destination_table = 'one';
} elseif ( $score >= GRADE_E_MIN ) {
$grade = 'E';
$destination_table = 'one';
} elseif ( $score >= GRADE_F_MIN ) {
$grade = 'F';
} else {
$grade = 'AR';
}
$table = 'table_'.$destination_table.'_values';
$$table[] = array(
'course_id' => $row4['course_id'],
'student_id' => $row4['student_id'],
'score' => $score,
'grade' => $grade
);
}
Some problems:
Your $grade variable gets overwritten in the loop so you only will have the last value at the end. As your row contains a student number as well, you could do something like: $grade[$row4['number']] = 'A'; to link that specific grade to a specific student number. Note that you would set your array before the loop: $grade = array();
Your assignment is wrong, unless A, B, etc. are constants. It should probably be 'A' instead of A, etc. So: $grade[$row4['number']] = 'C';, etc.