仅当将新内容添加到数据库时才刷新div的内容

时间:2012-09-14 12:42:20

标签: php jquery mysql

只有在将新内容添加到数据库时才可以刷新div内容吗?

我将其用于旁边菜单中显示的“最近帖子”。

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

在某种程度上可以检查一个新帖子是否已添加到数据库中,然后只将该帖子数据添加到div中?

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

5 个答案:

答案 0 :(得分: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);

希望这有帮助!

答案 1 :(得分:1)

是可以的,但您需要在php中创建一个方法并使用Ajax刷新div

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

答案 2 :(得分: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而不是完全覆盖它。

答案 3 :(得分:1)

是的,这是可能的

以下代码将获取最近添加的照片:

  1. 在TIMER.PHP之后(或你的wordpress博客页面)
  2. 仅在新照片添加后(您可以将其用于最近的帖子,评论或其他任何内容)
  3. 这可用于创建实时博客,例如,用户将看到所有最近的评论,图像或帖子或所有内容,即使他没有重新加载当前页面。并且它不会消耗大量带宽,因为只有新添加的内容才会被重新加载(否则它只会在数据库中发送一个小的支票)。

    我刚刚为此制定了解决方案,我必须与您分享。我正在为Wordpress做这件事,我需要一个功能,可以在用户加载页面后获取已添加 ONLY 的照片。它可以通过每5秒刷新一次div来完成,但想象一下,如果有20张照片,并且每5秒钟就必须刷新一次......那就是大量的MB数据。因此,我们将刷新它们仅在添加新照片时(您可以将其应用于任何内容,从帖子到评论或用户等)。

    有3个PHP文件: timer.php check.php display.php

    timer.php将每5秒加载一次check.php,以检查是否添加了新内容。注意当前加载check.php时的 -6 extract

    timer.php的日期时间将通过check.php传递(通过 check.php?tim = **打印日期)(** display.php?timm ='。$ tizz 。“)和display.php,所以我们可以将它用作我们数据库中的参考(如果添加新图像,我们将加载所有图像的时间)。

    如果有任何问题,请问。它也必须为你工作,因为它对我有用。

    <强>享受! :)

    以下是3个PHP文件,只需根据您的需要进行自定义:

    TIMER.PHP (或您的wordpress博客页面):

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

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

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

答案 4 :(得分:0)

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

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

此外,您必须使用.html().append()

创建新内容