使用post方法分页的搜索结果

xyhw6mcr  于 2021-06-18  发布在  Mysql
关注(0)|答案(0)|浏览(280)

我有一个分页的搜索结果,我使用这个http://www.findsourcecode.com/php/pagination-in-search-engine/ 作为参考。该网站正在使用mysql和get方法进行搜索。我把mysql改成mysqli,效果很好。然后,我尝试将搜索结果的get方法改为post方法。根据我读到的内容,post方法更安全。问题是如何改变这种方法并使其发挥作用?我面临的问题是,结果不显示在第二页,但第一页显示的结果,我想要的。请帮帮我!!
下面是我的代码。。
php(搜索表单)

<div class="container">
    <img src="image/sbback.jpg">
    <div class="center">
        <form class="form-wrapper cf" action="page.php" method="POST">
            <input type="text" placeholder="Search here..." name="search" required>
            <button type="submit" name="submit" value="Search source code" ><img src="image/search1.png" width="30" height="30" vspace="11"></button>
        </form>

    </div>

</div>

页面.php

<?php
include( 'session.php' );
header('Cache-Control: no cache'); //no cache
if(isset($_SESSION['user']['role'])){
 $role=$_SESSION['user']['role'];
}

?>

<?php
     if (isset($_POST['submit'])){
     $search = mysqli_real_escape_string($connection, $_POST['search']);

if(strlen($search)<=1)
echo "Search term too short";
else{                
echo "You searched for <b>$search</b> <hr size='1'></br>";
$connection=mysqli_connect("localhost","root","","company");

$search_exploded = explode (" ", $search);

$x = "";
$construct = "";  

foreach($search_exploded as $search_each)
{
$x++;
if($x==1)
$construct .="title LIKE '%$search_each%' OR description LIKE '%$search_each%' OR author LIKE '%$search_each%' OR date LIKE '%$search_each%' OR time LIKE '%$search_each%'";
else
$construct .="AND title LIKE '%$search_each%' OR description LIKE '%$search_each%' OR author LIKE '%$search_each%' OR date LIKE '%$search_each%' OR time LIKE '%$search_each%'";

}

$constructs ="SELECT * FROM tbl_uploads WHERE $construct";
$run = mysqli_query($connection,$constructs);

$foundnum = mysqli_num_rows($run);

if ($foundnum==0)
echo "Sorry, there are no matching result for <b>$search</b>.</br></br>1. 
Try more general words. for example: If you want to search 'how to create a website'
then use general keyword like 'create' 'website'</br>2. Try different words with similar
 meaning</br>3. Please check your spelling";
else
{ 

echo "$foundnum results found !<p>";

$per_page = 10;
$start = isset($_GET['start']) ? $_GET['start']: '';
$max_pages = ceil($foundnum / $per_page);
if(!$start)
$start=0; 
$sql = "SELECT * FROM tbl_uploads WHERE $construct LIMIT $start,$per_page"; 
$getquery = mysqli_query($connection,$sql);

while($runrows = mysqli_fetch_assoc($getquery))
{
$title = $runrows ['title'];
$desc = $runrows ['description'];
$author = $runrows ['author'];
$date = $runrows ['date'];
$time = $runrows ['time'];  

 echo '<a href="uploads/'.$runrows['file'].'">
      '.$runrows['title'].'</a>
      <p>'.$desc.'</p>
       <p>'.$author.' / '.$date.' / '.$time.'</p>';

}

//Pagination Starts
echo "<center>";

$prev = $start - $per_page;
$next = $start + $per_page;

$adjacents = 3;
$last = $max_pages - 1;

if($max_pages > 1)
{   
//previous button
if (!($start<=0)) 
echo " <a href='page.php?search=$search&submit=Search+source+code&start=$prev'>Prev</a> ";    

//pages 
if ($max_pages < 7 + ($adjacents * 2))   //not enough pages to bother breaking it up
{
$i = 0;   
for ($counter = 1; $counter <= $max_pages; $counter++)
{
if ($i == $start){
echo " <a href='page.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='page.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}  
$i = $i + $per_page;                 
}
}
elseif($max_pages > 5 + ($adjacents * 2))    //enough pages to hide some
{
//close to beginning; only hide later pages
if(($start/$per_page) < 1 + ($adjacents * 2))        
{
$i = 0;
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($i == $start){
echo " <a href='page.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='page.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
} 
$i = $i + $per_page;                                       
}

}
//in middle; hide some front and some back
elseif($max_pages - ($adjacents * 2) > ($start / $per_page) && ($start / $per_page) > ($adjacents * 2))
{
echo " <a href='page.php?search=$search&submit=Search+source+code&start=0'>1</a> ";
echo " <a href='search.php?search=$search&submit=Search+source+code&start=$per_page'>2</a> .... ";

$i = $start;                 
for ($counter = ($start/$per_page)+1; $counter < ($start / $per_page) + $adjacents + 2; $counter++)
{
if ($i == $start){
echo " <a href='page.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='page.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}   
$i = $i + $per_page;                
}

}
//close to end; only hide early pages
else
{
echo " <a href='page.php?search=$search&submit=Search+source+code&start=0'>1</a> ";
echo " <a href='search.php?search=$search&submit=Search+source+code&start=$per_page'>2</a> .... ";

$i = $start;                
for ($counter = ($start / $per_page) + 1; $counter <= $max_pages; $counter++)
{
if ($i == $start){
echo " <a href='page.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='page.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";   
} 
$i = $i + $per_page;              
}
}
}

//next button
if (!($start >=$foundnum-$per_page))
echo " <a href='page.php?search=$search&submit=Search+source+code&start=$next'>Next</a> ";    
}   
echo "</center>";
} 
}
     }
?>

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题