显示表头中的行

时间:2012-07-26 08:10:49

标签: php mysql

我有一张像

这样的表格
id       course       activities   

1     Microbiology      Quiz1
2     Microbiology      Quiz2
3     Microbiology      Quiz3
4     Microbiology      Quiz4

我需要在php文件中显示为表头

Quiz1   Quiz2   Quiz3    Quiz3  Quiz4

这是我的代码

<?php
$sql = mysql_query('SELECT * FROM activity');
while($row = mysql_fetch($sql))
{
    $activities $row['activities'];
}
echo '<table>
       <tr>
        <th>'.$activities.'</th>
       </tr>
     </table>';
?>

1 个答案:

答案 0 :(得分:1)

您只需重新组织代码:

<?php
$sql = mysql_query('SELECT * FROM activity');

echo '<table><tr>';
while($row = mysql_fetch_array($sql))
{
    echo '<th>'.$row['activities'].'</th>';
}
echo '</tr>';

//your data rows here

echo '</table>';
?>