Я выбрал 5 строк из базы данных в поиске. php файл и теперь я показываю кнопку «загрузить больше», когда я нажимаю кнопку «загрузить больше», тогда строки загружаются, но те же строки, которые уже загружены, загружаются снова и снова, когда я нажимаю на кнопку «загрузить больше». Я хочу, чтобы все остальные строки относились к поисковому запросу, но не к тем, которые уже загружены. Вот мои коды
search. 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 -->
script. 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);
}
});
});
action. 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>';
}
}
}
А это дБ. 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;
}
Я пробовал все, но, к сожалению, я не смог это исправить.