Skip to content
Advertisement

How to make search results and pagination not disappear on click to next page?

whenever i click next page, my search results disappear and my pagination disappear as well. I want them both to stay whenever i click the page numbers, for example when i click next page, the table data displays straight away. right now i have to search the keyword again to display the page 2 results. Is there a fix to this? i cant seem to figure it out what’s wrong with my code. thanks for any help in advance.

<?php
require_once "db.php";

if(isset($_POST['submit'])){


    $search = $db->real_escape_string($_POST['search']);
    if($search != ""){
        $search = "%" . $search . "%";
    }

    $category = $_POST['category'];

    $targetpage = "search.php";  
        $limit =5;

        $query = "SELECT COUNT(*) as num FROM books WHERE(booktitle LIKE '$search') OR (author LIKE '$search') OR category = '$category'";
        $total_pages = mysqli_fetch_array(mysqli_query($db, $query));
        $total_pages = $total_pages['num'];

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


        // Get page data
        $query1 = "SELECT * FROM books WHERE(booktitle LIKE '$search') OR (author LIKE '$search') OR category = '$category' LIMIT $start, $limit";
        $result = mysqli_query($db, $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($rows = mysqli_fetch_array($result)){
            $au = $rows['author'];
            $bt = $rows['booktitle'];
            $rev = $rows['reserved'];
            if($rev == 'N'){
                echo"<tr><td>$au</td><td>$bt</td><td>$rev</td><td><a href='reserve.php?id=".$rows['isbn']."'>Reserve</a></td></tr>n";
            }
        }
        echo"</table>";
        echo "<center>".$paginate."</center>";
    }
}
?>

Advertisement

Answer

Maybe it could be helped by saving all the stuff you want to keep into a Cookie(s)

setcookie(name, value, expire, path, domain, security);

Name: It is used to set the name of the cookie. Value: It is used to set the value of the cookie. Expire: It is used to set the expiry timestamp of the cookie after which the cookie can’t be accessed. Path: It is used to specify the path on the server for which the cookie will be available. Domain: It is used to specify the domain for which the cookie is available. Security: It is used to indicate that the cookie should be sent only if a secure HTTPS connection exists.

For reading from the cookie:

$_COOKIE['name']

In your case, I would start with

if(!isset($_COOKIE['result'])) {
[load your DB results into the cookie]
} else {
[Display the table (or display any error message?)]
}

We have three different points of look

  1. Asking the DB for each page (it may lags the server)
  2. Loading everything into cookies, doing the user save all this data (Deppends on user network speed and storage size )
  3. Loading everything once, and by JS, show/hide the pages you want or not to show (Requires more RAM from the user)

Edit 1:

PHP

function searchBooks() {
    //saves into a cookie the books that match the conditions
    //(Cookie name: $_COOKIE['results'])
    if (isset($_COOKIE['results'])) {
        unset($_COOKIE['results']); //this way you make sure it won't cause any error when assigning it to the new search
        setcookie('results', null, -1, '/'); 
    }
    $sql =[...] //SQL Returns the and fetchs it...
    i = 0;
    while($rows = mysqli_fetch_array($sql)){
        $array[i]=$rows;
        $i++;
    }
    setcookie('results', $array, 85400);
}

function paginate() {
    $umbral = 10; //the max number of rows per page
    $page = $_GET['page']; //saves the current page into a variable for use it easier
    $disp = ($page-1)*$umbral // this set the displacement for this page
    for (i=$disp; (i<count($_COOKIE['results']) && i<$page*$umbral); i++) {
        echo [your stuff to show the list here];
        echo $_COOKIE['results'][i]['author'];
        echo $_COOKIE['results'][i]['book'];
        $next = $page+1;
        $prev = $page-1;
        echo "<a href=file.php?page='"+$next+"'> Next </a>";
        echo "<a href=file.php?page='"+$prev+"'> Prev </a>";
    }
}

Warning

Please, note that if the cookie expires, and the user wants to load the next page, it will cause an error for do not have the cookie setted, so it’s better to check it when the next page load (probably on paginate() function) and handle that error.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement