Ajax每5秒刷新一次DB结果,但只需刷新新内容

时间:2015-05-07 12:36:25

标签: javascript php jquery ajax

我每隔5秒使用ajax刷新我的数据库表结果,因为表中的结果存储日期/时间,我正在试图弄清楚如何为20“新”结果加载所有当前数据在缓存中然后加载只有5秒的新结果,而不是整个表刷新。

这是我目前的代码:

     <script language="JavaScript"> 
   setInterval("updateContent();", 5000 ); 
   function updateContent() 
    { 
         $('#feed').empty(); 
         $('#feed').load("load.php"); 
    } 
</script> 
<div id="feed">
</div>

load.php

<?php
$servername = "localhost";
$username = "root";
$password = "n/a";
$dbname = "n/a";

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


  session_start();
if(!isset($_SESSION["user"]) or !is_array($_SESSION["user"]) or empty($_SESSION["user"])
)

      // redirect to index page if not superuser
header('Location: index.php');

                $now = time(); $utc_time = $now - intval(date('Z', $now)); 

$sql = "SELECT DISTINCT users.id, users.firstname, users.lastname, 
    feed.date, feed.time, feed.text, feed.userID, feed.ip, feed.post_id, feed.format, feed.file_format, feed.MediaTxt, feed.author_firstname, feed.author_lastname, feed.shared
FROM users 
JOIN friends
    ON users.id IN (friends.sender, friends.recipient)
JOIN feed 
    ON (users.id = feed.userID)
WHERE ".$_SESSION["user"]["id"]." IN (friends.sender,friends.recipient) 
    AND friends.status = 1
ORDER BY feed.date DESC, feed.time DESC";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<div class='feed'><div class='feedtext'><h4 class='feedfontname'>" 
         . $row["firstname"]. 
         " "
          . $row["lastname"]. 
          "</h4><h4 style='color:gray'>"
          . $row["shared"]. " "
          . $row["author_firstname"]. " "
          . $row["author_lastname"]. "</h4><h6 class='feedfontstyle'>"
          . $row["MediaTxt"]. "</h6><"
          . $row["format"]." class='feedfontstyle' src='http://www.socialnetwk.com/users/". $_SESSION["user"]["id"] ."/feed/"
          . $row["format"]."/". $row["post_id"]."."
          . $row['file_format']."' ". $row['format'] . ">". $row['text'] ."</". $row['format'] ."></div><h6 class='feedtimefont'>". $row['time']."</h6>";




    }
}

$conn->close();
?>

0 个答案:

没有答案