【问题标题】:For loop causes page to crashFor循环导致页面崩溃
【发布时间】:2017-11-23 20:21:43
【问题描述】:

索引.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>

数据库.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);
        }
    }
?>

地图.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']} \n {$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();
         }
       }
    }
?>

索引.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);
});

所以我使用 for 循环制作了一张地图,输出的数据是用户坐标。无论我在地图上按什么位置,它都会将用户坐标更新到该位置。这按预期工作,但我有一个问题。当用户坐标更新时,我需要地图自动刷新/更新。

所以我的方法是在 ajax 中使用 .load() ,就像我在大多数项目中使用的那样,但这里的区别在于 for 循环。我假设因为它第二次重新迭代 for 循环,它会导致一切崩溃。我怎么解决这个问题?我知道游戏也做了同样的事情,但我想不出一个反解决方案。写的代码很好理解。

我重写了原来的版本,所以它对stackoverflow来说干净易懂,原来的样子如下:

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

它正在使用不可行的 html 自动更新。那么请问如何在页面不崩溃的情况下使用新信息(玩家轴)更新 div?

【问题讨论】:

  • 为什么要为循环的每一次迭代都创建一个新的数据库连接?当它是实例方法时,您正在静态调用连接
  • @MarkBaker 这不是故意的,但我需要在某处启动数据库
  • 是的,启动一次,在循环之前使用$this-&gt;Connect(),甚至在构造函数中
  • 问题在于您将整个页面加载到一个 DIV 中,而不仅仅是替换该 DIV 的内容。
  • $db = new Database(); 在你的index.php 是一个毫无意义的附加数据库连接。

标签: javascript php ajax loops


【解决方案1】:

问题是您将整个页面加载到#map DIV 中,但您只想替换它自己的内容。将其更改为:

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

创建一个只返回所需内容的新脚本可能会更好,而不是使用index.php 然后忽略所有其他内容。

【讨论】:

  • 我很感激这个答案,你知道我是怎么做到的,所以两个人的战斗按钮会同时出现。目前第一个去其他玩家的人会先看到“战斗”。我需要让它在之后加载?如果没有,那很好;我会找到解决办法的
  • 在不同客户端之间同步脚本很复杂,我不知道你会怎么做。
猜你喜欢
  • 2014-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-19
  • 1970-01-01
相关资源
最近更新 更多