The pagination works fine if I dont use any search queries. And when I use search query the first page shows correct result but a problem occurs whenever I click the 'Next page' because it will show the values of the database instead of the continuation of the searched values.

Please do help me..Thanks

here's the code..

<?php

/**
 * @link: http://www.Awcore.com/dev
 */

   function pagination($query, $per_page = 10,$page = 1, $url = '?'){
            $query = "SELECT COUNT(*) as `num` FROM {$query}";
            $row = mysql_fetch_array(mysql_query($query));
            $total = $row['num'];
        $adjacents = "2";

            $page = ($page == 0 ? 1 : $page);
            $start = ($page - 1) * $per_page;

            $prev = $page - 1;
            $next = $page + 1;
        $lastpage = ceil($total/$per_page);
            $lpm1 = $lastpage - 1;

            $pagination = "";
            if($lastpage > 1)
            {
                    $pagination .= "<ul class='pagination'>";
                    $pagination .= "<li class='details'>Page $page of $lastpage</li>";
                    if ($lastpage < 7 + ($adjacents * 2))
                    {
                            for ($counter = 1; $counter <= $lastpage; $counter++)
                            {
                                    if ($counter == $page)
                                            $pagination.= "<li><a class='current'>$counter</a></li>";
                                    else
                                            $pagination.= "<li><a href='{$url}page=$counter' onclick='showHide('signup');return true;'>$counter</a></li>";
                            }
                    }
                    elseif($lastpage > 5 + ($adjacents * 2))
                    {
                            if($page < 1 + ($adjacents * 2))
                            {
                                    for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
                                    {
                                            if ($counter == $page)
                                                    $pagination.= "<li><a class='current'>$counter</a></li>";
                                            else
                                                    $pagination.= "<li><a href='{$url}page=$counter' onclick='showHide('signup');return true;'>$counter</a></li>";
                                    }
                                    $pagination.= "<li class='dot'>...</li>";
                                    $pagination.= "<li><a href='{$url}page=$lpm1' onclick='showHide('signup');return true;'>$lpm1</a></li>";
                                    $pagination.= "<li><a href='{$url}page=$lastpage' onclick='showHide('signup');return true;'>$lastpage</a></li>";
                            }
                            elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
                            {
                                    $pagination.= "<li><a href='{$url}page=1' onclick='showHide('signup');return true;'>1</a></li>";
                                    $pagination.= "<li><a href='{$url}page=2' onclick='showHide('signup');return true;'>2</a></li>";
                                    $pagination.= "<li class='dot'>...</li>";
                                    for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
                                    {
                                            if ($counter == $page)
                                                    $pagination.= "<li><a class='current'>$counter</a></li>";
                                            else
                                                    $pagination.= "<li><a href='{$url}page=$counter' onclick='showHide('signup');return true;'>$counter</a></li>";
                                    }
                                    $pagination.= "<li class='dot'>..</li>";
                                    $pagination.= "<li><a href='{$url}page=$lpm1' onclick='showHide('signup');return true;'>$lpm1</a></li>";
                                    $pagination.= "<li><a href='{$url}page=$lastpage' onclick='showHide('signup');return true;'>$lastpage</a></li>";
                            }
                            else
                            {
                                    $pagination.= "<li><a href='{$url}page=1' onclick='showHide('signup');return true;'>1</a></li>";
                                    $pagination.= "<li><a href='{$url}page=2' onclick='showHide('signup');return true;'>2</a></li>";
                                    $pagination.= "<li class='dot'>..</li>";
                                    for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
                                    {
                                            if ($counter == $page)
                                                    $pagination.= "<li><a class='current' onclick='showHide('signup');return true;'>$counter</a></li>";
                                            else
                                                    $pagination.= "<li><a href='{$url}page=$counter' onclick='showHide('signup');return true;'>$counter</a></li>";
                                    }
                            }
                    }

                    if ($page < $counter - 1){
                            $pagination.= "<li><a href='{$url}page=$next' onclick='showHide('signup');return true;'>Next</a></li>";
                $pagination.= "<li><a href='{$url}page=$lastpage' onclick='showHide('signup');return true;'>Last</a></li>";
                    }else{
                            $pagination.= "<li><a class='current' onclick='showHide('signup');return true;'>Next</a></li>";
                $pagination.= "<li><a class='current' onclick='showHide('signup');return true;'>Last</a></li>";
            }
                    $pagination.= "</ul>\n";
            }


        return $pagination;
    }
?>

Recommended Answers

All 6 Replies

Member Avatar for diafol

Is this is a ready-made script. If so, have you tried the author? There's a forum, "Configuring Readymade Scripts" too, if this is part of a bigger project like a well-known cms, BB etc.

I don't think it is, because for example $prev is set but never used.

Actually I copied and edited this code script but I dont remember where I got it. But any way just need any help on the problem I posted above.Thanks in advance.

here's the code that calls the above function from the previous php file..

<?php

pagination($statement,$limit,$page);
?>

I have a gut feeling that the problem may be in the code calling this function, not the function itself (unless you made some changes to it).

I dont think so. Because whenever I use a select query it shows the correct values but when I will use a Search Query, the first page shows the correct output but it wont if I will click Page 2 instead it shows the page 2 of all the values of the database.

Yes of course.. i had the same problem too.. Bcz.. When using search query it will get the result what we wish.. But when user clicks the 'prev' or 'next' button.. the page will get refresh, that means the page wil skip like, searchresult.php?page=1,2,3..
So the posting data variables will lose their values.. so the search query will not work longer when click next.. i that case i used ajax,PHP.. Unsatisfied working.. Any help??

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.