我有一个加载更多按钮,它从数据库(post_id)加载特定帖子的注释。问题是,当我点击加载更多时,它正在加载数据库表中的所有注释
$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'];
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中的查询
答案 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
。