仅当将新内容添加到数据库中时,刷新div的内容 [英] Refresh a div's content only if new content is added to the database

查看:99
本文介绍了仅当将新内容添加到数据库中时,刷新div的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只有将新内容添加到数据库中时,才可以刷新divs内容吗?

Is it possible to refresh a divs content only if new content is added to the database?

我将其用于侧面菜单中显示的最新帖子".

I'm using this for "recent posts" that appear in the side menu.

现在,将div设置为每10秒刷新一次.

Right now the div is set refresh every 10 seconds.

是否可以通过某种方式检查它是否已将新帖子添加到数据库,然后仅将该帖子数据添加到div?

Is it somehow possible to check it a new post was added to the db and then add only that posts data to the div?

我正在使用MySql,php和jquery完成所有这些操作.

I'm using MySql, php and jquery to do all of this.

推荐答案

是可能的

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

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

  1. 在放置TIMER.PHP之后(或您的wordpress博客页面)
  2. 并且仅在添加新照片后(您可以将其用于最近的帖子,评论或其他内容)

例如,这可以用于创建实时博客,在该博客中,即使用户没有重新加载当前页面,用户也将看到所有最近的评论,图像或帖子,或所有内容.而且它不会消耗很多带宽,因为只有在添加了新内容的情况下才会重新加载内容(否则只会在数据库中发送一小笔支票).

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

我刚刚完成了一个解决方案,我必须与您分享.我在Wordpress上这样做,我需要一个函数来获取用户加载页面后仅添加的照片.只需每5秒钟刷新一次div即可完成,但是想像一下是否有20张照片,并且每5秒钟必须刷新一次……这是大量的MB数据.因此,我们将仅在添加新照片后刷新它们(您可以将其应用于任何内容,从帖子到评论或用户等).

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

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

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

timer.php将每5秒加载一次check.php,以检查是否添加了新内容.请注意,从当前check.php加载时间开始的 -6提取.

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.

timer.php的日期时间将通过check.php(通过** display.php?timm =')传递(通过 check.php?tim = **打印日期). )和display.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.

享受! :)

下面是3个PHP文件,只需根据需要对其进行自定义:

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

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

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>

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


 ?>

这篇关于仅当将新内容添加到数据库中时,刷新div的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆