2

是否只有在将新内容添加到数据库时才能刷新 div 内容?

我将它用于出现在侧边菜单中的“最近的帖子”。

现在 div 设置为每 10 秒刷新一次。

是否有可能以某种方式检查是否将新帖子添加到数据库中,然后仅将帖子数据添加到 div 中?

我正在使用 MySql、php 和 jquery 来完成所有这些工作。

4

5 回答 5

2

是的,您可以通过比较文本内容来做到这一点。

function updateHtmlContent() {
    var htmlContent=$('#divToUpdate').text();
    $.ajax({
        type: "GET",
        datatype: "text",
        //php function that should give you the text back
        url: 'dataBaseFunction.php',
        success: function(data) {
            if (htmlContent != data) {
                $('#divToUpdate').text(data);
            }
        }
    });
}

//compare and upate every 20 seconds
setInterval(updateHtmlContent(), 20000);

希望这有帮助!

于 2012-09-14T12:55:32.137 回答
1

YES IT IS POSSIBLE

The below code will get the recent photos that has been added:

  1. AFTER THE TIMER.PHP WAS LAODED (or your wordpress blog page)
  2. AND ONLY AFTER A NEW PHOTO IS ADDED (you can use it for recent posts, comments, or anything)

This can be used to create a live blog for example, where the user will see all the recent comments, images or posts, or everything, even if he doesn't reload the current page. And it won't consume a lot of bandwidth as the content will be reloaded ONLY IF THERE IS NEW ADDED (it will only send a tiny check in the database otherwise).

I've just finished working out a solution for that, and I have to share it with you. I was doing it for Wordpress, i needed a function that gets the photos that has been added ONLY after the user loaded the page. It could be done by simply refreshing the div every 5 seconds, but imagine if there were 20 photos, and they had to be refreshed every 5 seconds... that's a lot of MB of data. So we will refresh them ONLY when a new photo is added (you can apply it to anything, from posts to comments or users, etc.).

There are 3 PHP files: timer.php, check.php and display.php.

The timer.php will load the check.php every 5 seconds, to check if new content is added. Notice the -6 extraction from the time of the current load of check.php.

The date-time of the timer.php will be passed (by check.php?tim=**print date) through the check.php (by **display.php?timm='. $tizz .') and to the display.php, so we can use it for a reference in our database (that will be the time from where we will load all the images, if new images are added).

If any questions, just ask. It has to work for you too, as it works for me.

ENJOY! :)

Below are the 3 PHP files, just customize them for your needs:

THE TIMER.PHP (or your wordpress blog pages):

<script type="text/javascript" src="jquery.js"></script>

<script type="text/javascript">
        $(document).ready(function(){


            setInterval(function(){
                $("#quotex a").load("check.php?tim=<?php print date("Y-m-d H:i:s"); ?>");
            }, 5000);

        });
        </script>

        <div id="quote"><a></a></div>


        <div id="quotex"><a></a></div>

THE CHECK.PHP :

 <?php 
     // Connects to your Database 
     mysql_connect("localhost", "username", "password") or die(mysql_error()); 
     mysql_select_db("database name") or die(mysql_error()); 



     // SQL query
        $strSQL = "SELECT * FROM wp_posts WHERE   post_mime_type LIKE 'image/jpeg' ORDER BY `wp_posts`.`id`  DESC LIMIT 1";

        // Execute the query (the recordset $rs contains the result)
        $rs = mysql_query($strSQL);

        // Loop the recordset $rs
        // Each row will be made into an array ($row) using mysql_fetch_array
        while($row = mysql_fetch_array($rs)) {

        $atime = $row['post_date'];

    $tizz = $_GET['tim'];    

    $dsff = $row['post_date'];;

    $duff = date("Y-m-d H:i:s");

    //convert the date-time into seconds so we can extract 6 seconds from it
    $six = strtotime(date("Y-m-d H:i:s"))-6; 

    //convert the latest image date-time too from database so we can compare it
     $sox = strtotime("$dsff");

     if ($six < $sox)
      {
      echo '<script type="text/javascript">$(document).ready( function(){ $("#quote a").load("display.php?timm='. $tizz .'"); } ); </script>';
      }
          }

        // Close the database connection
        mysql_close();


     ?>

THE DISPLAY.PHP:

<?php 
$tipp = $_GET["timm"];
 // Connects to your Database 
 mysql_connect("localhost", "username", "password") or die(mysql_error()); 
 mysql_select_db("database name") or die(mysql_error()); 




 // SQL query
    $strSQL = "SELECT * FROM wp_posts WHERE   post_mime_type LIKE 'image/jpeg' AND post_date > '$tipp' ORDER BY `wp_posts`.`id`  DESC LIMIT 10";
    // Execute the query (the recordset $rs contains the result)
    $rs = mysql_query($strSQL);

    // Loop the recordset $rs
    // Each row will be made into an array ($row) using mysql_fetch_array
    while($row = mysql_fetch_array($rs)) {
    //guid is the column where the image url is located in the wordpress database table
    $atime = $row['guid'];

     echo "<img src='". $atime ."' /><br />";
      }
    // Close the database connection
    mysql_close();


 ?>
于 2013-01-13T11:52:23.310 回答
1

我想你可能已经完成了大约 90% 的工作。我假设您每 10 秒在内容中使用 jQuery 到 AJAX(PS 这似乎很多?)

我认为您可以通过使后台功能从找到的最新帖子之后的帖子开始来解决您的问题。

因此,如果您的查询现在看起来像这样:

SELECT a,b,c FROM foo

你应该把它改成

SELECT a,b,c FROM foo WHERE id > $last_found_id

您可以存储last_found_id在您的 Javascript 中,并在调用该函数时将其发回。如果结果集为空,则不必刷新 div。

此更改还可能需要您预先/附加到 div 而不是完全覆盖它。

于 2012-09-14T12:49:03.687 回答
1

Yes 是可能的,但是您需要在其中创建一个方法php并使用它Ajax来刷新您的div.

如果您使用代码更新您的问题,我可以提供一个示例。

于 2012-09-14T12:44:14.077 回答
0

查看setInterval(function, time)javascript 中的每个“时间”(以毫秒为单位)进行循环,

以及$.ajax()jQuery 中将信息传递给服务器的函数。

此外,您必须使用.html()或创建新内容.append()

于 2012-09-14T12:49:35.247 回答