【发布时间】:2013-09-26 15:16:23
【问题描述】:
我是 php 的新手程序员,我一直在寻找这个问题的解决方案。如果有人有想法请。发表你的答案,我很感谢你解决这些问题的好主意。
在我的数据库表中,我有这样的数据:
在我的 php 页面中,我想使用 html 表格以这种方式呈现。
谁能帮我做这件事?非常感谢……
【问题讨论】:
我是 php 的新手程序员,我一直在寻找这个问题的解决方案。如果有人有想法请。发表你的答案,我很感谢你解决这些问题的好主意。
在我的数据库表中,我有这样的数据:
在我的 php 页面中,我想使用 html 表格以这种方式呈现。
谁能帮我做这件事?非常感谢……
【问题讨论】:
我认为您正在 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
【讨论】:
试试这个:
$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>';
【讨论】:
ORDER BY week ASC 会将2th、3th 和30th 排序为2th、30th、3th,因为它们按字符串排序。
SUBSTRING,它将不返回任何内容(必须是 1)。它仍然有同样的问题,因为它仍然比较两个字符串。为了解决这个问题,请改用LPAD( WEEK, 10, "0" )。 0003th 小于 0030th,因为字符 3 具有比 0 更高的字符值,并且它比较相同长度的字符串,其中 3th 和 30th 以相反的方式排序,因为 @987654338 @ 的值高于0(并且4 中的4 比30 中的3 '更多')。
试试这个:
$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);
}
}
【讨论】: