我正在为搜索结果创建分页,在第一个加载页面上一切都很好, 但是,当我单击分页链接时,它将显示白页(搜索页面,但没有条目)。 根本没有从我的数据中找到结果
这是index.php中的网站搜索框
<form action="search/" method="post">
<input type="text" name="search" placeholder="word..." required />
<input type="submit" name="submit_search" class="search-btn" value="" />
</form>
这是我的
htaccess
分页设置:
RewriteRule ^search/page/(.*)$ search.php?page=$1 [L,NC]
在浏览器上看起来不错。
这是我的搜索页代码:
<?php
if(isset($_POST['submit_search']) && !empty($search)){
$search = htmlspecialchars(strip_tags($_POST['search']), ENT_QUOTES);
$perpage = intval("12");
$n_stmt = $pdo->prepare("SELECT * FROM posts");
$n_stmt->execute();
$total_posts = $n_stmt->rowCount();
$total_pages = ceil($total_posts/$perpage);
$page = !empty($_GET['page']) && intval($_GET['page']) ? (int) intval($_GET['page']) : intval("1");
if($page < intval("1")) $page = intval("1");
if($page > $total_pages) $page = $total_pages;
$limit = ($page - intval("1")) * $perpage;
$pages_to_show = intval("10");
$stmt = $pdo->prepare("SELECT * FROM posts WHERE title LIKE :search OR subject LIKE :search OR descriptions LIKE :search OR keywords LIKE :search ORDER BY created DESC LIMIT :limit, :perpage");
$stmt->bindValue(":search","%".$search."%", PDO::PARAM_STR);
$stmt->bindValue(":limit",$limit, PDO::PARAM_INT);
$stmt->bindValue(":perpage",$perpage, PDO::PARAM_INT);
if($stmt->execute()){
if($stmt->rowCount() > 0){
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){?>
<div class="news_box">
<a href="<?php echo htmlspecialchars($row['news_url']);?>/" title="<?php echo htmlspecialchars($row['title']);?>">
<div class="title"><h2><?php echo htmlspecialchars($row['title']);?></h2></div>
<div class="image">
<img src="images/posts/<?php echo htmlspecialchars($row['img']);?>" alt="<?php echo htmlspecialchars($row['title']);?>"/></div>
<div class="spot"><?php echo htmlspecialchars($row['subject']);?></div>
</a>
</div>
<?php
}
}
}
unset($stmt);
?>
这是我的分页部分:
<div class="pagination">
<?php if($page >1){?>
<a href="search/page/1/">First</a>
<a href="search/page/<?php echo $page - intval("1");?>">previous</a>
<?php } for($i = $page - $pages_to_show; $i < $page + $pages_to_show + 1; $i++){
if($i > 0 and $i <= $total_pages){
if($i == $page){?>
<a href="search/page/<?php echo intval($i);?>" class="active"><?php echo intval($i);?></a>
<?php }else{?>
<a href="search/page/<?php echo intval($i);?>"><?php echo $i;?></a>
<?php
}
}
?>
<?php } ?>
<?php if($page != $total_pages){?>
<a href="search/page/<?php echo $page + intval("1");?>">Next</a>
<a href="search/page/<?php echo $total_pages;?>">Last</a>
<?php } ?>
</div>