将数据显示到html表中

时间:2013-09-26 03:47:25

标签: php html mysql

我是php的新手程序员,我一直在寻找这个问题的解决方案。如果有人有想法请。发布你的答案,我很感谢你的好主意来解决这个问题。

在我的数据库表中,我有这样的数据:

tblattendance

在我的php页面中,我希望使用html表以这种方式呈现。 Html Table as output

有人可以帮我做这些事吗?非常感谢你......

3 个答案:

答案 0 :(得分:0)

试试这个:

$SQL = "select NAME, WEEK, STATUS from tblattendance order by NAME, SUBSTRING(WEEK,1,LENGTH(WEEK) - 2) ASC";

$data = $db->query($SQL);

$last_name = "";
echo '<table><tr><th>NAME</th><th>1st WEEK</th><th>2nd WEEK</th><th>3rd WEEK</th><th>4th WEEK</th>';
while($row = $data->fetch_assoc()){
    if($last_name != $row["NAME"]){
        $last_name = $row["NAME"];
        echo '</tr>';
        echo '<tr>';
        echo '<td>'.$row["NAME"].'</td>';
    }
    echo '<td>'.$row["STATUS"].'</td>';
}
echo '</tr></table>';

答案 1 :(得分:0)

我认为您正在mysql(docs)中搜索GROUP_CONCAT的功能。你会得到类似的东西:

SELECT name, GROUP_CONCAT( week ), GROUP_CONCAT( there )
FROM presence
GROUP BY name;

将返回以下结果,您可以使用php(docs)中的explode()进行解析:

Andrew  4th,1st,3rd,2nd     Present,Present,Present,Present
John    1st,4th,3rd,2nd     Absent,Present,Present,Present
Mark    2nd,3rd,1st,4th     Present,Present,Present,Present
Micheal 2nd,3rd,4th,1st     Absent,Absent,Absent,Present

旁注:如果你尚未确定数据库方案,那么对于周数列使用int可能会更好,因为它在排序时更可靠并且更易于操作。

Sqlfiddle:http://sqlfiddle.com/#!2/fc785/1

答案 2 :(得分:-1)

试试这个:

$sql="select distinct name from tblattendance;";
$res=$mysqli->query($sql);
if($res){
   while($row=$res->fetch_assoc()){
       $name=$row['name'];
       $sql="select week, status from tblattendance where name='$name';";
       $res1=$mysqli->query($sql);
   }
}