[英]Display values of two arrays in the same table?
Trying to build a html table, wich will be displying the buildings a player is allowed build in a game. 试图建立一个html表,这将显示允许玩家在游戏中建造的建筑物。 And there should also be a column that displays how many the player already own.
并且还应该有一栏显示玩家已经拥有的人数。
The code to display the table works aswell as all the buildings you can build, but i cant get it to display how many of what building you already own. 显示表格的代码与您可以建造的所有建筑物一样有效,但是我无法显示您已经拥有的建筑物的数量。
Code is abit messy now with some out commented querys, but i kept them in there to show what i have already tried to get them to display correctly. 现在代码有些混乱,带有一些注释掉的查询,但是我将它们保留在其中以显示我已经尝试使它们正确显示的内容。
EDIT(With C Würtz code): 编辑(带有CWürtz代码):
// 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>";
The first query is like SELECT id, name, etc FROM buildings;
第一个查询类似于
SELECT id, name, etc FROM buildings;
to PHP array $buildings
. 到PHP array
$buildings
。
The second could be like SELECT buildings_id, count(*) as n FROM player_buildings WHERE owner_id = :user_id;
第二个就像
SELECT buildings_id, count(*) as n FROM player_buildings WHERE owner_id = :user_id;
. 。 Then order the PHP array:
然后订购PHP数组:
$playerBuildings = [];
while($owned = $database->fetch($buildings_owned)) {
$playerBuildings[$owned['buildings_id']] = $owned['n'];
}
In the view, just loop on $buildings
and check if there is a relative $playerBuildings
. 在视图中,只需循环
$buildings
并检查是否存在相对的$playerBuildings
。
foreach ($buildings as $building) {
$bid = $building['id'];
$building['player_count'] = isset($playerBuildings[$bid])
? $playerBuildings[$bid]
: 0;
// render something
}
SELECT count(*) as owned_buildings FROM player_buildings WHERE owner_id = '$user_id'
除了您替换列名并使用user_id连接参数外,类似这样的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.