在同一个表中显示两个数组的值?

时间:2016-02-11 09:34:19

标签: php mysqli

尝试建立一个html表格,这将是一个允许玩家在游戏中构建的建筑物。还应该有一个列显示玩家已经拥有的数量。

显示表格的代码以及您可以构建的所有建筑物都可以使用,但是我无法显示您已经拥有的建筑物的数量。

现在有一些评论的查询,代码很乱,但我把它们留在那里,以显示我已经尝试过让它们正确显示的内容。

编辑(使用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%;'>&nbsp;</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>";

3 个答案:

答案 0 :(得分:1)

第一个查询类似于SELECT id, name, etc FROM buildings;到PHP数组$buildings

第二个可能就像SELECT buildings_id, count(*) as n FROM player_buildings WHERE owner_id = :user_id;。然后订购PHP数组:

$playerBuildings = [];
while($owned = $database->fetch($buildings_owned)) {
   $playerBuildings[$owned['buildings_id']] = $owned['n'];
}

在视图中,只需循环$buildings并检查是否有亲戚$playerBuildings

foreach ($buildings as $building) {
   $bid = $building['id'];
   $building['player_count'] = isset($playerBuildings[$bid])
       ? $playerBuildings[$bid]
       : 0;
   // render something
}

答案 1 :(得分:0)

SELECT count(*) as owned_buildings FROM player_buildings WHERE owner_id = '$user_id'

答案 2 :(得分:0)

类似this之类的内容,除非您替换列名并使用user_id加入参数。