2016-02-19 34 views
1

我有一個遊戲服務器,它具有聊天功能並將所有玩家的聊天消息記錄到我的數據庫。我試圖創建一個自動更新數據但不是表本身的表格,這是因爲在我的桌子上我想要一個每個玩家的動作下拉列表(從服務器,禁止玩家,靜音玩家,巴掌玩家等等的踢球員) 。)但我的JavaScript代碼在這一刻刷新整個表每5秒。所以,如果我打開我的下拉列表,當表格刷新時它將關閉下拉列表,此時我已經將下拉列表更改爲一個按鈕,因爲這個問題。PHP表自動更新數據但不是表

這裏是我的代碼:

索引頁顯示的表:

<?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(); 
 
?>

我的 「在線玩家」 表上面。

回答

0

您可以在每次更改數據庫時更新的服務器上存儲時間戳。然後,在您的setInterval中,首先從服務器獲取上次更新時間,並將其與加載頁面時的時間進行比較;如果他們刷新頁面不同,否則什麼都不做。

+0

服務器24/7連續有玩家,所以桌子不斷更新:S – BrenLib