我有我的網站的一部分,我可以給成員頒獎。現在我正在爲每個獎項創建單獨的描述頁面。在該頁面上,我想包括哪些成員已經獲得此獎項,並將這些數據顯示在HTML表格中。從MySQLi表格到HTML表格的多維PHP數組
我已經將數據傳遞到多維數組中,如下所示。
<?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>
是的,我的變量名稱現在真的如此拼湊在一起。我已經迷失在我的頁面代碼中,並開始混合變量..謝謝指出。我現在會清理乾淨的。 **編輯**感謝注意到我的表中缺少飛行員姓名。 –
這工作完美。但是,不要只是複製你的代碼,你能告訴我到底發生了什麼,所以我可以學習嗎?我看到你將每行數組數據傳遞給$ pilot,但是實際發生了什麼?你是否將每一行定義爲$ pilot,然後你是通過<?= $ pilot ['pilotid']?>調用並在該行中查找pilotid來查詢每一行? –
呃是的。很簡單,代碼包含一個遍歷'$ awardsList'的foreach循環。在每次迭代中,下一個項目在變量'$ pilot'中可用(因爲'awardsList'實際上包含了人員,就像我之前提到的那樣)。因此,該代碼只是爲該數組中的每個項目生成一個表格行。然後'<?= $ pilot ['pilotid']?>'。 '<?='只是'<?php echo'的縮寫,所以代碼只會迴應'$ pilot'中的值。除此之外,它只是你的代碼未修改。 @JulioSoares的答案顯示了相同的解決方案,僅爲循環和回聲使用稍微不同的記法。 – GolezTrol