Jump to content

Array and Grade Output


tobimichigan

Recommended Posts

Fellow Coders,

Please is there a way I could assign a value to an exisitng numeric array type in php? I am trying to output "A" if the math score is >=80, B if >=70 etc this is to alternate for 7 subjects. But so far I'm only geting the results for Math please kindly help...

 

<?php
						$grade=array("A","B","C","D","E","F");

						$adno=$_GET['adno'];
	 $select=mysql_query("select * from acadinfo where adno='$adno'");
			  $num=mysql_num_rows($select);
			  $a=1;
			  if(!@select) {
			  	die('<p>Error Retrieving<br/>'.
				'Error: ' .mysql_error() . '</p>');}
				while ($datalist=mysql_fetch_array($select)){
					$adno=htmlspecialchars($datalist["adno"]);
					$math=htmlspecialchars($datalist["math"]);
					$eng=htmlspecialchars($datalist["eng"]);
					$physics=htmlspecialchars($datalist["physics"]);
					$chem=htmlspecialchars($datalist["chem"]);
					$biology=htmlspecialchars($datalist["biology"]);
					$economics=htmlspecialchars($datalist["economics"]);
					$yoruba=htmlspecialchars($datalist["yoruba"]);

echo ("<table width='548' border='1'>");
echo("<tr>
    <th width='26' scope='col'>ADMISSION NO.</th>
    <th width='26' scope='col'>MATHEMATICS</th>
    <th width='26' scope='col'>ENGLISH</th>
    <th width='26' scope='col'>PHYSICS</th>
    <th width='26' scope='col'>CHEMISTRY</th>
    <th width='365' scope='col'>BIOLOGY</th>
    <th width='365' scope='col'>ECONOMICS</th>
    <th width='365' scope='col'>YORUBA</th>

  </tr>");
for each ($grade as ($g)){
  echo("<tr>
    <th scope='row'>$adno</th>");
if ($datalist >=$g) {
    echo("<td>$grade[0]</td>");}
elseif ($datalist >=70) {
echo("<td>$grade[1]</td>");}
elseif ($datalist >=60) {
echo("<td>$grade[2]</td>");}
elseif ($datalist >=50) {
echo("<td>$grade[3]</td>");}
elseif ($datalist >=40) {
echo("<td>$grade[4]</td>");}
elseif ($datalist <=39) {
echo("<td>$grade[5]</td>");}
echo("</tr>");
echo("</table>");
$a++;
}
}
?>

Link to comment
Share on other sites

for each ($grade as ($g)){

(by the way foreach is one word)

is iterating A, B, C, D, E and assigning $g those values.

 

so when you if ($datalist >= $g)

you are comparing an array to a single character string.

 

What I think you are trying to do is:

 

  echo("<tr>

    <th scope='row'>$adno</th>");

foreach ($datalist as $key => $arg)

{

echo "<td>";

switch ($arg){

case ($arg >= 80 ):

echo $grade[0];

break;

case ($arg >= 70 ):

echo $grade[1];

break;

case ($arg >= 60 ):

echo $grade[2];

break;

case ($arg >= 50 ):

echo $grade[3];

break;

case ($arg >= 40 ):

echo $grade[4];

break;

case default:

echo $grade[5];

break;

}

echo "</td>";

}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.