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>';
               
               
                }






?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.