我在search.php文件中从数据库中获取了5条记录,我现在显示加载更多按钮,当我点击加载更多按钮时,记录正在加载,但是当我点击加载更多按钮时,已经加载的记录又在不断地加载,我想让所有其他的记录与搜索查询相关,而不是那些已经加载的记录。
搜索.php
<?php include_once 'assets/inc/header.php'; ?>
<div class="container custom-post-margin">
<div class="row">
<div class="col-lg-8 px-2 px-lg-3 mt-1 mt-lg-0">
<nav>
<ol class="breadcrumb rounded-0 m-0 text-secondary pb-1">
<h4 class="text-info">Showing result for : <b><?= $_GET['q'] ?></b></h4>
</ol>
</nav>
<!-- Posts Section -->
<?php
include_once $base_upload_url.'config/config.php';
include_once $base_upload_url.'config/utils.php';
$util = new Utils();
$config = new Config();
$q = $util->testInput($_GET['q']);
$sql = "SELECT * FROM posts WHERE post_title LIKE :q OR post_excerpt LIKE :q OR post_tags LIKE :q OR post_category LIKE :q AND post_status = :status ORDER BY id DESC LIMIT 5";
$stmt = $config->conn->prepare($sql);
$stmt->execute(['q'=>'%'.$q.'%','status'=>'publish']);
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<div id="showAllSearchedPost">
<?php if($result): ?>
<?php foreach ($result as $row): ?>
<a href="<?= BASE_URL ?>post/<?= $row['post_slug']; ?>" class="card-link text-secondary openPostLink" id="<?= $row['id'] ?>">
<div class="card p-lg-4 p-3 mb-3 rounded-0 shadow custom-post-link">
<img src="<?= BASE_URL ?>uploads/<?= basename($row['post_image']) ?>" alt="User Management System" class="card-img-top img-thumbnail shadow-sm">
<div class="card-body p-0 py-3">
<h4 class="card-title text-info font-weight-bold">
<?= $row['post_title'] ?>
</h4>
<div class="d-inline">
<i class="fas fa-user fa-sm"></i> <?= $row['post_author'] ?> | <i class="fas fa-calendar fa-sm"></i> <?= $util->timeInAgo($row['created_at']) ?> | <i class="fas fa-th-list fa-sm"></i> <?= strtoupper($row['post_category']) ?> | <i class="fas fa-comment fa-sm"></i> 15
</div>
<div class="card-text mt-2 text-justify">
<?= $row['post_excerpt'] ?>
<a href="<?= BASE_URL ?>post/<?= $row['post_slug']; ?>" class="badge badge-primary px-2 py-1 openPostLink" id="<?= $row['id'] ?>"><i class="fas fa-book-reader"></i> Read More</a></div>
</div>
</div>
</a>
<?php $post_id = $row['id']; ?>
<?php endforeach; ?>
<?php else: ?>
<div class="card p-lg-4 p-3 mb-3 rounded-0 shadow">
<div class="card-body p-0 py-3">
<h2 class="text-danger text-center py-5">
☹ No Result Found! ☹
</h2>
</div>
</div>
<?php endif; ?>
</div>
<?php
$sql2 = "SELECT * FROM posts WHERE post_title LIKE :q OR post_excerpt LIKE :q OR post_tags LIKE :q OR post_category LIKE :q AND post_status = :status ORDER BY id DESC";
$stmt2 = $config->conn->prepare($sql2);
$stmt2->execute(['q'=>'%'.$q.'%','status'=>'publish']);
?>
<div class="m-4 text-center <?= ($stmt2->rowCount() <= 5)?'d-none':''; ?>" id="show_more_main<?= $post_id; ?>">
<input type="hidden" name="q" id="q" value="<?= $_GET['q'] ?>">
<a href="#" id="<?= $post_id; ?>" class="btn btn-danger load_more">Load More <i class="fas fa-arrow-right"></i></a>
<h3 class="text-center text-light lead mt-4" id="loading" style="display:none;">Loading...</h3>
</div>
<!-- Posts Section End -->
</div>
<!-- Sidebar Widget Section -->
<div class="col-lg-4 px-2 px-lg-3">
<?php
include_once 'assets/inc/search_widget.php';
include_once 'assets/inc/recent_widget.php';
include_once 'assets/inc/category_widget.php';
include_once 'assets/inc/tags_widget.php';
include_once 'assets/inc/subscription_widget.php';
include_once 'assets/inc/author_widget.php';
include_once 'assets/inc/follow_widget.php';
?>
</div>
<!-- Sidebar Widget Section End -->
</div>
</div>
<!-- Footer Section -->
<?php include_once 'assets/inc/footer.php'; ?>
<!-- Footer Section End -->
脚本.js
$(document).on("click", ".load_more", function(e) {
e.preventDefault();
let post_id = $(this).attr('id');
let query = $("#q").val();
$("#loading").show();
$.ajax({
url: ''+BASE_URL+'assets/php/action.php',
method: 'post',
data: { searched_post_id: post_id, query: query },
success:function(res){
$("#show_more_main"+post_id).remove();
$("#showAllSearchedPost").append(res);
}
});
});
动作.php
if(isset($_POST['searched_post_id'])){
$id = $_POST['searched_post_id'];
$query = $util->testInput($_POST['query']);
$searched_post_count = $db->countPostSearch($id, $query);
$showLimit = 2;
$data = $db->loadMoreSearchedPost($id, $query, $showLimit);
$postId = '';
if($searched_post_count > 0 ){
foreach ($data as $row) {
$postId = $row['id'];
echo '<a href="'.BASE_URL.'post/'.$row['post_slug'].'" class="card-link text-secondary openPostLink" id="'.$row['id'].'">
<div class="card p-lg-4 p-3 mb-3 rounded-0 shadow custom-post-link">
<img src="'.BASE_URL.'uploads/'.basename($row['post_image']).'" alt="User Management System" class="card-img-top img-thumbnail shadow-sm">
<div class="card-body p-0 py-3">
<h4 class="card-title text-info font-weight-bold">
'.$row['post_title'].'
</h4>
<div class="d-inline">
<i class="fas fa-user fa-sm"></i> '.$row['post_author'].' | <i class="fas fa-calendar fa-sm"></i> '.$util->timeInAgo($row['created_at']).' | <i class="fas fa-th-list fa-sm"></i> '.$row['post_category'].' | <i class="fas fa-comment fa-sm"></i> 15
</div>
<div class="card-text mt-2 text-justify">
'.$row['post_excerpt'].'
<a href="'.BASE_URL.'post/'.$row['post_slug'].'" class="badge badge-primary px-2 py-1 openPostLink" id="'.$row['id'].'"><i class="fas fa-book-reader"></i> Read More</a></div>
</div>
</div>
</a>';
}
if($searched_post_count > $showLimit){
echo '<div class="m-4 text-center" id="show_more_main'.$postId.'">
<input type="hidden" name="q" id="q" value="'.$query.'">
<a href="#" id="'.$postId.'" class="btn btn-danger load_more">Load More <i class="fas fa-arrow-right"></i></a>
<h3 class="text-center text-light lead mt-4" id="loading" style="display:none;">Loading...</h3>
</div>';
}
}
}
而这是db.php
public function countPostSearch($id, $q){
$sql = "SELECT id FROM posts WHERE post_title LIKE :q OR post_excerpt LIKE :q OR post_tags LIKE :q OR post_category LIKE :q AND id < :id AND post_status = :status ORDER BY id DESC";
$stmt = $this->conn->prepare($sql);
$stmt->execute(['q'=>'%'.$q.'%', 'id'=>$id, 'status'=>'publish']);
$row = $stmt->rowCount();
return $row;
}
// Get more post of searched term
public function loadMoreSearchedPost($id, $q, $limit){
$sql = "SELECT * FROM posts WHERE post_title LIKE :q OR post_excerpt LIKE :q OR post_tags LIKE :q OR post_category LIKE :q AND id < :id AND post_status = :status ORDER BY id DESC LIMIT $limit";
$stmt = $this->conn->prepare($sql);
$stmt->execute(['q'=>'%'.$q.'%', 'id'=>$id, 'status'=>'publish']);
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $result;
}
我已经尝试了所有的方法,但不幸的是,我没有能够解决这个问题,我已经在search.php文件中从数据库中获取了5条记录,现在我正在显示load more按钮,当我点击load more按钮时,行正在加载,但同样的行已经加载了,但我没有能够解决这个问题。
仔细查看你的SQL语句 loadMoreSearchedPost()
:
$sql = "SELECT * FROM posts WHERE post_title LIKE :q OR post_excerpt LIKE :q OR post_tags LIKE :q OR post_category LIKE :q AND id < :id AND post_status = :status ORDER BY id DESC LIMIT $limit";
您的 WHERE
子句的评价是这样的。
WHERE
post_title LIKE :q
OR
post_excerpt LIKE :q
OR
post_tags LIKE :q
OR
( post_category LIKE :q AND id < :id AND post_status = :status )
你应该把它改成:
$sql = "SELECT * FROM posts WHERE ( post_title LIKE :q OR post_excerpt LIKE :q OR post_tags LIKE :q OR post_category LIKE :q ) AND id < :id AND post_status = :status ORDER BY id DESC LIMIT $limit";
同样的SQL语句在 countPostSearch()
:
$sql = "SELECT id FROM posts WHERE post_title LIKE :q OR post_excerpt LIKE :q OR post_tags LIKE :q OR post_category LIKE :q AND id < :id AND post_status = :status ORDER BY id DESC";
应该是
$sql = "SELECT id FROM posts WHERE ( post_title LIKE :q OR post_excerpt LIKE :q OR post_tags LIKE :q OR post_category LIKE :q ) AND id < :id AND post_status = :status ORDER BY id DESC";