私は php の初心者プログラマーであり、常にこの問題の解決策を探しています。誰かがアイデアを持っているなら、pls. あなたの答えを投稿してください。この問題を解決するための素晴らしいアイデアに感謝します。
私のデータベーステーブルには、次のようなデータがあります。
私のphpページでは、htmlテーブルを使用してこのように表示したいと考えています。
誰かが私がこのことをするのを手伝ってくれますか? どうもありがとうございました…</p>
これを試して:
$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>';
GROUP_CONCAT
mysql(docs )の機能を探していると思います。次のようなものが得られます。
SELECT name, GROUP_CONCAT( week ), GROUP_CONCAT( there )
FROM presence
GROUP BY name;
explode()
これは、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
余談ですが、データベース スキームをまだ決定していない場合は、週番号の列に int を使用することをお勧めします。
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);
}
}