$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
Related
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 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 ')
I have the need to count the number of integers which i am adding/substracting from equation.
Lets say I have a switch statement like that:
switch($_POST['dni']){
case 2:
$sum1 = $day1;
$sum2 = $day2;
break;
case 3:
$sum1 = $day1 + $day2;
$sum2 = $day3;
break;
case 4:
$sum1 = $day1 + $day2;
$sum2 = $day3 + $day4;
break;
case 5:
$sum1 = $day1 + $day2 + $day3;
$sum2 = $day4 + $day5;
break;
case 6:
$sum1 = $day1 + $day2 + $day3;
$sum2 = $day4 + $day5 + $day6;
break;
case 7:
$sum1 = $day1 + $day2 + $day3 + $day4;
$sum2 = $day5 + $day6 + $day7;
break;
case 8:
$sum1 = $day1 + $day2 + $day3 + $day4;
$sum2 = $day5 + $day6 + $day7 + $day8;
break;
And I have selected $_POST['dni'] to be 7, can I count the number of integers in $sum1 equation?
The things I have tried so far:
explode($sum1, '+') - returns the sum of the equation in array...
count($sum1) - returns 1, because it is counting again the sum of
equation...
Are you thinking of something like this? But without a better example this is all I got for you.
$day1 = 1;
$day2 = 2;
$day3 = 3;
$day4 = 4;
$array = array(
'0' => $day1,
'1' => $day2,
'2' => $day3,
'3' => $day4
);
$sum = array_sum($array);
echo 'Sum: = ' . $sum . '<br>'; //Outputs 10
echo '# of Integers: = ' . count($array); //Outputs 4
So I think it's a bad practice to use switch statement and re-did your code by -infer- method and resolved the issue, I hope it helps:
<?php
// In my example I have to declare the days and sums variables
// but I'm assuming you have them declared in your code already…
$dni = 8; // Or $_POST['dni'];
$day1 = 3;
$day2 = 4;
$day3 = 6;
$day4 = 8;
$day5 = 2;
$day6 = 5;
$day7 = 3;
$day8 = 10;
$day9 = 15;
$sum1 = $sum2 = 0;
// Here by what I saw in your code, can be simplified with basic arithmetic.
// First I divide $dni by 2, if it's an odd number it'll result in a float number.
// By using ceil we round it to the closest even number.
$primary_sum = ceil($dni / 2);
// Then just subtract the $primary_sum from $dni
// and you'll have your $secondary_sum.
$secondary_sum = $dni - $primary_sum;
// Here what I did is make an array with $dni number of keys.
// (Serves as your switch statement).
foreach (array_fill(1, $dni, '') as $day_number => $x)
{
// Follow the logic.
if ($day_number <= $primary_sum)
{
// This is a little trick but works like a charm.
$sum1 += ${"day{$day_number}"}; // It's basically $day1... and on.
}
else if ($day_number > $primary_sum)
{
// This is a little trick but works like a charm.
$sum2 += ${"day{$day_number}"};
}
}
// Number of integers in first sum (sum1)
var_dump($primary_sum);
// Number of integers in second sum (sum2)
var_dump($secondary_sum);
// The total sum of sum1 (pretty obvious).
var_dump($sum1);
// The total sum of sum2 (pretty obvious).
var_dump($sum2);
You can paste that code in phpsandbox and see it in action.
I am having a small problem with my PHP MySQL Select. The function is inside of a PHP class. Here is the error I get:
Warning: mysql_fetch_array() expects parameter 1 to be resource,
integer given in C:\xampp\htdocs\include\database.php on line 59
Warning: extract() expects parameter 1 to be array, null given in
C:\xampp\htdocs\include\database.php on line 59
The function just simply updates the database to show what browser and OS they visited the site with. The function is called from another file that is called by an AJAX call that uses POST to send the data about the OS and browser that was gathered from a Javascript file. It only fails if there is an entry of the IP address already in the database. If there is no IP Address entry in the database it succeeds in creating one.
Here is my code:
function addStat($browser, $os){
$IE = 0; $Firefox = 0; $Safari = 0; $Opera = 0; $Chrome = 0; $otherb = 0;
$Windows = 0; $Linux = 0; $Mac = 0; $Android = 0; $iOS = 0; $otheros = 0;
$ql = 0; $totalVisits = 0;
$ip = ip2long($_SERVER['REMOTE_ADDR']);
$q1 = mysql_query("SELECT * FROM " . DB_STATS . " WHERE ip='$ip'", $this->connection);
if (mysql_num_rows($q1)==0){
$browser = mysql_real_escape_string($browser);
$os = mysql_real_escape_string($os);
switch($browser){
case "Internet Explorer":
$IE += 1;
break;
case "Firefox":
$Firefox += 1;
break;
case "Safari":
$Safari += 1;
break;
case "Opera":
$Opera += 1;
break;
case "Chrome":
$Chrome += 1;
break;
default:
$otherb += 1;
break;
}
switch($os){
case "Windows":
$Windows += 1;
break;
case "Mac OS X":
$Mac += 1;
break;
case "Linux":
$Linux += 1;
break;
case "Android":
$Android += 1;
break;
case "iOS":
$iOS += 1;
break;
default:
$otheros += 1;
break;
}
$q = $this->query("INSERT INTO " . DB_STATS . " VALUES (null, '$ip', '$Chrome', '$IE', '$Firefox', '$Opera', '$Safari', '$otherb', '$Windows', '$Mac', '$Linux', '$Android' , '$iOS' , '$otheros', 1)");
if ($q == true){
return(1);
}
else{
return(0);
}
}
else if (mysql_num_rows($q1)==1){
extract(mysql_fetch_array($ql));
switch($browser){
case "Internet Explorer":
$IE += 1;
break;
case "Firefox":
$Firefox += 1;
break;
case "Safari":
$Safari += 1;
break;
case "Opera":
$Opera += 1;
break;
case "Chrome":
$Chrome += 1;
break;
default:
$otherb += 1;
break;
}
switch($os){
case "Windows":
$Windows += 1;
break;
case "Mac OS X":
$Mac += 1;
break;
case "Linux":
$Linux += 1;
break;
case "Android":
$Android += 1;
break;
case "iOS":
$iOS += 1;
break;
default:
$otheros += 1;
break;
}
$totalVisits += 1;
$q = $this->query("UPDATE " . DB_STATS . " set Chrome='$Chrome', IE='$IE', Firefox='$Firefox', Opera='$Opera', Safari='$Safari', otherb='$otherb', Windows='$Windows', Mac='$Mac', Linux='$Linux', Android='$Android' , iOS='$iOS' , otheros='$otheros', totalVisits='$totalVisits'");
if ($q == true){
return(1);
}
else{
return(0);
}
}
else{
return(-1);
}
}
I hope everything made sense and that someone will help.
I see it now -- you used $ql (lower case L) when you intend to use $q1. Let this be a lesson against using very short variable names or very similar names.
// $ql was initialized to 0
$ql = 0; $totalVisits = 0;
// $q1 holds the result resource
extract(mysql_fetch_array($q1));
It is not advisable to call extract() on the output of mysql_fetch_array() unless you also specify the second parameter MYSQL_ASSOC as the fetch type. By default it returns both numeric and associative indices for each column.
extract(mysql_fetch_array($q1, MYSQL_ASSOC));
// Or better
extract(mysql_fetch_assoc($q1));
In general, I would probably advise against using extract() in most any situation, since it results in numerous variables dumped into the global namespace, in particular when you have done SELECT * without being specific about which columns are selected. Better to access them via their array:
$row = mysql_fetch_assoc($q1);
echo $row['browser'];
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.