gpt4 book ai didi

javascript - PHP 表自动更新数据但不更新表

转载 作者:行者123 更新时间:2023-11-30 22:25:04 25 4
gpt4 key购买 nike

我有一个游戏服务器,它具有聊天功能并将所有玩家的聊天消息记录到我的数据库中。我正在尝试创建一个自动更新数据但不更新表格本身的表格,这是因为在我的表格上我想要每个玩家的 Action 下拉列表(从服务器踢玩家,禁止玩家,静音玩家,打耳光玩家等.) 但目前我的 JavaScript 代码每 5 秒刷新一次整个表格。因此,如果我打开我的下拉列表,当表格刷新时它将关闭下拉列表,因为这个问题,目前我已经将下拉列表更改为一个按钮。

这是我的代码:

显示表格的索引页:

<?php require 'session.php';
require 'header.php'; ?>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">// <![CDATA[
$(document).ready(function() {
$.ajaxSetup({ cache: false }); // This part addresses an IE bug. without it, IE will only load the first number and will never refresh
setInterval(function() {
$('#results').load('includes/online.php');
}, 3000); // refresh rate in milliseconds.
});
// ]]></script>
<div id="results">Loading data ...</div>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">// <![CDATA[
$(document).ready(function() {
$.ajaxSetup({ cache: false }); // This part addresses an IE bug. without it, IE will only load the first number and will never refresh
setInterval(function() {
$('#results2').load('includes/chatlog.php');
}, 3000); // refresh rate in milliseconds.
});
// ]]></script>
<div id="results2">Loading data ...</div>
<?php 
include 'database.php';

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT *,current_clients.CID AS con_id FROM current_clients INNER JOIN groups ON current_clients.Level = groups.level Order By Team DESC, Score DESC";
$result = $conn->query($sql);
while( $row = mysql_fetch_array($result));

if ($result->num_rows > 0) {
echo "<div id=left>";
echo "<table class=table align=center><tr><th>ID</th><th>Name</th><th>Rank</th><th>Score</th><th>IP</th><th>Action</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
$id=$row['con_id'];
$ip=$row['IP'];
$team=$row['Team'];
// $team = str_replace("3","<tr bgcolor=midnightblue>",$team);
// $team = str_replace("2","<tr bgcolor=darkred>",$team);
// $team = str_replace("1","<tr bgcolor=grey>",$team);
$name=$row['ColorName'];
$group=$row['name'];
$name=htmlentities($name);
$name = str_replace("^0","</font><font color=black>",$name);
$name = str_replace("^1","</font><font color=red>",$name);
$name = str_replace("^2","</font><font color=lime>",$name);
$name = str_replace("^3","</font><font color=yellow>",$name);
$name = str_replace("^4","</font><font color=blue>",$name);
$name = str_replace("^5","</font><font color=aqua>",$name);
$name = str_replace("^6","</font><font color=#FF00FF>",$name);
$name = str_replace("^7","</font><font color=white>",$name);
$name = str_replace("^8","</font><font color=white>",$name);
$name = str_replace("^9","</font><font color=gray>",$name);
$score=$row['Score'];
//echo $team;
echo "<td align=center> $id </td>";
echo "<td align=center><a href='user.php?id=".$row["DBID"]."' > $name </a></td>";
echo "<td align=center> $group </td>";
echo "<td align=center> $score </td>";
echo "<td align=center> $ip </td>";
echo "<td align=center>";
echo "<form action=q3/slap.php?id=$id method=POST><button type=submit>Slap</button></form>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
} else {
echo "<table class=table align=center><tr><th>ID</th><th>Name</th><th>Rank</th><th>Score</th><th>IP</th><th>Action</th></tr>";
echo "<tr>";
echo "<td>";
echo "There are no players online";
echo "</td>";
echo "</tr>";
echo "</table>";
echo "</div>";
}
$conn->close();
?>

我的“在线玩家”表在上面。

最佳答案

您可以在每次对数据库进行更改时更新的服务器上存储一个时间戳。然后,在您的 setInterval 中,首先从服务器获取最后更新时间,并将其与页面加载时的时间进行比较;如果它们不同则刷新页面,否则什么都不做。

关于javascript - PHP 表自动更新数据但不更新表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35501949/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com