[英]Multidimensional PHP array from a MySQLi table to HTML table
我在網站的一部分中可以獎勵會員。 現在,我正在為每個獎項創建單獨的描述頁面。 在該頁面上,我想列出哪些成員已經獲得了該獎項,並在HTML表格中顯示此數據。
我已經將數據傳遞到多維數組中,如下所示。
<?php
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$currentAward = $awardid; // Current Page Award. Local value to the page.
$result = $conn->query("SELECT * FROM vms_awardsgranted WHERE awardid='$currentAward'");
$awardsList = array();
while($pilotsList = $result->fetch_assoc()){
$awardsList[ $pilotsList["id"] ] = $pilotsList;
}
echo nl2br("DEBUG: $currentAward \n");
print_r(array_values($awardsList));
$conn->close();
?>
結果示例
DEBUG: 8
Array ( [0] => Array ( [id] => 28 [awardid] => 8 [pilotid] => 4 [dateissued] => 2015-10-14 20:12:21 ) [1] => Array ( [id] => 32 [awardid] => 8 [pilotid] => 1 [dateissued] => 2015-10-14 20:14:14 ) )
從這里開始,我試圖解析此信息並將其添加到下面的HTML表中,但是老實說,我找不到使用多維數組執行此操作的正確方法。 外面有人可以給我一些見識嗎? 我的HTML表格如下所示。
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="ocean_table">
<thead>
<tr>
<th>Pilot ID</th>
<th>Pilot Name</th>
<th>Date Awarded</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center">
</td>
<td align="center">
</td>
<td align="center">
</td>
</tr>
</tbody>
</table>
就像是
<tbody>
<?php foreach($awardsList as $record){ ?>
<tr>
<td align="center">
<?php echo $record['id']; ?>
</td>
<td align="center">
<?php echo $record['awardid']; ?>
</td>
<td align="center">
<?php echo $record['pilotid']; ?>
</td>
</tr>
<?php } ?>
</tbody>
基本上,要遍歷結果,您的代碼應如下所示:
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="ocean_table">
<thead>
<tr>
<th>Pilot ID</th>
<th>Pilot Name</th>
<th>Date Awarded</th>
</tr>
</thead>
<tbody>
<?php foreach($awardsList as $member):?>
<tr>
<td align="center">
<?=$pilot['pilotid']?>
</td>
<td align="center">
</td>
<td align="center">
<?=$pilot['dateissued']?>
</td>
</tr>
<?php endforeach;?>
</tbody>
</table>
不過,有幾點評論。
$pilotList
建議飛行員列表,但該變量僅包含獎勵和一名飛行員之間的參考/交界處。 Dito for awardsList
實際上包含了獲獎者的名單。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.