0

if i load my mypage.php page from database it will display all page numbers of paginated data. Now i need is to display with a pagination pages accord to number or search results when i search it must show the number of pages according to filtered data.

need to fetch next results on next page means page 2 , 3 like that end of the result says last page instead as all pages to show like 1, 2, 3, 4 , 5. kindly help me on it .. my code is below

        <?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);

        ?>
  • thank you very much it worked!.... what about show page number according to filtered data – Irwin Kumwenda Nov 06 '17 at 01:39
  • $search is user input. Your query is not safe because of this. Research mysqli prepared statements with placeholders (SO has pages dedicated exclusively to LIKE queries.) SO also has heaps of pages that deal with pagination. Do lots of research, attempt to self-solve, then if you are still stuck you should update your question and _maybe_ it will be reopened. – mickmackusa Nov 06 '17 at 03:13

0 Answers0