根据搜索结果分页

时间:2017-11-06 01:21:46

标签: php pagination

如果我从数据库加载mypage.php页面,它将显示分页数据的所有页码。现在我需要的是在搜索时显示符合数字或搜索结果的分页页面,它必须根据过滤数据显示页数。

需要在下一页上获取下一个结果意味着第2页,第3页,结果结果显示最后一页,而不是显示所有页面,如1,2,3,4,5。请帮助我..我的代码在下面

        <?php
        include_once("mysqli_connection.php");
        $search = $_GET['search']; 
        $sql = "SELECT COUNT(id) FROM employee";
        $query = mysqli_query($db_conx, $sql);
        $row = mysqli_fetch_row($query);
        // Here we have the total row count
        $rows = $row[0];
        $page_rows = 2;
        // This tells us the page number of our last page
        $last = ceil($rows/$page_rows);
        if($last < 1){
            $last = 1;
        }
        $pagenum = 1;
        // Get pagenum from URL vars if it is present, else it is = 1
        if(isset($_GET['pn'])){
            $pagenum = preg_replace('#[^0-9]#', '', $_GET['pn']);
        }
        if ($pagenum < 1) { 
            $pagenum = 1; 
        } else if ($pagenum > $last) { 
            $pagenum = $last; 
        }
        // This sets the range of rows to query for the chosen $pagenum
        $limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;

        $sql = "SELECT id, name, lastname FROM employee WHERE name LIKE '%".$search."%' $limit";
        $query = mysqli_query($db_conx, $sql);
        $textline1 = "employee (<b>$rows</b>)";
        $textline2 = "Page <b>$pagenum</b> of <b>$last</b>";
        // Establish the $paginationCtrls variable
        $paginationCtrls = '';
        //count search pages 
        if($last != 1){

            if ($pagenum > 1) {
                $previous = $pagenum - 1;
                $paginationCtrls .= '<a class="pagenumber" href="'.$_SERVER['PHP_SELF'].'?pn='.$previous.'&search='.$search.'">Previous</a> &nbsp; &nbsp; ';

                for($i = $pagenum-2; $i < $pagenum; $i++){
                    if($i > 0){
                        $paginationCtrls .= '<a class="pagenumber" href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'&search='.$search.'">'.$i.'</a> &nbsp; ';
                    }
                }
            }
            // Render the target page number, but without it being a link
            $paginationCtrls .= ''.$pagenum.' &nbsp; ';
            for($i = $pagenum+1; $i <= $last; $i++){
                $paginationCtrls .= '<a class="pagenumber" href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'&search='.$search.'">'.$i.'</a> &nbsp; ';
                if($i >= $pagenum+4){
                    break;
                }
            }
            if ($pagenum != $last) {
                $next = $pagenum + 1;
                $paginationCtrls .= ' &nbsp; &nbsp; <a class="pagenumber" href="'.$_SERVER['PHP_SELF'].'?pn='.$next.'&search='.$search.'">Next</a> ';
            }
        }
        $list = '';
        while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
            $id = $row["id"];
            $firstname = $row["name"];
            $lastname = $row["lastname"];
            /*$datemade = $row["datemade"];
            $datemade = strftime("%b %d, %Y", strtotime($datemade));*/
            $list .= '<p><a  href="testimonial.php?id='.$id.'">'.$id.' '.$firstname.' '.$lastname.' Testimonial</a> - Click the link to view this testimonial<br>Written </p>';
        }
  // Close your database connection
        mysqli_close($db_conx);

        ?>

0 个答案:

没有答案