[英]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.