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?