試圖建立一個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