我有我的网站的一部分,我可以给会员颁奖。现在我正在为每个奖项创建个人描述页面。在这个页面上,我想包括哪些成员已经获得了这个奖项,并将这些数据显示在一个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>
基本上,要循环遍历结果,您的代码可以像这样:
<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
表示一个飞行员列表,但该变量只包含奖励和一个飞行员之间的参考/连接。awardsList
的Dito实际上包含了获奖人员的名单。
类似
<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>