tobimichigan Posted April 15, 2010 Share Posted April 15, 2010 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 https://forums.phpfreaks.com/topic/198637-array-and-grade-output/ Share on other sites More sharing options...
andrewgauger Posted April 15, 2010 Share Posted April 15, 2010 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 https://forums.phpfreaks.com/topic/198637-array-and-grade-output/#findComment-1042478 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.