试图建立一个html表格,这将是一个玩家被允许建立在游戏中的建筑物。还应该有一列显示玩家已经拥有的人数。在同一个表中显示两个数组的值?
显示表格的代码以及您可以构建的所有建筑物的代码,但我无法获得它显示您已拥有的建筑物的数量。
代码现在有点乱,现在有一些注释查询,但我让他们在那里显示我已经试图让他们正确显示。
EDIT(用C维尔茨代码):
// Fetch buildings
$result = $database->query("SELECT id, name, description, cost, power_use FROM buildings;");
$buildings = array();
while($building = $database->fetch($result)) {
$buildings[$building['id']] = $building;
}
// Buildings Owned
$buildings_owned = $database->query("SELECT building_id, count(*) as n FROM player_buildings WHERE owner_id = '$user_id'");
$buildings = array();
while($owned = $database->fetch($buildings_owned)) {
$owned_buildings[$owned['id']] = $owned;
}
$playerBuildings = array();
while($owned = $database->fetch($buildings_owned)) {
$playerBuildings[$owned['buildings_id']] = $owned['n'];
}
// Display form
echo "For every 10 building you construct, it will also cost you 1 turn!";
echo "<table style='width:900px;'>
<tr>
<th style='border: solid black 1px;width:40% text-align:left;'>Name</th>
<th style='border: solid black 1px;width:50%;'>Description</th>
<th style='border: solid black 1px;width:5%;'>Price</th>
<th style='border: solid black 1px;width:5%;'>Power Usage</th>
<th style='border: solid black 1px; width:5%;'>Buildins Owned</th>
<th style='width:5%;'> </th>
</tr>";
foreach ($buildings as $building) {
$bid = $building['id'];
$building['player_count'] = isset($playerBuildings[$bid])
? $playerBuildings[$bid]
: 0;
echo "<tr>
<td style='border: solid black 1px;'>{$building['name']}</td>
<td style='border: solid black 1px;'>{$building['description']}</td>
<td style='border: solid black 1px;'>{$building['cost']}</td>
<td style='border: solid black 1px;'>{$building['power_use']}</td>
<td style='border: solid black 1px;'>{$owned['amount']} </td>
<td>
<form action='$self_link' method='POST'>
<input type='hidden' name='building_id' value='$id' />
<input style='width:40px' type='number' name='amount' value='amount' />
<input type='submit' name='build' value='Build' />
</form>
</td>
</tr>";
}
echo "</table>";
你可以使用连接查询来做到这一点,如果你想分开查询然后使用一些数组连接函数,如:array_combine(),array_merge()。 –
我尝试使用联接查询,但我没有得到它的工作。然后没有显示任何值。不知道我是否正确地呼吁他们.. – Naxor