我有一个从数据库中获取数据的html表。
这是我的SQL查询:
// 1.Get data
// data for final table
// format is [username][projectNo] => [process1, process2, ..., processN]
$result = [];
// map project no to its title
$projectNoToTitle = [];
$sql = '
SELECT uid, username, staff_id, longname
FROM `user`
ORDER BY username
';
$query = mysqli_query($conn, $sql);
// for each user
while ($data = mysqli_fetch_assoc($query)) {
$sql2 = '
SELECT a.* FROM
(
(
-- select pairs project - leader
SELECT p.projectNo, p.title, CONCAT(upr.process, (upr.role) ) AS process
FROM project p
LEFT JOIN user_project upr ON p.projectNo = upr.projectNo
AND upr.username = "' . mysqli_real_escape_string($conn, $data['username']) . '"
)
) AS a
ORDER BY a.projectNo
';
$query2 = mysqli_query($conn, $sql2);
// for each project => process pair of user
while ($data2 = mysqli_fetch_assoc($query2)) {
$staff_id = $data['staff_id'];
$longname = $data['longname'];
$username = $data['username'];
$projectNo = $data2['projectNo'];
$projectTitle = $data2['title'];
$process = $data2['process'];
$projectNoToTitle[$projectNo] = $projectTitle;
if (!isset($result[$username])) {
$result[$username] = [];
}
if (!isset($result[$username][$projectNo])) {
$result[$username][$projectNo] = [];
}
if ($process) {
$result[$username][$projectNo][] = $process;
}
}
}
然后我想在表格中水平和垂直打印数据:
<table style="background-color:rgb(238, 238, 238)" id="dataTable4" class="tablesorter" class="tblD" border="0" cellpadding="0" cellspacing="1">
<?php
// 2. Output table
// create table header
// it's columns should contain all projects
if ($result) {
$header ='<th>Staff ID</th>
<th>Full Name</th>
<th>Username</th>' .
array_reduce(array_values($projectNoToTitle), function ($p, $n) {
return $p . '<th>Project ' . htmlspecialchars($n) . '</th>';
});
// output body
$body = '';
foreach ($result as $username => $usernameData) {
$row = '<td>' . htmlspecialchars($longname) . '</td>' . '<td>' . htmlspecialchars($staff_id) . '</td>' . '<td>' . htmlspecialchars($username) . '</td>';
foreach ($projectNoToTitle as $projectNo => $projectTitle) {
$r = isset($usernameData[$projectNo])
? implode(', ', $usernameData[$projectNo])
: 'N/A';
$row .= '<td>' . htmlspecialchars($r) . '</td>';
}
$body .= "<tr>$row</tr>";
}
echo "<thead>$header</thead><tbody>$body</tbody>";
}// \2. Output table
?>
我可以打印用户名,但staff_id
和longname
出现问题。这是我现在的输出。
系统从表用户名中获取姓氏,并为列表中的每个用户名打印
答案 0 :(得分:1)
问题是您没有将$longname
和$staff_id
放入$results
数组中。打印表时,只需使用这些变量,其中包含数据库中最后一个用户的值。
将处理数据库结果的循环更改为:
while ($data2 = mysqli_fetch_assoc($query2)) {
$staff_id = $data['staff_id'];
$longname = $data['longname'];
$username = $data['username'];
$projectNo = $data2['projectNo'];
$projectTitle = $data2['title'];
$process = $data2['process'];
$projectNoToTitle[$projectNo] = $projectTitle;
if (!isset($result[$username])) {
$result[$username] = [ 'longname' => $longname, 'staff_id' => $staff_id, 'projects' => []];
}
if (!isset($result[$username]['projects'][$projectNo])) {
$result[$username]['projects'][$projectNo] = [];
}
if ($process) {
$result[$username]['projects'][$projectNo][] = $process;
}
}
然后构建表的代码应为:
foreach ($result as $username => $usernameData) {
$row = '<td>' . htmlspecialchars($usernameData['longname']) . '</td>' . '<td>' . htmlspecialchars($usernameData['staff_id']) . '</td>' . '<td>' . htmlspecialchars($username) . '</td>';
foreach ($projectNoToTitle as $projectNo => $projectTitle) {
$r = isset($usernameData['projects'][$projectNo])
? implode(', ', $usernameData['projects'][$projectNo])
: 'N/A';
$row .= '<td>' . htmlspecialchars($r) . '</td>';
}
$body .= "<tr>$row</tr>";
}