Multiply results of two different functions - php

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.

Related

Finding certain percentages from function

I'm making a grade calculator where the user inputs their earned points, points possible, then its weight within the class. Afterwards it then calculates and prints the percentage earned for each portion along with its weight. Finally, it prints your total calculated score along with the letter grade. I'm able to print the very last line completely, along with each categories' weight, but I can't print out the percentage earned in each category
<?php
$par_Earned = $_POST['earnedParticipation'];
$par_Possible = $_POST['maxParticipation'];
$par_Weight = $_POST['weightParticipation'];
$q_Earned = $_POST['earnedQuiz'];
$q_Possible = $_POST['maxQuiz'];
$q_Weight = $_POST['weightQuiz'];
$l_Earned = $_POST['earnedLab'];
$l_Possible = $_POST['maxLab'];
$l_Weight = $_POST['weightLab'];
$p_Earned = $_POST['earnedPracticum'];
$p_Possible = $_POST['maxPracticum'];
$p_Weight = $_POST['weightPracticum'];
function percentage($a,$b){
$percent = ($a/$b)*100;
return $percent;
}
function weightValue($c,$d,$e){
$weight = (percentage($c,$d) * $e)/100 ;
return $weight;
}
$parWeight = weightValue($par_Earned,$par_Possible,$par_Weight);
$quizWeight = weightValue($q_Earned,$q_Possible,$q_Weight);
$labWeight = weightValue($l_Earned,$l_Possible,$l_Weight);
$pracWeight = weightValue($p_Earned,$p_Possible,$p_Weight);
$total = $parWeight+$quizWeight+$labWeight+$pracWeight;
function lettergrade($total) {
if ($total >= 95) {
$lettergrade = "A+";
}
else if ($total < 95 && $total >= 90 ) {
$lettergrade = "A";
}
else if ($total < 90 && $total >= 85) {
$lettergrade = "B+" ;
}
else if ($total < 85 && $total >= 80) {
$lettergrade = "B" ;
}
else if ($total < 80 && $total >= 75) {
$lettergrade = "C+" ;
}
else if ($total < 75 && $total >= 70) {
$lettergrade = "C" ;
}
else if ($total < 70 && $total >= 60) {
$lettergrade ="D" ;
}
else if ($total < 60 && $total >= 0) {
$lettergrade = "F" ;
}
else {
$lettergrade = "Grade is not valid";
}
return $lettergrade;
}
echo "<p>You earned a X with a weighted value of " .$par_Weight. "% </p>";
echo "<p>You earned a X with a weighted value of " .$q_Weight. "% </p>";
echo "<p>You earned a X with a weighted value of " .$l_Weight. "% </p>";
echo "<p>You earned a X with a weighted value of " .$p_Weight. "% </p>";
echo "<p><b>Your final grade is " .$total. "%, which is a ".lettergrade($total)."</b></p>";
?>

Display students grades in alphabetical order

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 ";
} ?>

PHP Switch case & if statement not returning correct values

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;
}

Prime numbers up to a certain number

here is the problem, i need to find prime numbers up to a certain number and here is my code:
$a = 56;
for($i = 2; $i<=$a; $i++)
{
if($i == 2)
{
echo "2</br>";
}
if($i == 3)
{
echo "3</br>";
}
if($i == 5)
{
echo "5</br>";
}
if($i == 7)
{
echo "7</br>";
}
for($j =3; $j <= ceil($i/2); $j = $j + 2)
{
if($i % 2 == 0)
{
break;
}
if($i % 3 == 0)
{
break;
}
if($i % 5 == 0)
{
break;
}
if($i % 7 == 0)
{
break;
}
else
{
echo "$i</br>";
break;
}
}
}
It works fine, but it kinda seems like a brute force algorithm, doesnt it? Is there any other way to do this?
Thanks for help!!!
Suppose x is the limit (till which you want prime number)..
for($n=2;$n<=$x;$n++)
{
$i=2;
while($i<=$n-1)
{
if($n % $i == 0)
break;
$i++;
}
if($i==$num)
echo $n; // $n is the prime number...
}

PHP - If number is divisible by 3 and 5 then echo

I'm new to PHP and trying to create the following whilst minimizing the amount of code needed. PHP should show a list of 100 then display if the number is / by 3, 5 or 3 and 5. If not by any then show nothing.
This is what I've got so far, but any help would be great since not sure about the / by 3 and 5 bit as you can see below.
<?php $var = range(0, 100); ?>
<table>
<?php foreach ($var as &$number) {
echo " <tr>
<td>$number</td>
<td>";
if($number % 3 == 0) {
echo "BY3";
} elseif ($number % 5 == 0) {
echo "BY5";
} elseif ($number % 3 and 5 == 0) {
echo "BY3 AND 5";
}
echo "</td></tr>";
}
?>
</table>
Thanks
Nope... you should check first if it's divisble for 15 (3x5) (or 3 and 5) and after you can do other checks:
if($number % 15 == 0) {
echo "BY3 AND 5";
} elseif ($number % 5 == 0) {
echo "BY5";
} elseif ($number % 3 == 0) {
echo "BY3";
}
echo "</td></tr>";
?>
Because every number divisble for 15 is also divisble for 3 and 5. So your last check could never hit
if I'm reading your question correct then you are looking for :
if ($number % 3 == 0 && $number %5 == 0) {
echo "BY3 AND 5";
} elseif ($number % 3 == 0) {
echo "BY3";
} elseif ($number % 5 == 0) {
echo "BY5";
}
Alternative version :
echo ($number % 3 ? ($number % 5 ? "BY3 and 5" : "BY 3") : ($number % 5 ? "BY 5" : ""));
$num_count = 100;
$div_3 = "Divisible by 3";
$div_5 = "Divisible by 5";
$div_both = "Divisible by 3 and 5";
$not_div = "Not Divisible by 3 or 5";
for($i=0;$i<=$num_count;$i++)
{
switch($i)
{
case ($i%15==0):
echo $i." (".$div_both.")</br>";
break;
case ($i%3==0):
echo $i." (".$div_3.")</br>";
break;
case ($i%5==0):
echo $i." (".$div_5.")</br>";
break;
default:
echo $i."</br>";
break;
}
}
No need to do three if statements:
echo "<table border='1'>";
for ($i = 1; $i <= 100; $i++) {
echo "<tr><td>{$i}</td><td>";
if ($i % 3 == 0) echo "BY3 ";
if ($i % 5 == 0) echo "BY5";
echo "</td></tr>\n";
}
echo "</table>";
Update the code as given below
<?php $var = range(0, 100); ?>
<table>
<?php foreach ($var as &$number)
{
echo " <tr>
<td>$number</td>
<td>";
if($number % 3 == 0 && $number % 5 == 0)
{
echo "BY3 AND 5";
}
elseif ($number % 5 == 0)
{
echo "BY5";
}
elseif ($number % 3 == 0)
{
echo "BY3";
}
echo "</td></tr>";
}
?>
<?php
if($number % 5 == 0 && $number % 3 == 0) {
echo "BY3 AND 5";
} elseif ($number % 5 == 0) {
echo "BY5";
} elseif ($number % 3 == 0) {
echo "BY3";
} else{
echo "NOT BY3 OR 5";
}
?>
if($number % 15 == 0)
{
echo "Divisible by 3 and 5";
}
elseif ($number % 5 == 0)
{
echo "Divisible by 5";
}
elseif ($number % 3 == 0)
{
echo "Divisible by 3";
}
This is neater and completed to be run:
<?php
for ($i = 1; $i <= 100; $i++) {
if ($i % 15 == 0)
{
echo"Divisible by 3 and 5</br>";
}
elseif ($i%3==0)
{
echo"Divisible by 3</br>";
}
elseif ($i%5==0)
{
echo"Divisible by 5</br>";
}
else
{
echo $i,"</br>";
}
}
?>
<?php
for ($i = 1; $i <= 100; $i++) {
if ($i % 15 == 0) echo "This Number is Divisible by 3 and 5<br>";
else if ($i % 3 == 0) echo "This Number is Divisible by 3 only<br>";
else if ($i % 5 == 0) echo "This number is Divisible by 5 only<br>";
else{
echo "$i<br>";
}
}
?>

Categories