【问题标题】:PHP get results from mysql in a for() loopPHP 在 for() 循环中从 mysql 获取结果
【发布时间】:2017-02-07 00:51:03
【问题描述】:

我有一个'地图'之类的东西我正在做的事情。我画出每个图块,然后检查数据库以查看是否有人位于该图块处。

代码有效,但仅适用于数据库中的第一个结果。

谁能帮忙。非常感谢。

$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>

【问题讨论】:

  • 使用数组,而不是变量。 $player_coord_x 只会有最后一条记录。使用$player_coord_x[]
  • ... 或在fetch_assoc 循环内进行所有处理。
  • 我根据你的信息尝试了一些方法,但我可以让它工作,你介意给我更多的例子吗
  • 你在用for 循环什么?
  • 我正在创建一个 div 网格,当前为 16x16,每个 div 在我要检查数据库的每个 div 上都有一个值,即 $i 和 $j(或我的数据库中的 x 和 y)对于任何包含相同值的记录,因此如果 $i 和 x 的值匹配并且 $j 和 y 的值匹配,那么我只是简单地回显一个 XX 进行测试。我希望你能理解这一点

标签: php mysql sql for-loop


【解决方案1】:
$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>

【讨论】:

  • 感谢您的代码。我已经尝试过了,网格仍然可以成功绘制但是我现在从我的数据库中没有得到任何结果。
  • 实际上是我的错误,我在建立与数据库的连接时打错了字。非常感谢你的代码真的帮助了我
猜你喜欢
  • 1970-01-01
  • 2013-04-20
  • 1970-01-01
  • 2015-09-18
  • 1970-01-01
  • 1970-01-01
  • 2013-01-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多