我是 php 的新手程序员,我一直在寻找这个问题的解决方案。如果有人有想法请。发表您的答案,我感谢您解决这些问题的好主意。
在我的数据库表中,我有这样的数据:
在我的 php 页面中,我想使用 html 表以这种方式呈现。
谁能帮我做这件事?非常感谢……</p>
Try this:
$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>';
I think you are searching for the functionality of GROUP_CONCAT
in mysql (docs). You would get something like:
SELECT name, GROUP_CONCAT( week ), GROUP_CONCAT( there )
FROM presence
GROUP BY name;
which will return the following results you can parse using explode()
in php (docs):
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
On a side note: If you haven't settled on a database scheme yet, it might be better to use an int for the week number column, as it is more reliable when sorting and easier to manipulate.
Sqlfiddle: http://sqlfiddle.com/#!2/fc785/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);
}
}