2016-09-28 2 views
0

私は何かのために働いているもののためのものの 'マップ'種類があります。それぞれのタイルを引き出し、データベースと照合して、誰かがそのタイルに位置しているかどうかを確認します。PHPはfor()ループでmysqlの結果を取得します

コードは動作しますが、dbの最初の結果に対してのみ有効です。

誰でも手助けできます。どうもありがとう。

$sqlw = "SELECT id, player_coord_x, player_coord_y FROM player_game WHERE  world_id='$world'"; 
$world_result = $player_stat->query($sqlw); 


?> 

<div class='map-grid'> 

    <? 
    $id = ''; 
    $size = 16; 
    for($i = 1; $i <= $size; $i++) { 
     echo "<div class='map-grid-row'>"; 
     for ($j=1; $j <= $size; $j++) { 

      // check for player at location 
      if ($world_result->num_rows > 0) { 
       while($w_row = $world_result->fetch_assoc()) { 

        $player_coord_x = $w_row['player_coord_x']; 
        $player_coord_y = $w_row['player_coord_y']; 
        $id = $w_row['id']; 

       } 
      } 

      if ($player_coord_x == $i and $player_coord_y == $j){ 

       echo "<div class='map-grid-cell high'>"; 
       echo "XXX"; 
       echo "</div>"; 
      }else{ 

       echo "<div class='map-grid-cell high'>"; 
       echo "<span class=\"map-small\">(x-$i y-$j)</span>"; 
       echo "</div>"; 
      } 

     } 
     echo "</div>"; 
    } 
    ?> 

</div> 
+3

配列ではなく変数を使用。 '$ player_coord_x'は最後のレコードしか持っていません。 '$ player_coord_x []'を使ってください。 – chris85

+3

... 'fetch_assoc'ループ内のすべての処理を行います。 – chris85

+0

私はあなたの情報に基づいていくつかのことを試しましたが、うまくいけばいいですか、もっと私に例を挙げてください。 – Mystic

答えて

0
$sqlw = "SELECT id, player_coord_x, player_coord_y FROM player_game WHERE world_id='$world'"; 
$world_result = $player_stat->query($sqlw); 

if($world_result->num_rows > 0){ 
    while($w_row = $world_result->fetch_assoc()){ 
    //first type of array 
    $player[] = array(
     'x'  => $w_row['player_coord_x'], //your player X record 
     'y'  => $w_row['player_coord_y'], //your player Y record 
     'id'  => $w_row['id'] //your player id 
    ); 

    //second type of array 
    // OR store player's X and Y as key of array 
    $player[$w_row['player_coord_x'] . '-' . $w_row['player_coord_y']] = $w_row['id']; 
    // This are only if the player record will never repeat 
    } 
} 

?> 

<div class="map-grid"> 
    <?php 
    $size = 16; 
    for($i = 1; $i <= $size; $i++){ 
     echo '<div class="map-grid-row">'; 
     for($j = 1; $j <= $size; $j++){ 
     //with first type of array 
     $exists = false; 
     foreach($player as $play){ 
      if($play['x'] == $i && $play['y'] == $j){ 
      $exists = true; 
      break; // break the loop once you got the result. 
      } 
     } 
     if($exists){ //Player exists? if no detect in foreach loop above, default exists would return false. 
      echo '<div class="map-grid-cell high">'; 
      echo 'XXX'; 
      echo '</div>'; 
     } else { 
      echo '<div class="map-grid-cell high">'; 
      echo '<span class="map-small">(x-' . $i . ' y-' . $j . ')</span>'; 
      echo '</div>'; 
     } 



     //second type of array 
     //Check if the key exists and if it's empty. 
     if(isset($player[$i.'-'.$j]) && !empty($player[$i.'-'.$j])){ 
      echo '<div class="map-grid-cell high">'; 
      echo 'XXX'; 
      echo '</div>'; 
     } else { 
      echo '<div class="map-grid-cell high">'; 
      echo '<span class="map-small">(x-' . $i . ' y-' . $j . ')</span>'; 
      echo '</div>'; 
     } 
     } 
    } 
    ?> 
</div> 
+0

あなたのコードをありがとう。私はそれを試して、グリッドはまだ正常に描画されますが、私は今私のデータベースから何の結果も得られません。 – Mystic

+0

実際に私の間違い、私はデータベースへの接続を確立するときにタイプミスをしました。コードが本当に助けてくれてありがとう – Mystic

関連する問題