2016-02-11 36 views
0

试图建立一个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%;'>&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>"; 
+0

你可以使用连接查询来做到这一点,如果你想分开查询然后使用一些数组连接函数,如:array_combine(),array_merge()。 –

+0

我尝试使用联接查询,但我没有得到它的工作。然后没有显示任何值。不知道我是否正确地呼吁他们.. – Naxor

回答

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 
} 
+0

我试着添加这个最好的我可以,而我没有得到任何错误表是empy .. – Naxor

+0

您提取两次查询第二。删除'$ buildings_owned = $ database-> query(...);'后的4行 –

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

解释一下你的答案 – davejal

0

有点像this除了你可以替换你的列名和用user_id加入参数。

相关问题