PHP在for()循环中从mysql获取结果

时间:2016-09-28 21:09:05

标签: php mysql sql for-loop

我有一个'地图'的东西,我正在努力的东西。我将每个图块绘制出来然后检查数据库以查看是否有人位于该图块上。

代码可以工作,但仅适用于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>

1 个答案:

答案 0 :(得分: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>