请帮助我,我正在制作在线学生信息,它有很多学生,我不知道如何根据他们的 StudID (学生证)来分离学生的成绩)。
我的代码是这样的......
function Content()
{
$code = <<< htmlcode
<center>
<form name="frmGrade" method="POST" action="" style="padding-top:30px;">
<table border="1" align="center">
<tr>
<td align="center">Subject</td>
<td align="center">First Grading</td>
<td align="center">Second Grading</td>
<td align="center">Third Grading</td>
<td align="center">Fourth Grading</td>
<td align="center">Average</td>
<td align="center">Remarks</td>
</tr>
</center>
htmlcode;
//-------------------------------------
// retrieve data from database
//-------------------------------------
$sql = "SELECT Subject,FirstGrading,SecondGrading,ThirdGrading,FourthGrading,Average,Remarks FROM grades WHERE StudID = '002' ;";
$result = mysql_query($sql);
$num_rows = mysql_num_rows($result);
//-------------------------------------
// fetch each record from database
//-------------------------------------
if($num_rows > 0)
{
for($ctr = 1; $ctr <= $num_rows; $ctr++)
{
$row = mysql_fetch_array($result);
$sub = $row['Subject'];
$fg = $row['FirstGrading'];
$secg = $row['SecondGrading'];
$thg = $row['ThirdGrading'];
$fourthg = $row['FourthGrading'];
$ave = $row['Average'];
$remarks = $row['Remarks'];
$code .= <<< htmlcode
<tr $bg>
<td style="padding:5;"> $sub </td>
<td style="padding:5;"> $fg </td>
<td style="padding:5;"> $secg </td>
<td style="padding:5;"> $thg </td>
<td style="padding:5;"> $fourthg </td>
<td style="padding:5;"> $ave </td>
<td style="padding:5;"> $remarks </td>
$actions
</tr>
htmlcode;
}
//-------------------------------------
// end table
//-------------------------------------
$code .= "</table> </form></center> </br></br>";
}
//-------------------------------------
// Display table
//-------------------------------------
return $code;
}
?>
我想要的是,当学生一个StudID = 001时,他/她将会看到他/她的成绩,当学生二的StudID = 002时,他们只会看到他的成绩。
答案 0 :(得分:0)
从$result
而不是while loop
for loop
获取行
function Content($sid)
{
$code = <<< htmlcode
<center>
<form name="frmGrade" method="POST" action="" style="padding-top:30px;">
<table border="1" align="center">
<tr>
<td align="center">Subject</td>
<td align="center">First Grading</td>
<td align="center">Second Grading</td>
<td align="center">Third Grading</td>
<td align="center">Fourth Grading</td>
<td align="center">Average</td>
<td align="center">Remarks</td>
</tr>
</center>
htmlcode;
//-------------------------------------
// retrieve data from database
//-------------------------------------
$sql = "SELECT Subject,FirstGrading,SecondGrading,ThirdGrading,FourthGrading,Average,Remarks FROM grades WHERE StudID = '$sid' ;";
$result = mysql_query($sql);
$num_rows = mysql_num_rows($result);
//-------------------------------------
// fetch each record from database
//-------------------------------------
if($num_rows > 0)
{
while($row = mysql_fetch_array($result))
{
$sub = $row['Subject'];
$fg = $row['FirstGrading'];
$secg = $row['SecondGrading'];
$thg = $row['ThirdGrading'];
$fourthg = $row['FourthGrading'];
$ave = $row['Average'];
$remarks = $row['Remarks'];
$code .= <<< htmlcode
<tr $bg>
<td style="padding:5;"> $sub </td>
<td style="padding:5;"> $fg </td>
<td style="padding:5;"> $secg </td>
<td style="padding:5;"> $thg </td>
<td style="padding:5;"> $fourthg </td>
<td style="padding:5;"> $ave </td>
<td style="padding:5;"> $remarks </td>
$actions
</tr>
htmlcode;
}
//-------------------------------------
// end table
//-------------------------------------
$code .= "</table> </form></center> </br></br>";
}
//-------------------------------------
// Display table
//-------------------------------------
return $code;
}
?>
<?php
//to get content for student 001
echo Content('001');
//to get content for student 002
echo Content('002');
?>