将数据添加到html表

我有一个从数据库中获取数据的html表。将数据添加到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_idlongname有问题。这是我现在的输出结果。

系统从表中的用户名取姓并打印在清单

回答:

中所有用户的问题是,你不把$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>";

}

以上是 将数据添加到html表 的全部内容, 来源链接: utcz.com/qa/261486.html

回到顶部