想象一下,你有這個 SQL 查詢結果:
ID ID_KEY VALUE
1 1 Text1.1
2 1 Text1.2
3 1 Text1.3
4 2 Text2.1
5 2 Text2.2
6 2 Text2.3
7 3 Text3.1
8 3 Text3.2
9 3 Text3.3
并且您想列印一個考慮ID_KEY的表格,如下所示:
ID_KEY VALUE1 VALUE2 VALUE3
1 Text1.1 Text1.2 Text1.3
2 Text2.1 Text2.2 Text2.3
3 Text3.1 Text3.2 Text2.3
我怎樣才能做到這一點?我想在 ID_KEY 更改時列印一個新行。例如,現在我有這個代碼:
$result = $con->query($sql);
if ($result->num_rows > 0) {
$res = "<table>";
$res .= "<tr>";
$res .= "<th>ID_KEY</th>";
$res .= "<th>VALUE1</th>";
$res .= "<th>VALUE2</th>";
$res .= "<th>VALUE3</th>";
$res .= "</tr>";
while ($row=mysqli_fetch_assoc($result)) {
$res .= "<tr>";
$res .= "<td>" . $row['ID_KEY'] . "</td>";
$res .= "<td>" . $row['VALUE1'] . "</td>";
$res .= "<td>" . $row['VALUE2'] . "</td>";
$res .= "<td>" . $row['VALUE3'] . "</td>";
$res .= "</tr>";
}
$res .= "</table>";
return $res;
}
此代碼不起作用,因為“value1”、“value2”和“value3”是我的表中不存在的欄位。
相反,如果我這樣說:
$res .= "<tr>";
$res .= "<td>" . $row['ID_KEY'] . "</td>";
$res .= "<td>" . $row['VALUE'] . "</td>";
$res .= "<td>" . $row['VALUE'] . "</td>";
$res .= "<td>" . $row['VALUE'] . "</td>";
$res .= "</tr>";
這也不起作用,因為“VALUE”的值將重復 3 次。
是否可以這樣做,或者我應該重組資料庫以另一種方式存盤資訊?
uj5u.com熱心網友回復:
下面的代碼可以解決問題,但它取決于資料庫的內容和查詢。確保每個總是有 3 個值,ID_KEY并且結果正確排序。這個想法是你重復輸出具有值的單元格,直到ID_KEY發生變化。許多細微的變化是可能的,但最終它們都遵循這個原則。
$result = $con->query($sql);
if ($result->num_rows > 0) {
$res = "<table>";
$res .= "<tr>";
$res .= "<th>ID_KEY</th>";
$res .= "<th>VALUE1</th>";
$res .= "<th>VALUE2</th>";
$res .= "<th>VALUE3</th>";
$res .= "</tr>";
$row = mysqli_fetch_assoc($result);
while ($row) {
$idKey = $row['ID_KEY'];
$res .= "<tr>";
$res .= "<td>" . $idKey . "</td>";
$res .= "<td>" . $row['VALUE'] . "</td>";
while (($row = mysqli_fetch_assoc($result)) &&
($idKey == $row['ID_KEY'])) {
$res .= "<td>" . $row['VALUE'] . "</td>";
}
$res .= "</tr>";
}
$res .= "</table>";
return $res;
}
uj5u.com熱心網友回復:
不是最優雅的,但您可以先對每個集合進行分組,然后回圈遍歷分組資料以進行輸出。但是,使用 SQL 以這種方式對資料進行分組可能會更有效。
<?php
// Original Data
$originalData = [
[
'ID' => 1,
'ID_KEY' => 1,
'VALUE' => "Text1.1"
],
[
'ID' => 2,
'ID_KEY' => 1,
'VALUE' => "Text1.2"
],
[
'ID' => 3,
'ID_KEY' => 1,
'VALUE' => "Text1.3"
],
[
'ID' => 4,
'ID_KEY' => 2,
'VALUE' => "Text2.1"
],
[
'ID' => 5,
'ID_KEY' => 2,
'VALUE' => "Text2.2"
],
[
'ID' => 6,
'ID_KEY' => 2,
'VALUE' => "Text2.3"
],
[
'ID' => 7,
'ID_KEY' => 3,
'VALUE' => "Text3.1"
],
[
'ID' => 8,
'ID_KEY' => 3,
'VALUE' => "Text3.2"
],
[
'ID' => 9,
'ID_KEY' => 3,
'VALUE' => "Text3.3"
],
];
// Group the Data by ID_KEY
$groupedData = [];
foreach($originalData as $item) {
$groupedData[$item['ID_KEY']][] = $item['VALUE'];
}
// HTML Output
$output = "<table>";
$output .= "<tr>";
$output .= "<th>ID_KEY</th>";
$output .= "<th>VALUE1</th>";
$output .= "<th>VALUE2</th>";
$output .= "<th>VALUE3</th>";
$output .= "</tr>";
// Output each row
foreach($groupedData as $idKey => $groupData) {
$output .= "<tr>";
$output .= "<td>" . $idKey . "</td>";
// Iterrate of each value
foreach($groupData as $value) {
$output .= "<td>" . $value . "</td>";
}
$output .= "</tr>";
}
$output .= "</table>";
// Output the HTML
'<pre>' . print_r($output, true) . '</pre>';
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/486729.html
標籤:php mysql sql 循环 phpmyadmin
