仅从 PHP 文件获取新数据 - jQuery
我只想加载新数据,而不是从使用 JavaScript 使用 JSON 解析的 PHP 文件中重新加载旧数据。我的项目是一个小型多人游戏环境,我的目标是让所有用户实时看到其他移动。
这是我的代码:
JavaScript:
function getUsers() {
$.get("database/getData.php", function(data) {
// data returned from server;
for(var i = 0; i < data.response.length; i++) {
// users obtained from the array... parse them now
var user = data.response[i].split(" ");
// user info
var id = user[0];
var username = user[1];
var xx = user[2];
var yy = user[3];
userCount = data.response.length;
$(".online").html(userCount + " users online right now.")
// position the users
moveCharacter(username, xx, yy);
}
}, "json");
}
var infoGetter = setInterval(getUsers, 2000); // we may need to improve this... type of realtime
这是我的 PHP:
<?
$database = sqlite_open("thenew.db", 0999, $error);
if(!$database) die($error);
$query = "SELECT * FROM users";
$results = sqlite_query($database, $query);
if(!$results) die("Canot execute query");
$data = array();
while($row = sqlite_fetch_array($results)) {
$data[] = $row['uid'] . " " . $row['username'] . " " . $row['xPos'] . " " . $row['yPos'];
}
echo json_encode(array("response"=>$data));
sqlite_close($database);
?>
如何才能使 JavaScript 仅解析 PHP 文件中的新数据而不是所有数据?
谢谢!
Instead of reloading old data from a PHP file that I parse with JavaScript using JSON, I want to ONLY load new data. My project is a little multiplayer environment and my goal is to have all users see either other move in realtime.
Here is my code:
JavaScript:
function getUsers() {
$.get("database/getData.php", function(data) {
// data returned from server;
for(var i = 0; i < data.response.length; i++) {
// users obtained from the array... parse them now
var user = data.response[i].split(" ");
// user info
var id = user[0];
var username = user[1];
var xx = user[2];
var yy = user[3];
userCount = data.response.length;
$(".online").html(userCount + " users online right now.")
// position the users
moveCharacter(username, xx, yy);
}
}, "json");
}
var infoGetter = setInterval(getUsers, 2000); // we may need to improve this... type of realtime
And here is my PHP:
<?
$database = sqlite_open("thenew.db", 0999, $error);
if(!$database) die($error);
$query = "SELECT * FROM users";
$results = sqlite_query($database, $query);
if(!$results) die("Canot execute query");
$data = array();
while($row = sqlite_fetch_array($results)) {
$data[] = $row['uid'] . " " . $row['username'] . " " . $row['xPos'] . " " . $row['yPos'];
}
echo json_encode(array("response"=>$data));
sqlite_close($database);
?>
How can I make it so ONLY new data from the PHP file is parsed by the JavaScript instead of all data?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议您修改选择查询。不要将其设置为 select * from users,而是添加一些 where 条件,例如 where userId > >某个数字。现在发送我们在上一个请求中处理的最后一个 userId 以及您从客户端发出的下一个请求。这将确保您始终得到新的回复。
I would suggest you to modify the select query. Instead of having it as select * from users, add some where condition like where userId > somenumber. Now send the last userId which we processed in the last request along with next request which you make from client side. This will make sure that you will always gets new response.
事实上,根据您当前的设置,每次调用“getUsers”都会重播游戏的整个状态,直到最后一步,并且不一定以正确的状态完成。为了获得您想要的行为,您需要将时间戳或序列 ID 添加到“用户”表中,然后对结果进行排序以获得最新的移动。您需要分别为每个用户执行此操作,因此您还需要提供一种机制来检索和迭代所有用户...
因此您的 javascript 变得类似于...
而您的 php 查询变成...
Indeed, with your current setup, every call to "getUsers" will replay the entire state of the game up to the last move made, and not necessarily finish in the correct state. In order to get the behavior you want, you will need to add a timestamp or serial id to the "users" table and then order your results in order to get the most recent moves. You will need to do this for each user separately, so you will also need to provide a mechanism to retrieve and iterate through all users...
Thus your javascript becomes something like...
and your php query becomes...