0

Here is the pagination code its works fine pagination show like this url mywebsite.com/samplepage.php?page=1 and so on .. code are

<?php
    include"config.php";


function pagination($query,$per_page=10,$page=1,$url='?'){   
    global $conn; 

    $query = "SELECT COUNT(*) as `num` FROM {$query}";
    $row = mysqli_fetch_array(mysqli_query($conn,$query));

    $total = $row['num'];
    $adjacents = "2"; 

    $prevlabel = "&lsaquo; Prev";
    $nextlabel = "Next &rsaquo;";
    $lastlabel = "Last &rsaquo;&rsaquo;";

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

    $prev = $page - 1;                          
    $next = $page + 1;

    $lastpage = ceil($total/$per_page);

    $lpm1 = $lastpage - 1; // //last page minus 1

    $pagination = "";

    if($lastpage > 1){   

        $pagination .= "<ul class='pagination'>";
        $pagination .= "<li class='page_info'>Page {$page} of {$lastpage}</li>";

        if ($page > 1) $pagination.= "<li><a href='{$url}page={$prev}'>{$prevlabel}</a></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}'>{$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}'>{$counter}</a></li>";                    

                }

                $pagination.= "<li class='dot'>...</li>";
                $pagination.= "<li><a href='{$url}page={$lpm1}'>{$lpm1}</a></li>";
                $pagination.= "<li><a href='{$url}page={$lastpage}'>{$lastpage}</a></li>";  

            } elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2)) {

                $pagination.= "<li><a href='{$url}page=1'>1</a></li>";
                $pagination.= "<li><a href='{$url}page=2'>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}'>{$counter}</a></li>";                    
                }

                $pagination.= "<li class='dot'>..</li>";
                $pagination.= "<li><a href='{$url}page={$lpm1}'>{$lpm1}</a></li>";
                $pagination.= "<li><a href='{$url}page={$lastpage}'>{$lastpage}</a></li>";      
            } else {
                $pagination.= "<li><a href='{$url}page=1'>1</a></li>";
                $pagination.= "<li><a href='{$url}page=2'>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'>{$counter}</a></li>";
                    else
                        $pagination.= "<li><a href='{$url}page={$counter}'>{$counter}</a></li>";                    

                }
            }
        }
        if ($page < $counter - 1) {

            $pagination.= "<li><a href='{$url}page={$next}'>{$nextlabel}</a></li>";
                $pagination.= "<li><a href='{$url}page=$lastpage'>{$lastlabel}</a></li>";
        }
        $pagination.= "</ul>";        
    }
    return $pagination;
}
?>

I want to add page number in title like this..

Details - Page 24 OR Details - Page 24 of 200 (total page)

and can i update this pagination code. This code shows whole data from database but if i want to show pagination from single row in table. from table from row "keyword" .. but my main question is how to show page number in title.. thanks in advance

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Sahil
  • 1
  • 1

1 Answers1

0

To add a title to your website you need to add the <title></title> tag in your html head.

You might do something like

<title>Page <?php echo $pagination ?> of <?php echo $total ?></title>

If you want to change it in the fly you can use javascript (link)

document.title = currentPage+" of "+total;
Deben Oldert
  • 114
  • 1
  • 11
  • Page <?php echo $pagination ?> of <?php echo $total ?> i tried this but only " of " shows – Sahil Oct 18 '18 at 13:09
  • You need to declare the variables (`$pagination` and `$total`) before the `` line – Deben Oldert Oct 18 '18 at 13:10
  • i added the above code then (pagination code) and then write .. if i am missing something so can you give me the code so paste that and try again – Sahil Oct 18 '18 at 13:14
  • Create a function that only returns the current page and total number of pages. Your code above return a whole bunch of html with it – Deben Oldert Oct 18 '18 at 13:16
  • We are here to help you with your code, not to write the whole code for you: [How do I ask and answer homework questions?](https://meta.stackoverflow.com/q/334822) – Deben Oldert Oct 18 '18 at 13:38