[英]load more with ajax php
你好,我在加載更多的ajax時遇到問題,但是一切都很好,但是當沒有數據要顯示時,請向我顯示此錯誤: http : //i.stack.imgur.com/6158Y.png
index.php
<div id="show_here">
<div id="hide_here" onclick="show_more_posts(<?php echo $post['id']; ?>);">Show More Posts</div>
</div>
main.js
// Load more post
function show_more_posts(post_id)
{
var ID = post_id;
$.ajax({
type: "POST",
url: "ajax/ajax_more.php",
data: "lastmsg="+ ID,
cache: false,
success: function(html){
$("#show_here").append(html);
$("#hide_here").remove();
}
});
return false;
}
ajax_more.php
<?php
include("../config.php");
$lastmsg = $_POST['lastmsg'];
$result = mysqli_query($Connection, "select * from posts where id<'$lastmsg' order by id desc limit 10");
while($row = mysqli_fetch_array($result))
{
$msg_id=$row['id'];
$message=$row['text']; ?>
<li> <?php echo $message; ?> </li> <?php
}
?>
<div id="show_here">
<div id="hide_here" onclick="show_more_posts(<?php echo $msg_id; ?>);">Show More Posts</div>
</div>
你只需要這樣做
<div id="show_here">
<?php
if(isset($post['id'])) {
?>
<div id="hide_here" onclick="show_more_posts(<?php echo $post['id']; ?>);">Show More Posts</div>
<?php
}else{
?>
<div id="hide_here">No More Posts to show !!!!</div>
<?php
}
?>
</div>
試試這個,可能對您有幫助
您可以這樣使用。 那么您將不會出錯。
<?php
if(isset($msg_id)) {
?>
<div id="show_here">
<div id="hide_here" onclick="show_more_posts(<?php echo $msg_id; ?>);">Show More Posts</div>
</div>
<?php
}
?>
在ajax_more.php文件中,您必須在$lastmsg
之前將$msg_id
設置$msg_id
$lastmsg
:
<?php
include("../config.php");
$lastmsg = $_POST['lastmsg'];
$result = mysqli_query($Connection, "select * from posts where id<'$lastmsg' order by id desc limit 10");
$msg_id = $lastmsg;
while($row = mysqli_fetch_array($result))
{
$msg_id=$row['id'];
$message=$row['text']; ?>
<li> <?php echo $message; ?> </li> <?php
}
?>
<div id="hide_here" onclick="show_more_posts(<?php echo $msg_id; ?>);">Show More Posts</div>
因此,即使查詢不返回數據(沒有新消息),該變量也具有正確的值。
相反,如果您期望在某個時候沒有更多的帖子要加載,則必須使用Kaja Mydeen提供的解決方案:
<?php
include("../config.php");
$lastmsg = $_POST['lastmsg'];
$result = mysqli_query($Connection, "select * from posts where id<'$lastmsg' order by id desc limit 10");
while($row = mysqli_fetch_array($result))
{
$msg_id=$row['id'];
$message=$row['text']; ?>
<li> <?php echo $message; ?> </li> <?php
}
?>
<?php if(isset($msg_id)) { ?>
<div id="hide_here" onclick="show_more_posts(<?php echo $msg_id; ?>);">Show More Posts</div>
<?php } ?>
另外,您的代碼有些奇怪:您繼續使用和id="show_here"
添加div
。
我認為在通過while循環獲取數據之前,您應該檢查像這樣的總受影響行
$totrows = mysql_num_rows($result);
if($totrows > 0){
//your while loop for fetch data
}else{
//return something else
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.