使用PHP和MySQL实现带分页的搜索结果展示

3
我有一个搜索文本框,如果我搜索任何术语,例如PHP、html,它将显示结果。
现在我需要显示分页页面,如1 2 3 4 5等。
我已经浪费了3天的时间,我自己尝试了很多次,但没有用。我不知道我错在哪里。
没有错误出现,没有下一页显示前两个结果的记录。
需要获取下一页的结果,比如第2页、第3页等,最后一页显示结果。
请帮助我...我的代码如下:
<?php
require('../connect.php');
$rec_limit = 2;
if (isset($_GET['search']))
{
$searchtext= $_GET['searchtext'];
}
$sql = "SELECT * from jobpost WHERE jobtitle LIKE '%$searchtext%' LIMIT 2";
mysqli_select_db($con, 'login');
$result= mysqli_query($con, $sql);
if(!$result)
{
echo "error";
}   
if(mysqli_num_rows($result) >0){
echo "Your Search Term : $searchtext ";
echo"<br>";
while($row = $result->fetch_assoc())        
{

    echo "<table>";
    echo "<tr> <td>Job Title:</td> <td>{$row['jobtitle']} </td>      
 </tr>".
        "<tr> <td>Job Description: </td> <td>  
  {$row['jobdescription']}</td> </tr> ".
        "<br>";
    echo "</table>"; 

} 
}
else{
    echo "Your Search Term : $searchtext ";
    echo "<br>";
    echo "no results found";
}
// pagination
$row = mysqli_fetch_array($result, MYSQL_NUM );
$rec_count = $row[0];
if( isset($_GET{'page'} ) )
{
$page = $_GET{'page'} + 1;
$offset = $rec_limit * $page ;
}
else
{
$page = 0;
$offset = 0;
}
$left_rec = $rec_count - ($page * $rec_limit);
$data = mysqli_query($con, "SELECT * FROM jobpost WHERE jobtitle LIKE     
'%$searchtext%' LIMIT $offset, $rec_limit") or die(mysqli_error());
if(mysqli_num_rows($data) >0){
while($row = mysqli_fetch_array($data)) 
{
    /*echo "<table>";
    echo "<tr > <td>Job Title:</td> <td>{$row['jobtitle']} </td>     
</tr>".
        "<tr > <td>Job Description: </td> <td>        
{$row['jobdescription']}</td> </tr> ".
        "<br>";
    echo "</table>"; */
}
}

else{
echo"<a href='search.php?searchtext=$searchtext&search=search'>No images     
available click here for Home </a>";
}
if( $page > 0 )
{
$last = $page - 2;
echo "<br>";
echo "<a href=\"search.php?searchtext=$searchtext&search=search=$last\"      
>Last 2 Records</a> |";
echo "<a href=\"search.php?searchtext=$searchtext&search=search=$page\"               
>Next 2 Records</a>";
}
else if( $page == 0 )
{
echo "<br>";
echo "<a href=\"search.php?searchtext=$searchtext&search=search=$page\"      
>1</a>";
}
else if( $left_rec < $rec_limit )
{
$last = $page - 2;
echo "<br>";
echo "<a href=\"search.php?searchtext=$searchtext&search=search=$last\"   
>2</a>";
}
mysqli_close($con);
?>

请帮我一个忙。

我想在文本框中搜索任何输入,并在分页中显示它,这是我的需求。

2个回答

3
使用这个分页代码。
    include("../connect.php");
        $tableName="jobpost";   
        $targetpage = "view_data.php";  
        $limit =10;
        $_GET['searchtext'];


        $query = "SELECT COUNT(*) as num FROM $tableName";
        $total_pages = mysql_fetch_array(mysql_query($query));
        $total_pages = $total_pages['num'];

        $stages = 3;
        $page = mysql_escape_string($_GET['page']);
        if($page){
            $start = ($page - 1) * $limit; 
        }else{
            $start = 0; 
            }   


        // Get page data
        $query1 = "SELECT * FROM $tableName  WHERE jobtitle LIKE     
'%$searchtext%' LIMIT $start, $limit";
        $result = mysql_query($query1);

        // Initial page num setup
        if ($page == 0){$page = 1;}
        $prev = $page - 1;  
        $next = $page + 1;  
        $lastpage = ceil($total_pages/$limit);  
        $LastPagem1 = $lastpage - 1;    
        $paginate = '';
        if($lastpage > 1)
        {       
            $paginate .= "<div class='paginate'>";
            // Previous

            if ($page > 1){
                $paginate.= "<a href='$targetpage?page=$prev'>Previous</a>";
            }else{
                $paginate.= "<span class='disabled'>Previous</span>";   }
            // Pages    
            if ($lastpage < 7 + ($stages * 2))  // Not enough pages to breaking it up
            {   
                for ($counter = 1; $counter <= $lastpage; $counter++)
                {
                    if ($counter == $page){
                        $paginate.= "<span class='current'>$counter</span>";
                    }else{
                        $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}    
                }
            }
            elseif($lastpage > 5 + ($stages * 2))   // Enough pages to hide a few?
            {
                // Beginning only hide later pages
                if($page < 1 + ($stages * 2))   
                {
                    for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
                    {
                        if ($counter == $page){
                            $paginate.= "<span class='current'>$counter</span>";
                        }else{
                            $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}    
                    }
                    $paginate.= "...";
                    $paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
                    $paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";
                }
                // Middle hide some front and some back
                elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
                {
                    $paginate.= "<a href='$targetpage?page=1'>1</a>";
                    $paginate.= "<a href='$targetpage?page=2'>2</a>";
                    $paginate.= "...";
                    for ($counter = $page - $stages; $counter <= $page + $stages; $counter++)
                    {
                        if ($counter == $page){
                            $paginate.= "<span class='current'>$counter</span>";
                        }else{
                            $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}    
                    }
                    $paginate.= "...";
                    $paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
                    $paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";
                }
                // End only hide early pages
                else
                {
                    $paginate.= "<a href='$targetpage?page=1'>1</a>";
                    $paginate.= "<a href='$targetpage?page=2'>2</a>";
                    $paginate.= "...";
                    for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++)
                    {
                        if ($counter == $page){
                            $paginate.= "<span class='current'>$counter</span>";
                        }else{
                            $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
                    }
                }       
            }       
                    // Next
            if ($page < $counter - 1){ 
                $paginate.= "<a href='$targetpage?page=$next'>Next</a>";
            }else{
                $paginate.= "<span class='disabled'>Next</span>";
                }
            $paginate.= "</div>";   
    }

if(mysqli_num_rows($result) >0){
while($row = mysqli_fetch_array($result)) 
{
    /*echo "<table>";
    echo "<tr > <td>Job Title:</td> <td>{$row['jobtitle']} </td>     
</tr>".
        "<tr > <td>Job Description: </td> <td>        
{$row['jobdescription']}</td> </tr> ".
        "<br>";
    echo "</table>"; */
}
}

在添加分页的位置添加以下内容。
echo "<center>".$paginate."</center>";

添加样式表

.paginate {
    font-family:"Times New Roman", Times, serif;
    padding: 3px;
    margin: 3px;
}
.paginate a {
    padding:2px 5px 2px 5px;
    margin:2px;
    border:1px solid #999;
    text-decoration:none;
    color: #666;
}
.paginate a:hover, .paginate a:active {
    border: 1px solid #999;
    color: #0066FF;
}
.paginate span.current {
    margin: 2px;
    padding: 2px 5px 2px 5px;
    border: 1px solid #999;
    font-weight: bold;
    background-color: #999;
    color: #FFF;
}
.paginate span.disabled {
    padding:2px 5px 2px 5px;
    margin:2px;
    border:1px solid #eee;
    color:#DDD;
}

但是不要使用任何mysql_*函数,因为它们现在都已经过时了。 - MaggsWeb
这种方法还不错,尽管每页需要执行两个查询。但是可以通过一个查询返回所有记录、计算总数并显示返回记录的子集来实现。 - MaggsWeb

2
你的问题在于 LIMIT 2,它只会返回两行数据。
你需要做以下几步:
  1. 移除 LIMIT 限制
  2. 计算记录总数
  3. 决定每页显示多少条记录
  4. 在 URL 中传递一个“page”变量
  5. 使用 array_splice() 根据页面编号输出结果的子集
这并不是一件容易的事情。 :)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接