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;
}
Related
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 ')
$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
I have this PHP function for changing color between 2 numbers:
function color_switch($number){
switch (true){
case $number == range(1 , 3):
$color = "progress-bar-danger";
break;
case $number == range(3 , 5):
$color = "progress-bar-warning";
break;
case $number == range(5 , 6):
$color = "progress-bar-default";
break;
case $number == range(6 , 8):
$color = "progress-bar-success";
break;
case $number == range(8, 10):
$color = "progress-bar-success";
break;
}
return $color;
}
But in action this function does not work for me. How should I fix this ?
You are comparing range() which is an array, and $number is integer, which is invalid,
Change your function something like,
function color_switch($number) {
switch ($number) { // switching the function argument
case $number <= 3 : // if less than three, execute case
$color = "progress-bar-danger";
break;
case $number <= 5 :
$color = "progress-bar-warning";
break;
case $number <= 6 :
$color = "progress-bar-default";
break;
case $number <= 8 :
$color = "progress-bar-success";
break;
case $number <= 10 :
$color = "progress-bar-success";
break;
}
return $color;
}
Your utilization of switch is incorrect and your utilization of range() is too.
Your parameter of switch should be the variable you evaluate.
Range() will return an array containing the range.
So the correct code is better :
function color_switch($number) {
switch ($number){
case in_array($number, range(1 , 3)):
$color = "progress-bar-danger";
break;
case in_array($number, range(3 , 5)):
$color = "progress-bar-warning";
break;
case in_array($number, range(5 , 6)):
$color = "progress-bar-default";
break;
case in_array($number, range(6 , 8)):
$color = "progress-bar-success";
break;
case in_array($number, range(8 , 10)):
$color = "progress-bar-success";
break;
}
return $color;
}
(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;
}
What do I have to write in the cases (of switch function in PHP) to make if $number > 0 && $number < 15?
I tried with:
<?php
$number = rand("0","100");
switch($number) {
case: $number > 0 && $number < 15;
$output = 1;
break;
case: $number > 15 && $number < 50;
$output = 2;
break;
}
return $output;
?>
Try the following. Your colons were in the wrong place!
<?php
$number = rand("0", "100");
switch(true) {
case $number > 0 && $number < 15:
$output = 1;
break;
case $number > 15 && $number < 50:
$output = 2;
break;
}
?>
From the comment by DaveRandom below. Be aware that nothing will happen if $number is equal to 15. I would recommend changing one of your switch statements. Perhaps by changing your first case into:
case $number > 0 && $number <= 15: