Finding certain percentages from function - php

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

Related

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

Multiply results of two different functions

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.

php discounts for different ages and members [duplicate]

This question already has answers here:
The 3 different equals
(5 answers)
Closed 4 years ago.
I am attempting to create a program which can calculate the amount of discount for different ages and members. People aged under 12 can a 50% discount and a additional 10% if their a member. People aged under 18 or over 65 can have a 25% discount and an additional 10% on top if their a member.
My program only seems to work if the age is below 12 does anyone have any suggestions on how to fix (go easy on me i'm new to programming).
$ticketPrice = 25;
$age = 25;
$membership = 'Yes';
$finalPrice;
$discount;
$memberDis;
if($age < 12) {
$finalPrice = 25 / 2;
} else if($age < 18) {
$discount = 25 * 0.25;
$finalPrice = 25 - $discount;
} else if($age < 65) {
$discount = 25 * 0.25;
$finalPrice = 25 - $discount;
} else if($membership = 'Yes') {
$discount = $finalPrice * .10;
$memberDis = $dicount * 100;
}
echo "<br />";
echo "<h1>Ticket Example</h1>";
echo 'Inital Ticket Price: '."&pound".$ticketPrice;
echo "<br />";
echo "Age: ".$age;
echo "<br />";
echo "Member: ".$membership;
echo "<br />";
echo "Final Ticket Price: "."&pound".$finalPrice;
You should use the variable $ticketPrice instead of hardcoding its value in the if ..else.
Inside your conditional statements, just determine the $discount first.
Then outside the conditions, calculate the final price
Comparison operator is == not =.
Membership condition check should be moved out and separate from age checks.
Try
// Initialize discount to 0
$discount = 0;
$finalPrice = $ticketPrice;
if($age < 12) {
// if age is less than 12 then 50% discount
$discount = 50;
} elseif($age < 18 || $age > 65) {
// 25% discount for age < 18 or > 65
$discount = 25;
}
if ($membership == 'Yes') {
// additional 10% discount on membership
$discount += 10;
}
// now calculate the final price after removing discount
$finalPrice -= ($finalPrice*$discount/100);
You can see a flowchart representation of your code, in order to make easy to understand what are you doing and why it doesn't work.
As you can see the membership discount is applied if the age is greater or equals to 65, though you should see the difference between =, == and === operators.
according to your code structure, you should have:
<?php
$ticketPrice = 25;
$age = 25;
$membership = 'Yes';
$finalPrice;
$discount;
$memberDis;
if($age < 12) {
$finalPrice = 25 / 2;
} else if($age < 18) {
$discount = 25 * 0.25;
$finalPrice = 25 - $discount;
} else if($age < 65) {
$discount = 25 * 0.25;
$finalPrice = 25 - $discount;
}
if($membership === 'Yes') {
$discount = $finalPrice * .10;
$finalPrice -= $discount;
}
echo "<br />";
echo "<h1>Ticket Example</h1>";
echo 'Inital Ticket Price: '."&pound".$ticketPrice;
echo "<br />";
echo "Age: ".$age;
echo "<br />";
echo "Member: ".$membership;
echo "<br />";
echo "Final Ticket Price: "."&pound".$finalPrice;

Why is this integer considered a float?

I have created the following script for determining which page numbers to display for navigation on my website.
// query the database
$statement = $connect->prepare("SELECT COUNT(report_number) FROM reports");
$statement->execute();
$result = $statement->fetch(PDO::FETCH_ASSOC);
$rowCount = $result["COUNT(report_number)"];
$pgCount = ceil($rowCount / 10);
// fetch the corresponding entries from the database
if (!isset($pgParamArray["page"]) || $pgParamArray["page"] === $pgCount) {
$pgParamArray["page"] = $pgCount;
$sql = "SELECT * FROM reports ORDER BY report_number DESC LIMIT 10";
} elseif (isset($pgParamArray["page"])) {
$offset = ($pgCount - $pgParamArray["page"]) * 10;
$sql = "SELECT * FROM reports ORDER BY report_number DESC LIMIT " . $offset . ",10";
}
// calculate and display the appropriate page numbers
$upperDiff = $pgCount - $pgParamArray["page"];
$lowerDiff = $pgParamArray["page"] - 1;
for ($i = 0; $i < 5; $i++) {
$pgNo = 0;
if ($pgParamArray["page"]+2 <= $pgCount && $pgParamArray["page"]-2 >= 1) {
$pgNo = $pgParamArray["page"] + ($i - 2);
} elseif ($pgCount < 5) {
$pgNo = $i+1 <= $pgCount ? $i+1 : 0;
} elseif ($upperDiff < $lowerDiff && $upperDiff < 2 && $upperDiff >= 0) {
$pgNo = ($pgCount - 4) + $i;
} elseif ($lowerDiff < $upperDiff && $lowerDiff < 2 && $lowerDiff >= 0) {
$pgNo = $i + 1;
} else {
$pgNo = ($pgCount - 4) + $i;
}
if ($pgNo > 0) {
if ($pgNo !== $pgParamArray["page"]) {
$pgNoArray[] = ""
. "<li><div><a href='http://www.somewebsite.com/index.php/?page="
. $pgNo
. "'>"
. $pgNo
. "</a></div></li>";
} else {
$pgNoArray[] = ""
. "<li><div style='background:#333'>"
. $pgNo
. "</div></li>";
}
}
}
When I set $pgParamArray["page"] = $pgCount = ceil($rowCount/10), I have noticed that the foremost variable is still treated as a float. My question is why is $pgParamArray["page"] of type float even though ceil($rowCount/10) obviously returns an integer?
PHP's ceil returns a float according to their documentation.
Their rationale:
value rounded up to the next highest integer. The return value of ceil() is still of type float as the value range of float is usually bigger than that of integer.
This isn't just PHP that does this - C++ and Java both return floating point values for their ceiling functions.

MySQL/PHP - page number pagination Only show 10 pages at times

I am having difficulties with my MYSQL / PHP dashboard. - Currently i am having 50 pages, but currently they are all showing on the same page.
http://imgur.com/wDfTWUa - as you can see in the attached file. - I only want 10 pages to be shown, and be able to click through the rest of the pages without seeing 4 rows of pages.
Exsampel <- 2 3 4 5 6 7 8 9 10 -> when you are on ?page=1, if you are on page ?page=10 <- 11 12 13 14 15 16 17 18 19->
Hope you can help me.
Code:
<?php
include 'config.php';
$sidenr = $_GET['page'];
$sidenr2 = ($sidenr -1) * 10;
echo $sidenr2;
echo "<br><br>";
$query100 = mysqli_query($conn, "SELECT * FROM `test` LIMIT $sidenr2,10") or die(mysqli_error($conn));
while($row = mysqli_fetch_array($query100))
{
echo $row['id']."<br>";
}
$result = mysqli_query($conn, "SELECT * FROM test");
$num_rows = mysqli_num_rows($result);
$sideantal = $num_rows / 10;
echo "Der skal være antal sider: ". $sideantal;
echo "<br><br>antal rækker ". $num_rows . "<br><br>";
?>
<br><br>
<?php
for ($number = 1; $number <= $sideantal; $number++) {
echo "<li><a href=\"test.php?page=".$number."\" >". $number. "</a></li>";
}
?>
function getPageRange($current, $max, $total_pages = 10) {
$desired_pages = $max < $total_pages ? $max : $total_pages;
$middle = ceil($desired_pages/2);
if ($current <= $middle){
return [1, $desired_pages];
}
if ($current > $middle && $current <= ($max - $middle)) {
return [
$current - $middle,
$current + $middle
];
}
if ($current <= $max ) {
return [
$current - ($desired_pages - 1),
$max
];
}
}
list($min,$max) = getPageRange($sidenr, $sideantal);
foreach (range($min, $max) as $number) {
echo "<li><a href=\"test.php?page=".$number."\" >". $number. "</a></li>";
}
try to change this:
for ($number = 1; $number <= $sideantal; $number++) {
echo "<li><a href=\"test.php?page=".$number."\" >". $number. "</a>
</li>";
}
to this:
for ($number = 1; $number <= $sideantal; $number++) {
if (($number > $_GET['page']) && ($number <= $_GET['page'] + 10)) {
echo "<li><a href=\"test.php?page=".$number."\" >". $number. "</a>
</li>";
}
}
You can try the following code:
for ($number = 1; $number <= $sideantal; $number++) {
/** If the loop count is greater than the current page but less than current page plus 10 */
if ( ($number > $_GET['page'] && ($number < ($_GET['page'] + 10)))) $is_valid = true;
/** If the loop count is less than the current page but greater than current page -10 and the current page is the last page */
if ($number < $_GET['page'] && $_GET['page'] == $sideantal && $number > ($_GET['page'] - 10)) $is_valid = true;
else $is_valid = false;
if ($is_valid) {
echo "<li><a href=\"test.php?page=".$number."\" >". $number. "</a></li>";
}
}

Categories