weixin_33724659 2017-11-23 20:21 采纳率: 0%
浏览 303

For循环导致页面崩溃

Index.php

<?php
    require_once("inc/db.php");
    require_once("inc/map.php");

    $db = new Database();
    $map = new Map($_POST['x'], $_POST['y']);
    $map->createMap();
    $map->battle();
    $map->move();
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/style.css"/>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript">
</script>
</head>
<body>

</body>
</html>

Db.php

<?php
    class Database{
        private $config;
        protected $sql;

        public function __construct(){
            $this->Connect();
        }

        public function Connect(){
            $this->config  = parse_ini_file("config.ini");
            $con = $this->config;
            $this->sql = new PDO("mysql:host={$con['host']}; dbname={$con['dbname']}",
            $con['username'],$con['password']);
            $this->sql->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        }
    }
?>

Map.php

<?php 
    class Map extends Database{

      public $x;
      public $y;
      public $width = 5;
      public $height = 5;

      public function __construct($x,$y){
            $this->x = $x;
            $this->y = $y;
      }

      public function createMap(){
          for($x=0; $x < $this->width; $x++){
              for($y=0; $y < $this->height; $y++){
                  Database::Connect();
                  echo "<div id='map'>";
                  echo "<div class='map_columns' title='{$x}_{$y}' data-x='{$x}' data-y='{$y}' id='{$x}_{$y}'>";
                  $this->spawnPlayers($x,$y);
                  echo "</div>";
              }
              echo "<div class=\"break\"></div>";
          }
      }

      public function spawnPlayers($x,$y){
          $stmt = $this->sql->prepare('SELECT * FROM user WHERE x = "'.$x.'" AND y = "'.$y.'"');
          $stmt->execute();
          while($rows = $stmt->fetch(PDO::FETCH_ASSOC)){
              echo "<img title='{$rows['name']} 
 {$rows['x']}|{$rows['y']}'>";
          }
      }

      public function battle(){
          $stmt = $this->sql->prepare("SELECT * FROM user GROUP BY x,y HAVING count(*) > 1");
          $stmt->execute();
          $rows = $stmt->fetch(PDO::FETCH_ASSOC);
          if($rows){
              echo "<input type='submit' id='battle-btn' value='Fight'>";
          } else{
          }
       }

       public function move(){
         if(isset($this->x) && isset($this->y)){
            $stmt = $this->sql->prepare("UPDATE user SET x = '$this->x', y = '$this->y' WHERE name = 'Wiz'");
            $stmt->execute();
         }
       }
    }
?>

Index.js

$(document).ready(function(){
    $(document).on("click", ".map_columns", function(){
        var x = $(this).attr("data-x");
        var y = $(this).attr("data-y");
        $.post('./index.php', {x: x, y: y}, function(){

        });
    });

    setInterval(function(){
        $("#map").load("./index.php");
    }, 1000);
});

So I made a map using for loops and the data being outputted is the user coordinates. Wherever I press on the map, it will update the users coordinates to that location. This works as intended but I have an issue. I need the map to auto refresh / update when a users coordinates have been updated.

So my approach was to use .load() in ajax as I use in most projects but the difference here is the for loop. I'm assuming because it reinterates the for loop a second time, it causes everything to crash. How can I solve this problem? I know games have done the same but I can't think of a counter-solution. The code written is pretty understandable.

I re-wrote the original version so it's clean and understandable for stackoverflow, the original looks as below:

https://i.gyazo.com/a34793a0af0344b867f0537830da5cd3.mp4

It's being auto updated using the html which isn't viable. So please how can I update the div with new information(players axis) without the page crashing?

  • 写回答

1条回答 默认 最新

  • weixin_33674976 2017-11-23 20:35
    关注

    The problem is that you're loading the entire page into the #map DIV, but you just want to replace its own contents. Change it to:

        $.get("index.php", function(response) {
            $("#map").replaceWith($(response).find("#map"));
        });
    

    It would probably be better to create a new script that just returns what you want, instead of using index.php and then ignore all the other contents.

    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办