仅从 PHP 文件获取新数据 - jQuery

发布于 2024-11-28 19:14:34 字数 1469 浏览 1 评论 0原文

我只想加载新数据,而不是从使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡莣 2024-12-05 19:14:34

我建议您修改选择查询。不要将其设置为 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.

眼中杀气 2024-12-05 19:14:34

事实上,根据您当前的设置,每次调用“getUsers”都会重播游戏的整个状态,直到最后一步,并且不一定以正确的状态完成。为了获得您想要的行为,您需要将时间戳或序列 ID 添加到“用户”表中,然后对结果进行排序以获得最新的移动。您需要分别为每个用户执行此操作,因此您还需要提供一种机制来检索和迭代所有用户...

因此您的 javascript 变得类似于...

function getUsers() {
    $.get("database/getAllUsers.php", function(data) {
        $.each(data, function(i,uid) {
            $.get("database/getData.php?uid=".uid, function(data) {
                ...

而您的 php 查询变成...

$query = "SELECT * FROM users WHERE uid=$_GET['uid'] ORDER BY timestamp DESC LIMIT 1

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

function getUsers() {
    $.get("database/getAllUsers.php", function(data) {
        $.each(data, function(i,uid) {
            $.get("database/getData.php?uid=".uid, function(data) {
                ...

and your php query becomes...

$query = "SELECT * FROM users WHERE uid=$_GET['uid'] ORDER BY timestamp DESC LIMIT 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文