我桌上有很多学生有不同的课程。我想打印一次不同的课程并打印该班级的学生人数。我可以打印课程。但无法打印每个班级的学生人数。我怎样才能做到这一点?例如:
In my database: Desired Output:
Class Name Class Num. of Students
2 John 1 2
2 Snow 2 3
3 Jara 3 1
1 Peter
1 Nira
2 Jerin
这是我的代码:
$q = "SELECT * FROM ipsc_student group by class";
$rs = mysql_query($q);
$numOfRows=mysql_num_rows($rs);
$rt = "";
$sl = 0;
while($row=mysql_fetch_assoc($rs)){
$sl++;
$rt.="<tr>";
$rt.="<td>$sl</td>";
$rt.="<td>".$row['class']."</td>";
$rt.="<td>$numOfRows</td>";
$rt.="<td></td>";
$rt.="<td></td>";
$rt.="<td></td>";
$rt.="<td></td>";
$rt.="<td></td>";
$rt.="<td></td>";
$rt.="</tr>";
}
echo $rt;
答案 0 :(得分:1)
试试这个
$q = "SELECT class, count(*) as NUM FROM ipsc_student GROUP BY class";
$rs = mysql_query($q);
$numOfRows=mysql_num_rows($rs);
$rt = "";
$sl = 0;
while($row=mysql_fetch_assoc($rs)){
$sl++;
$rt.="<tr>";
$rt.="<td>$sl</td>";
$rt.="<td>".$row['class']."</td>";
$rt.="<td>".$row['NUM']."</td>";
$rt.="<td></td>";
$rt.="<td></td>";
$rt.="<td></td>";
$rt.="<td></td>";
$rt.="<td></td>";
$rt.="<td></td>";
$rt.="</tr>";
}
echo $rt;