sql查询加载更多

时间:2017-03-10 20:56:46

标签: php ajax mysqli

我有一个加载更多按钮,它从数据库(post_id)加载特定帖子的注释。问题是,当我点击加载更多时,它正在加载数据库表中的所有注释

pagecomment.php

$id = $_GET['id'];
$id_post = $id ;//the post or the page id
<div class="cmt-container" >
<?php
$sql = mysqli_query($con,"SELECT * FROM comments WHERE id_post ='$id_post'ORDER BY id DESC limit 2") or die(mysqli_error($con));
$rowCount = mysqli_num_rows($sql);
if($rowCount > 0){
while($affcom = mysqli_fetch_assoc($sql)){
    $name = $affcom['name'];
    $email = $affcom['email'];
    $comment = $affcom['comment'];
    $date = $affcom['date'];

loadmore.php

if(isset($_POST["id"]) && !empty($_POST["id"])){ 
 $queryAll =   mysqli_query($con,"SELECT COUNT(*) as num_rows FROM comments   WHERE id < ".$_POST['id']." ORDER BY id DESC");
$row = mysqli_fetch_assoc($queryAll);
$allRows = $row['num_rows'];

$showLimit = 2;

//get rows query
$query = mysqli_query($con, "SELECT * FROM comments WHERE id <     ".$_POST['id']." ORDER BY id DESC LIMIT ".$showLimit);

//number of rows
$rowCount = mysqli_num_rows($query);

if($rowCount > 0){
while($row = mysqli_fetch_assoc($query)){
    $tutorial_id = $row["id"]; ?>
    <div class="cmt-cnt">
        <div id="cmt-cmt"><img src="<?php echo $row['email']; ?>" alt="" />    </div>
        <div class="thecom">
            <h5><?php echo $row['name']; ?></h5><span data-utime="1371248446" class="com-dt"><?php $curenttime=$row['date'];
                $time_ago =strtotime($curenttime);
                echo timeAgo($time_ago); ?></span>
            <br/>
            <p>
                <?php echo $row['comment']; ?>
            </p>
        </div>
    </div><!-- end "cmt-cnt" -->

问题在于loadmore.php中的查询

1 个答案:

答案 0 :(得分:0)

在loadmore.php中,SQL查询不知道您想要评论的id_post

$queryAll =   mysqli_query($con,"SELECT COUNT(*) as num_rows FROM comments   WHERE id < ".$_POST['id']." ORDER BY id DESC");

用以下内容修改:

$queryAll =   mysqli_query($con,"SELECT COUNT(*) as num_rows FROM comments   WHERE id < ".$_POST['id']." AND id_post = ".$id_post." ORDER BY id DESC");

$query = mysqli_query($con, "SELECT * FROM comments WHERE id < ".$_POST['id']." ORDER BY id DESC LIMIT ".$showLimit);

用以下内容修改:

$query = mysqli_query($con, "SELECT * FROM comments WHERE id < ".$_POST['id']." AND id_post = ".$id_post." ORDER BY id DESC LIMIT ".$showLimit);

通过添加AND语句的WHERE部分,您可以添加更多条件。

希望它有所帮助。

编辑:

当然,您需要从Ajax或其他东西获取$id_post