I have a little issue I can't figgure out. I can't seem to figure out how to add dots to my pagination so I don't have more pagination numbers than mysql results in the page XD . This is my code:

$query = "SELECT id, .....";

$records_per_page=10;

$crud->paginglink($query,$records_per_page);

public function paginglink($query,$records_per_page)
    {

        $self = $_SERVER['PHP_SELF'];

        $stmt = $this->db->prepare($query);
        $stmt->execute();

        $total_no_of_records = $stmt->rowCount();

        if($total_no_of_records > 0)
        {
            ?><ul class="pagination"><?php
            $total_no_of_pages=ceil($total_no_of_records/$records_per_page);
            $current_page=1;
            if(isset($_GET["page_no"]))
            {
                $current_page=$_GET["page_no"];
            }
            if($current_page!=1)
            {
                $previous =$current_page-1;
                echo "<li><a href='".$self."?page_no=1'>Inceput</a></li>";
                echo "<li><a href='".$self."?page_no=".$previous."' aria-label='Inapoi'><span aria-hidden='true'>&laquo;</span></a></li>";
            }
            for($i=1;$i<=$total_no_of_pages;$i++)
            {
                if($i==$current_page)
                {
                    echo "<li><a href='".$self."?page_no=".$i."' style='color:red;'>".$i."</a></li>";
                }
                else
                {
                    echo "<li><a href='".$self."?page_no=".$i."'>".$i."</a></li>";
                }
            }
            if($current_page!=$total_no_of_pages)
            {
                $next=$current_page+1;
                echo "<li><a href='".$self."?page_no=".$next."' aria-label='Inainte'><span aria-hidden='true'>&raquo;</span></a></li>";
                echo "<li><a href='".$self."?page_no=".$total_no_of_pages."'>Sfarsit</a></li>";
            }
            ?></ul><?php
        }
    }
Member Avatar for diafol

If this is a class, it's not very OOP.

Anyway:

for($i=1;$i<=$total_no_of_pages;$i++)

Determines how many pages you show. You can change the $total_no_of_pages thing to a minimum of either the actual total no. of pages or a set maximum:

$max_show = 10;
$ellipsis = ($total_no_of_pages > $max_show) ? '...' : '';
$limit = min($max_show, $total_no_of_pages);
for($i=1;$i<=$limit;$i++)

//then attach $ellipsis to the end

But this only shows 10 pages out of x pages. When I reach the 11th the numbers don't change.

Member Avatar for diafol

Sorry I have no idea what you mean or want. Can you show a few examples of the output you want?

Well, here is my cheap and sexy solution that works the way I desire! ^_^

public function paginglink($query,$records_per_page)
    {

        $self = $_SERVER['PHP_SELF'];

        $stmt = $this->db->prepare($query);
        $stmt->execute();

        $total_no_of_records = $stmt->rowCount();

        if(isset($_GET["page_no"]))
            {
                $current_page=$_GET["page_no"];
            }

        if($total_no_of_records > 0)
        {
            ?><ul class="pagination"><?php
            $total_no_of_pages=ceil($total_no_of_records/$records_per_page);
            $current_page=1;

            $ellipsis = ($total_no_of_pages > $max_show) ? '...' : '';

            if(isset($_GET["page_no"]))
            {
                $current_page=$_GET["page_no"];
            }else{
                $current_page=1;
            }
            if($current_page!=1)
            {

                if($current_page > 2 ) {
                    $ellipsis_previous = '<li><a>'.$ellipsis.'</a></li>';
                    $inceput = "<li><a href='".$self."?page_no=1'>Inceput (1)</a></li>";
                }

                $previous =$current_page-1;

                echo $inceput;

                echo "<li><a href='".$self."?page_no=".$previous."' aria-label='Inapoi'><span aria-hidden='true'>&laquo;</span></a></li>";

                echo $ellipsis_previous;

                echo "<li><a href='".$self."?page_no=".$previous."'>".$previous."</a></li>";

            }

            echo "<li><a href='".$self."?page_no=".$current_page."' style='color:red;'>".$current_page."</a></li>";

            if($current_page!=$total_no_of_pages)
            {

                $next=$current_page+1;

                $total_no_of_pages_end = $total_no_of_pages-1;

                if($current_page < $total_no_of_pages_end ) {
                    $ellipsis_next = '<li><a>'.$ellipsis.'</a></li>';
                    $sfarsit = "<li><a href='".$self."?page_no=".$total_no_of_pages."'>Sfarsit (".$total_no_of_pages.")</a></li>";
                }

                echo "<li><a href='".$self."?page_no=".$next."'>".$next."</a></li>";

                echo $ellipsis_next;

                echo "<li><a href='".$self."?page_no=".$next."' aria-label='Inainte'><span aria-hidden='true'>&raquo;</span></a></li>";

                echo $sfarsit;

            }
            ?></ul><?php
        }
    }           
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.