简体   繁体   English

用ajax php加载更多

[英]load more with ajax php

hello here i have a problem with load more ajax all is okey but when there is no data to show from database show me this error: http://i.stack.imgur.com/6158Y.png 你好,我在加载更多的ajax时遇到问题,但是一切都很好,但是当没有数据要显示时,请向我显示此错误: http : //i.stack.imgur.com/6158Y.png

index.php 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 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 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>

you just need to do like this 你只需要这样做

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

Try this, it may help you 试试这个,可能对您有帮助

You can use like this. 您可以这样使用。 then you will not get error. 那么您将不会出错。

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

In your ajax_more.php file you have to set $msg_id to $lastmsg before the while: 在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>

so, even if your query doesn't return data (no new messages), the variable have the correct value. 因此,即使查询不返回数据(没有新消息),该变量也具有正确的值。

If, instead, you expect that at some point, there aren't more posts to load, you have to use the solution provided also from Kaja Mydeen: 相反,如果您期望在某个时候没有更多的帖子要加载,则必须使用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 } ?>

Also, your code have somethin strange: you continue to add a div with and id="show_here" . 另外,您的代码有些奇怪:您继续使用和id="show_here"添加div

I think before fetch data through while loop you should have check for total affected rows like this 我认为在通过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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM