【发布时间】:2012-09-07 15:14:22
【问题描述】:
只有在数据库中添加新内容时,是否可以刷新 div 内容?
我将它用于显示在侧边菜单中的“最近的帖子”。
现在 div 设置为每 10 秒刷新一次。
是否可以通过某种方式检查是否将新帖子添加到数据库中,然后仅将帖子数据添加到 div 中?
我正在使用 MySql、php 和 jquery 来完成所有这些工作。
【问题讨论】:
只有在数据库中添加新内容时,是否可以刷新 div 内容?
我将它用于显示在侧边菜单中的“最近的帖子”。
现在 div 设置为每 10 秒刷新一次。
是否可以通过某种方式检查是否将新帖子添加到数据库中,然后仅将帖子数据添加到 div 中?
我正在使用 MySql、php 和 jquery 来完成所有这些工作。
【问题讨论】:
可以,但是您需要在php 中创建一个方法并使用Ajax 来刷新您的div。
如果您用代码更新您的问题,我可以提供一个示例。
【讨论】:
我认为您可能已经完成了大约 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 而不是完全覆盖它。
【讨论】:
是的,你可以通过比较文本内容来做到这一点。
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);
希望对您有所帮助!
【讨论】:
是的,这是可能的
以下代码将获取已添加的近期照片:
这可用于创建实时博客,例如,用户将在其中看到所有最近的 cmets、图像或帖子或所有内容,即使他没有重新加载当前页面。并且它不会消耗大量带宽,因为只有在添加新内容时才会重新加载内容(否则只会在数据库中发送一个微小的检查)。
我刚刚完成了一个解决方案,我必须与你分享。我是为 Wordpress 做的,我需要一个函数来获取用户加载页面后ONLY添加的照片。这可以通过简单地每 5 秒刷新一次 div 来完成,但是想象一下如果有 20 张照片,并且它们必须每 5 秒刷新一次……那是很多 MB 的数据。所以我们会仅在添加新照片时刷新它们(您可以将其应用于任何内容,从帖子到 cmets 或用户等)。
有 3 个 PHP 文件:timer.php、check.php 和 display.php。
timer.php 将每 5 秒加载一次 check.php,以检查是否添加了新内容。注意-6提取从当前加载check.php的时间开始。
timer.php 的日期时间将通过 check.php 传递(通过 check.php?tim=**print 日期)(通过 **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();
?>
【讨论】: