tobimichigan Posted April 20, 2010 Share Posted April 20, 2010 Hi Code Gurus, Please my target is to display "A","B",C","D", OR "E" depending on what score exists in the database. But the code below seems to be displaying just A,B,C,D,E respectively without cognisance of the score limit. Please what do I do to make it do this? <?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> </tr>"); echo("<tr> <th scope='row'>$adno</th>"); if ($datalist >=$grade) { echo("<td>$grade[0]</td>");} if ($datalist >=70) { echo("<td>$grade[1]</td>");} if ($datalist >=60) { echo("<td>$grade[2]</td>");} if ($datalist >=50) { echo("<td>$grade[3]</td>");} if ($datalist >=40) { echo("<td>$grade[4]</td>");} if ($datalist <=39) { echo("<td>$grade[5]</td>");} echo("</tr>"); echo("</table>"); $a++; } ?> Quote Link to comment Share on other sites More sharing options...
conker87 Posted April 20, 2010 Share Posted April 20, 2010 You need an else if or make them inclusive: <?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> </tr>"); echo("<tr> <th scope='row'>$adno</th>"); if ($datalist >= $grade) { echo("<td>$grade[0]</td>");} if ($datalist >=70 && $datalist < 80) { echo("<td>$grade[1]</td>");} if ($datalist >=60 && $datalist < 70) { echo("<td>$grade[2]</td>");} if ($datalist >=50 && $datalist < 60) { echo("<td>$grade[3]</td>");} if ($datalist >=40 && $datalist < 50) { echo("<td>$grade[4]</td>");} if ($datalist <=39) { echo("<td>$grade[5]</td>");} echo("</tr>"); echo("</table>"); $a++; } ?> Quote Link to comment Share on other sites More sharing options...
TeddyKiller Posted April 20, 2010 Share Posted April 20, 2010 The code can be tidied alot. Though I don't think I've got a fix.. I'm not sure how you display your results. The way I see it.. only 1 person can have 1 adno number and the same number cannot be duplicated for another person. So I've made it like this.. based on that. <?php $grade = array("A","B","C","D","E","F"); $adno = $_GET['adno']; $select = mysql_query("select * from acadinfo where adno='$adno'") or die('<p>Error Retrieving<br/>'.'Error: ' .mysql_error() . '</p>'); echo "<table width='548' border='1'> <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> </tr> <tr><th scope='row'>$adno</th>"; $datalist = mysql_fetch_array($select); $datalist = htmlspecialchars($datalist); foreach($datalist as $data) { if (int($data) >= 80) { echo "<td>$grade[0]</td>"; } else { if (int($data) >= 70 && int($data) < 80) { echo "<td>$grade[1]</td>"; } else { if (int($data) >= 60 && int($data) < 70) { echo "<td>$grade[2]</td>"; } else { if (int($data) >= 50 && int($data) < 60) { echo "<td>$grade[3]</td>"; } else { if (int($data) >= 40 && int($data) < 50) { echo "<td>$grade[4]</td>"; } else { if (int($data) <= 39 && int($data) < 40) { echo "<td>$grade[5]</td>"; } } } } } } } echo "</tr></table>"; ?> Quote Link to comment Share on other sites More sharing options...
tobimichigan Posted April 20, 2010 Author Share Posted April 20, 2010 The code can be tidied alot. Though I don't think I've got a fix.. I'm not sure how you display your results. The way I see it.. only 1 person can have 1 adno number and the same number cannot be duplicated for another person. So I've made it like this.. based on that. <?php $grade = array("A","B","C","D","E","F"); $adno = $_GET['adno']; $select = mysql_query("select * from acadinfo where adno='$adno'") or die('<p>Error Retrieving<br/>'.'Error: ' .mysql_error() . '</p>'); echo "<table width='548' border='1'> <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> </tr> <tr><th scope='row'>$adno</th>"; $datalist = mysql_fetch_array($select); $datalist = htmlspecialchars($datalist); foreach($datalist as $data) { if (int($data) >= 80) { echo "<td>$grade[0]</td>"; } else { if (int($data) >= 70 && int($data) < 80) { echo "<td>$grade[1]</td>"; } else { if (int($data) >= 60 && int($data) < 70) { echo "<td>$grade[2]</td>"; } else { if (int($data) >= 50 && int($data) < 60) { echo "<td>$grade[3]</td>"; } else { if (int($data) >= 40 && int($data) < 50) { echo "<td>$grade[4]</td>"; } else { if (int($data) <= 39 && int($data) < 40) { echo "<td>$grade[5]</td>"; } } } } } } } echo "</tr></table>"; ?> Excellent try Ted...I think I'm almost there... I implemeted all your corrections as follows: <?php $grade = array("A","B","C","D","E","F"); $adno = $_GET['adno']; //$grade[0]=A; //$grade[1]=B; //$grade[2]=C; //$grade[3]=D; //$grade[4]=E; //$grade[5]=F; $adno=$_GET['adno']; $select = mysql_query("select * from acadinfo where adno='$adno'") or die('<p>Error Retrieving<br/>'.'Error: ' .mysql_error() . '</p>'); echo "<table width='548' border='1'> <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> </tr> <tr><th scope='row'>$adno</th>"; $datalist = mysql_fetch_array($select); $datalist = htmlspecialchars($datalist); foreach($datalist as $data){ if (int($data) >= 80) { echo "<td>$grade[0]</td>"; } else { if (int($data) >= 70 && int($data) < 80) { echo "<td>$grade[1]</td>"; } else { if (int($data) >= 60 && int($data) < 70) { echo "<td>$grade[2]</td>"; }else { if (int($data) >= 50 && int($data) < 60) { echo "<td>$grade[3]</td>"; } else { if (int($data) >= 40 && int($data) < 50) { echo "<td>$grade[4]</td>"; } else { if (int($data) <= 39 && int($data) < 40) { echo "<td>$grade[5]</td>"; } } } } } } } echo "</tr></table>"; ?> In the output, I'm getting this weird error: Warning: htmlspecialchars() expects parameter 1 to be string, array given in C:\xampp\htdocs\Ekeoma\details.php on line 132 Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\Ekeoma\details.php on line 134 where line 132=$datalist = htmlspecialchars($datalist); and line 134=foreach($datalist as $data){ Please what is wrong now? Quote Link to comment Share on other sites More sharing options...
TeddyKiller Posted April 20, 2010 Share Posted April 20, 2010 $datalist = htmlspecialchars($datalist); Remove that line.. if any errors occur because of htmlspecialchars, it can be added in another place later. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.