Jump to content

Outputing Student's Grade Issue...


tobimichigan

Recommended Posts

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++;

}
?>



Link to comment
Share on other sites

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++;

}
?>

Link to comment
Share on other sites

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

?>

Link to comment
Share on other sites

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? 

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.