PHP Looping results

Tags: php sql for-loop
By : Mystic
Source: Stackoverflow.com
Question!

I have a 'map' kind of thing for something im working on. Im drawing each tile out and then checking against a database to see if somebody is located at that tile.

The code works but only for the first result in the db.

Can anyone help. many thanks.

$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>
By : Mystic


Answers
$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>
By : Zeff Tang


I believe you are getting your axis mixed up. The width of node2 is 20, but you are only moving node1 (width of 10) by 10. Try moving it by 20 (or 23 for a small gap).



The key word that you're apparently missing is "epsilon." If you search on Python epsilon value then it returns a links to StackOverflow: Value for epsilon in Python near the top of the results in Google.

Obviously it helps if you understand how the term is associated with this concept.



This video can help you solving your question :)
By: admin