Jump to content

Need help improve php pagination with ellipsis


malakiahs

Recommended Posts

Please feel free to use this code in any way if you need to:

I will appreciate any help in rewriting this code to improve it by showing ellipsis.

 

The way the code is now shows this:

Previous 1 2 3 4 5 6 7 8 9 10 Next

 

I would like some help in rewriting the code so that we can get an ellipsis and show something like this:

Previous 1 ... 4 5 6 7 ... 10 Next

 

Please post your improved version of this code (showing the ellipsis).

I would like for it to work when sorting as well thats why the ' &sort=' . $sort .  is included in the code.

 

 

Thank you in advance.

 

 

 

 

<?php
//Number of records from query to display per page
$display = 20 ;
       
//Write your code to sort in here and store it $sort

if ( isset($_GET['np'])) { // Already been determined.
       
        $num_pages = $_GET['np'];
       
        } else {
                //Now we count the number of records in the query
               
                $query = "SELECT COUNT(*) FROM postings ORDER BY posted_date DESC";
                $result = mysql_query($query);
                $row = mysql_fetch_array($result, MYSQL_NUM);
                $num_records = $row[0];
               
                //Now we calculate the number of pages
               
                if ($num_records > $display) {  //More than 1 page
                        $num_pages = ceil ($num_records/$display);
                } else {
                        $num_pages = 1;
                       
                       
                        }
               
               
                } // End of np IF
       
       
        //Determine where in the database to start returning results
       
        if (isset($_GET['s'])) {
                $start = $_GET['s'];
               
               
                } else {
                        $start = 0;
                       
                        }

     
           //Add code for query here
           $query = //whatever you need from the database tables

      while { // show the results from query here

        }

if ($num_pages > 1) {
                echo '<br /><p>' ;
               
                $current_page = ($start/$display) + 1 ;
               
                //If it is not the first page, then we make a previous button.
               
                if ($current_page != 1 ) {
                        echo ' <a href="viewpostings.php?s=' . ($start - $display) . '&np=' . $num_pages . ' &sort=' . $sort . '">Previous </a>';
                       
                        }
               
                //Make all the numbered pages.
               
                for ($i = 1; $i <= $num_pages; $i++) {
                       
                        if ($i != $current_page) {
                                echo '<a href="viewpostings.php?s=' . (($display * ($i - 1 ))) . '&np=' . $num_pages . ' &sort=' . $sort . '"> ' . $i . ' </a>';
                               
                                } else {
                                        echo $i. ' ';
                                       
                                       
                                        }
                       
                }
               
                //If it is not the last page, then we make a Next button;
                if ($current_page != $num_pages) {
                echo '<a href="viewpostings.php?s=' . ($start + $display) . '&np=' . $num_pages . ' &sort=' . $sort . '">Next</a>';
                }
                echo '</p>';
               
               
                }






?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.