在 PHP 中存储 JOIN 值并将它们显示为 table

Storing JOIN Values in PHP and showing them as a table

我有以下 SQL 查询:

SELECT tbltasks.fldTaskNr
    ,tbltasks.fldRITMNr
    ,tbltasks.fldCHGNr
    ,tblci.fldCI
    ,tblgxp.fldGxP
    ,tblrequester.fldRequester
    ,tblstatus.fldStatus
    ,tbltasks.fldDescription
    ,tblresponsible.fldResponsible
    ,tbllocation.fldLocation
FROM tbltasks
LEFT JOIN tblCI ON tblci.pkCI = tbltasks.fkCI
LEFT JOIN tblgxp ON tblgxp.pkGxP = tbltasks.fkGxP
LEFT JOIN tblrequester ON tblrequester.pkRequester = tbltasks.fkRequester
LEFT JOIN tblstatus ON tblstatus.pkStatus = tbltasks.fkStatus
LEFT JOIN tblresponsible ON tblresponsible.pkResponsible = tbltasks.fkResponsible
LEFT JOIN tbllocation ON tbllocation.pkLocation = tbltasks.fkLocation

输出:

现在我想存储此查询的值并将它们输出为 table,应该如下所示:

我得到以下 table 作为 HTML,输出应该显示在此 table 中并重复自身,因为数据库中将有多个条目:

                    <th>Task Nr.</th>
                    <th>RITM Nr.</th>
                    <th>CHG Nr.</th>
                    <th>CI</th>
                    <th>GxP</th>
                    <th>Task Requester</th>
                    <th>Task Status</th>
                    <th>Description</th>
                    <th>Responsible</th>
                    <th style="width: 201px;">Location</th>
                </tr>

我该怎么做?感谢您的帮助!

您可以使用 foearch 打印您拉入 html 的数据。

foreach ($data as $key => $val) {
    <tr>
       <td>
          <?php echo $val ?>
       </td>
    </tr>

}

你可以通过将上面的代码应用到你自己的table和数据中得到你想要的结果。

这很容易,但过程有点长。 首先确保为查询列提供一个名称值,如下所示:

SELECT tbltasks.fldTaskNr AS "TASK_NR"
,tbltasks.fldRITMNr AS "RITMNR"
,tbltasks.fldCHGNr AS "CHGNR"
,tblci.fldCI AS "FLDCI"
,tblgxp.fldGxP AS "GXP"
,tblrequester.fldRequester AS "REQUESTER"
,tblstatus.fldStatus AS "FLD_STATUS"
,tbltasks.fldDescription AS "FLD_DESCRIPTION"
,tblresponsible.fldResponsible AS "RESPONSIBLE"
,tbllocation.fldLocation AS "LOCATION" FROM tbltasks .. (rest of your joints)

然后使用此代码将您的查询转换为对象数组

$data = array();
while ($row = mysqli_fetch_assoc($result)) {
    $data[] = $row; 
} 
return $data;

完成此步骤后,我们可以继续您的 HTML Table 部分

<tr>
<th>Task Nr.</th>
<th>RITM Nr.</th>
<th>CHG Nr.</th>
<th>CI</th>
<th>GxP</th>
<th>Task Requester</th>
<th>Task Status</th>
<th>Description</th>
<th>Responsible</th>
<th style="width: 201px;">Location</th>
</tr>

<?php
for($i = 0 ;$i < count($data); $i++){
echo '<tr>';
echo '<td>'.$data['TASK_NR'].'</td>';
echo '<td>'.$data['RITMNR'].'</td>';
echo '<td>'.$data['CHGNR'].'</td>';
echo '<td>'.$data['FLDCI'].'</td>';
echo '<td>'.$data['GXP'].'</td>';
echo '<td>'.$data['REQUESTER'].'</td>';
echo '<td>'.$data['FLD_STATUS'].'</td>';
echo '<td>'.$data['FLD_DESSCRIPTION'].'</td>';
echo '<td>'.$data['RESPONSIBLE'].'</td>';
echo '<td style="width: 201px;">'.$data['LOCATION'].'</td>';
echo '</tr>';
}
?>

希望这能解决您的问题,并确保您的文件扩展名为“.php”