您好,我是這些問題的初學者。
昨天我熟悉了諸如 sql 的 group_concat() 函式之類的東西。我有名為“科目”和“成績”的表格。在科目中存盤“id、name、subject_type”,在成績中存盤“id、grade、subject_type”。
因此,如果科目得到 subject_type = 1,它將顯示該型別的所有成績。
我已經創建了這樣的代碼:
<?php
$sqltest1 = "SELECT s.id AS id, s.name AS name, group_concat(g.grade SEPARATOR ',') as grades, s.teacher_1 as teacher_1
FROM subjects s
INNER JOIN grades g ON (g.subject_type = s.subject_type)
GROUP BY s.id";
?>
<table class="table table-bordered">
<thead>
<tr>
<th class="col-1">#</th>
<th class="col-3 text-center">Subject</th>
<th class="col-6 text-center">Grade</th>
<th class="col-1 text-center">Options</th>
</tr>
</thead>
<tbody>
<!-- ###################################################################### -->
<?php
$result = $conn->query($sqltest1);
if ($result->num_rows > 0) {
$i = 1;
// output data of each row
while($row = $result->fetch_assoc()) {
//$id = $row['id'];
echo "<tr>";
echo "<td scope='row'>". $i ."</td>";
echo "<td>". $row['name'] ."</td>";
echo "<td><span class='bg-primary'>". $row["grades"] ."</span></td>";
echo "<td class='text-center'>". $row['teacher_1']."</td>";
echo "</tr>";
$i ;
}
} else {
echo "";
}
?>
<!-- ###################################################################### -->
</tbody>
</table>
所以我的問題是我不能在一欄中檢索單獨的成績。我想獲得給定型別的所有成績,但要顯示為單獨的成績。
uj5u.com熱心網友回復:
while 應該是這樣的(檢查 while 中的分隔符)您通過將值拆分為用于分隔它的每個字符來獲得一個陣列:逗號。然后你foreach所有元素并在里面寫一個span。
// output data of each row
while($row = $result->fetch_assoc()) {
//$id = $row['id'];
echo "<tr>";
echo "<td scope='row'>". $i ."</td>";
echo "<td>". $row['name'] ."</td>";
echo "<td>";
$arrayGrades = explode(",", $row["grades"]);
foreach ($arrayGrades as $grade) {
echo "<span class='bg-primary'>". $grade ."</span>";
}
echo "</td>";
echo "<td class='text-center'>". $row['teacher_1']."</td>";
echo "</tr>";
$i ;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/370093.html
上一篇:PHPPUT方法Slim框架
下一篇:使用2個特定鍵檢查值是否在陣列中
