我有我的网站的一部分,我可以给成员颁奖。现在我正在为每个奖项创建单独的描述页面。在该页面上,我想包括哪些成员已经获得此奖项,并将这些数据显示在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