Using php function to grade elements of a numerically indexed array - php

I intend to grade the elements of a numerically indexed array in php using function call; my function call does not work: it doesn't grade the elements and I cannot figure out the error in the code.Please help
Thanks, in advance
<?php
//function intended to grade array elements
function gradeArray($x){
if($score>= 70){
echo"A";
}
elseif($score >= 50){
echo"B";
}
elseif($score>= 40){
echo"C";
}
else{
echo"F";
}
}
// Array of Scores to be graded
$scores = array ("55", "68", "43", "78");
//Display result in a tabular form
echo"<table border = '1'><th>Score</th><th>Grade</th>";
foreach($scores as $score){
echo"<tr><td>";
echo$score."</td>";
echo"<td>". gradeArray($score);
echo"</td></tr>";
}
echo"</table>";
?>

You are passing $x into your function then calling $score.
Your scores array is also in string format, just need to remove the quotes to make them numbers.
Also change $x to $score and it should work fine! :)
<?php
//function intended to grade array elements
function gradeArray($score) {
if ($score >= 70) return "A";
elseif ($score >= 50) return "B";
elseif ($score >= 40) return "C";
else return "F";
}
// Array of Scores to be graded
$scores = array (55, 68, 43, 78);
//Display result in a tabular form
echo "<table border='1'><th>Score</th><th>Grade</th>";
foreach ($scores as $score) {
echo "<tr><td>$score</td><td>" . gradeArray($score) . "</td></tr>";
}
echo "</table>";
?>

Your array elements are in string .convert all element to int using
gradeArray($x){
$score=(int)$x;
}
Try this it will work

First off, most likely $score is undefined in your function since you use $x.
function gradeArray($x){
Then you are using your if conditions as if($score>= 70){.
Also, in your return values, just use return.
return"A"; // and others
Use return not echo so that this concatenation echo "<td>". gradeArray($score); works.

$score is undefined in your variable, the function is fetching the variable as $x try to change $score to $x or define $score., you can use global $score also in your function. Also you should return the values instead of using echo use the code below
<?php
//function intended to grade array elements
function gradeArray($x){
$score=$x;
if($score>= 70){
return "A";
}
elseif($score >= 50){
return "B";
}
elseif($score>= 40){
return "C";
}
else{
return "F";
}
}
// Array of Scores to be graded
$scores = array ("55", "68", "43", "78");
//Display result in a tabular form
echo"<table border = '1'><th>Score</th><th>Grade</th>";
foreach($scores as $score){
echo"<tr><td>";
echo$score."</td>";
echo"<td>". gradeArray($score);
echo"</td></tr>";
}
echo"</table>";
?>
Hope this helps you

Try this
<?php
//function intended to grade array elements
function gradeArray($x) {
if ($x >= 70) {
return "A";
} elseif ($x >= 50) {
return "B";
} elseif ($x >= 40) {
return "C";
} else {
return "F";
}
}
// Array of Scores to be graded
$scores = array("55", "68", "43", "78");
//Display result in a tabular form
echo"<table border = '1'><th>Score</th><th>Grade</th>";
foreach ($scores as $score) {
echo"<tr><td>";
echo$score . "</td>";
echo"<td>" . gradeArray($score);
echo"</td></tr>";
}
echo"</table>";
?>

function grading( $marks ){
$grade = mysql_query("SELECT grade_name,grade_point FROM table_name **strong text**WHERE smark <= round($marks) AND hmark >= round($marks)");
$gd = mysql_fetch_row( $grade );
return $gd[0];
}

Related

Php how to echo if

Hello I have problem with PHP if else at echo part
where my echo didn't count my php result with * instead its calculate normally with variable $tot_gaji
<td align="right">
<?php
$tot_gaji = $row['gajipokok']+$rows['jlhbonus'];
if ($tot_gaji>=4500000) {
$tot_gaji+0.05*$tot_gaji;
} elseif ($tot_gaji>=5000000) {
$tot_gaji+0.15*$tot_gaji;
}
echo number_format($tot_gaji,0,".",",");
?>
</td>
If you don't assign the calculated results to the variable, it be still unchanged. So you have to assign the calculated result to a variable
<td align="right">
<?php
$tot_gaji = $row['gajipokok']+$rows['jlhbonus'];
if ($tot_gaji>=4500000) {
$tot_gaji = $tot_gaji+0.05*$tot_gaji;
// ^^^^^^^^^^^
} elseif ($tot_gaji>=5000000) {
$tot_gaji = $tot_gaji+0.15*$tot_gaji;
// ^^^^^^^^^^^
}
echo number_format($tot_gaji,0,".",",");
?>
</td>
You are not assigning the mathematics expression to any variable that's why the values of variable $tot_gai stay original
$tot_gaji = $row['gajipokok']+$rows['jlhbonus'];
if ($tot_gaji>=4500000)
{
$final = $tot_gaji+0.05*$tot_gaji;
}
elseif ($tot_gaji>=5000000)
{
$final = $tot_gaji+0.15*$tot_gaji;
}
echo number_format($final,0,".",",");
You have to assign the calculated values to $tot_gaji
$tot_gaji = $row['gajipokok']+$rows['jlhbonus'];
if ($tot_gaji >= 4500000) {
$tot_gaji = $tot_gaji+0.05*$tot_gaji;
} elseif ($tot_gaji>=5000000) {
$tot_gaji = $tot_gaji+0.15*$tot_gaji;
}
echo number_format($tot_gaji,0,".",",");
Other than not assigning your adjusted figure your second condition may not be met. i.e. a number such as 6000000 is greater than 4500000, it is also greater than 5000000, under your logic the first condition is met.
You likely want to switch your conditions or test for a range.
<?php
function adjustment($num)
{
if($num > 10) {
$num += 0.25 * $num;
} else if ($num > 5) {
$num += 0.5 * $num;
}
return $num;
}
foreach([1,6,12] as $num) {
echo adjustment($num), "\n";
}
Output:
1
9
15

do-while loop only runs once

I am trying to make a simple while loop using a class to get the factorial of a number. However, for some reason, the while loop is only returning the value after it has run once.
Here is my code:
<?php
class Factorial {
public function calculate($int){
$intStep = $int-1;
do {
$int*=$intStep;
$int--;
return $int;
} while($int>0);
if (!is_int($int)){
echo "entry is not a number";
}
}
}
$factorial = new Factorial;
echo $factorial->calculate(5);
I see a number of problems with your code:
return $int; is run inside the do while loop, which means it'll only ever run once.
You're decrementing $int instead of $intStep
You're checking if $int is below zero instead of $intStep
Here's your code with all of these problems fixed, it works and returns 15:
class Factorial {
public function calculate ($int) {
if (!is_int($int)) {
echo "entry is not a number";
}
$intStep = $int - 1;
do {
$int += $intStep;
$intStep--;
} while ($intStep > 0);
return $int;
}
}
$factorial = new Factorial;
echo $factorial->calculate(5);
3v4l.org demo
You shouldn't return from your function until your result is ready. Since you return early, you won't finish your calculation.
In general, your life will be easier if you learn how to debug. For PHP, the easiest way would be to echo stuff throughout your code. If you put echo $int inside of your loop it would be more obvious to you what the issue was.
try this
<?php
class Factorial {
public function calculate($num){
$Factorial = 1;
$i =1;
do{
$Factorial *= $i;
$i++;
}while($i<=$num);
return $Factorial;
}
}
$factorial = new Factorial;
echo $factorial->calculate(5);
?>
Factorial ? Maybe the following code is what you want:
Don't forget negative Numbers.
class Factorial {
public function calculate ($int) {
if (!is_int($int)) {
echo "entry is not a number";
}
if ($int == 0 || $int == 1) {
$result = 1;
} else if ($int < 0) {
$result = $this->calculate($int + 1) * $int;
} else {
$result = $this->calculate($int - 1) * $int;
}
return $result;
}
}
$factorial = new Factorial;
echo $factorial->calculate(-4);

php First value of array not displaying

I have the following bit of code and for some reason in the first WHILE loop the first value of $actor_list (when $i = 0) does not display. If I simply echo $actor_list[0] then it displays fine, but in the loop it will not display. I merely get [td][/td] as the output. The remaining values of the array display fine.
Also the line
echo '</tr><tr> </br>';
is not displaying.
What am I missing here? The value of $num_actors is an even number in my test scenario so there doesn't seem to be a reason for the above echo line to be skipped.
$actor_list = explode(" ", $actors);
$num_actors = count($actor_list);
if ($num_actors <= 6) {
foreach ($actor_list as $actor) {
echo '[td]'.$actor.'[/td] </br>';
}
} elseif ($num_actors <= 12) {
if ($num_actors % 2 == 0) {
$half_actors = $num_actors / 2;
while ($i <= ($half_actors - 1)) {
echo '[td]'.$actor_list[$i].'[/td] </br>';
$i++;
}
echo '</tr><tr> </br>';
while ($i <= $num_actors) {
echo '[td]'.$actor_list[$i].'[/td] </br>';
$i++;
}
}
}
You're not initialising the variable $i to 0, which means it will be set to 'null' and thus not reference the 0th index of the array; but when it's then incremented its value will become 1.
Note: [..] Decrementing NULL values has no effect too, but incrementing them results in 1.
See: http://php.net/manual/en/language.operators.increment.php
Try adding:
$i = 0;
before the if statement.
Made a few changes.
This is working for me.
Try it out.
<?php
$actor_list = 'act1 act2 act3 act4 act5 act6';
$actors = explode(" ", $actor_list);
$num_actors = count($actor_list);
//debug
//echo "<pre>";
//print_r($actor_list);
//echo "</pre>";
echo "<table>";
if ($num_actors <= 6) {
foreach ($actors as $actor) {
echo '<tr><td>'.$actor.'</td></tr>';
}
} elseif ($num_actors <= 12) {
if ($num_actors % 2 == 0) {
$half_actors = $num_actors / 2;
while ($i <= ($half_actors - 1)) {
echo '<tr><td>'.$actor_list[$i].'</td></tr>';
$i++;
}
while ($i <= $num_actors) {
echo '<tr><td>'.$actor_list[$i].'</td></tr>';
$i++;
}
}
}
echo "</table>";
?>

Count elements in a loop

I'm trying to count elements in a loop to break each number of elements and show in groups
My little script
$data="house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*";
$exp_filter = explode("*", trim($data));
for ($x = 0; $x <= count($exp_filter); $x++)
{
print "".$exp_filter[$x]."";
if ($x%5 == 0)
{
print "<br>";
}
}
As you can see in the little script each 5 rounds I want show the tag for break and show as in groups of elements.
The problem it´s always show in the first line one element and after this the rest, and no works fine.
The index of $exp_filter starts at 0, so this block of code
if ($x % 5 == 0)
{
print "<br>";
}
should be
if (($x+1) % 5 == 0)
{
print "<br>";
}
Here's the complete modified code
$data = "house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*";
$exp_filter = explode("*", trim($data));
for ($x = 0; $x <= count($exp_filter); $x++)
{
print "".$exp_filter[$x]."";
if (($x + 1) % 5 == 0)
{
print "<br>";
}
}
Working example: http://codepad.org/iEsKK98M
$data="house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*";
$exp_filter=explode("*",trim($data));
for($x=1;$x<=count($exp_filter);$x++)
{
print "".$exp_filter[$x]."";
if($x%5==0)
{
print "<br>";
}
}
Try this. The problem is you started at 0 in for, you should start from 1 ;)
Quickfix:
Demo
<?php
$data="house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*";
$exp_filter=explode("*",trim($data));
for($x=1;$x<=count($exp_filter);$x++)
{
print "".$exp_filter[$x]."";
if($x > 0 && $x%5==0)
{
echo "<br />";
}
}
?>
I would use array chunk and implode instead:
$data="house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*house*";
foreach(array_chunk(explode('*', $data), 5) as $chunk){
echo implode(' ', $chunk) . '<br>';
}
Live example: http://codepad.viper-7.com/ED2wHR

Problem with displaying rows of 3 in php

So basically I have a script that is suppose to display my code into rows of 3, and create a new row. However, it is all displayed in one column. Here's the code.
while($mysql2 = mysql_fetch_array($sql2)) {
$prodlogo = $mysql2['logo'];
$prodid = $mysql2['id'];
$prodname = $mysql2['name'];
$x = 1;
if($x==1) {
echo "<tr>";
}
echo "<td><img src=images/productpics/$prodlogo></br><a href=viewproduct.php?pid=$prodid>$prodname</a></td>";
$x++;
if($x==3) {
echo "</tr>";
$x = 1;
}
}
move the $x out of the loop,otherwise you are reset it to one in every loop
each <td> will be a separate column. I would suggest you format it thus.
while($mysql2 = mysql_fetch_assoc($sql2)) {
foreach($mysql2 as $row) {
echo "<tr>";
echo "<td>".$row['logo']."</td><td>$".row['id']."</td><td>".$row['name']."</td>";
echo "</tr>";
}
}
also you need to use mysql_fetch_assoc() to get an associative array you can access like $row['key']
You may need to instantiate $x = 1 outside of your while loop - you're resetting it to 1 each time you're running it.
Using modulo might be good too:
if (($x % 3) == 0) {
echo "<tr>"
}
echo "<td><img src=images/productpics/$prodlogo></br><a href=viewproduct.php?pid=$prodid>$prodname</a></td>";
if (($x % 3) == 0) {
echo "</tr>"
}
You wouldn't need to worry about resetting $x to 1...

Categories