Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7688311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:53:27+00:00 2026-05-31T19:53:27+00:00

I created a pagination for an website file last year, and I’ve never really

  • 0

I created a pagination for an website file last year, and I’ve never really had to use it again.

But now I’ll need to use it in a bigger application, and I’m afraid it won’t behave really good with a huge amount of mysql rows, I don’t know.

I’ll post an example bellow and I’ll be greatful if someone could tell me if I really need to upgrade the code.

/* Connection file */
include( 'config.php' );

/* Get the amount of all rows */
$mqc = "SELECT COUNT(*) AS total FROM table";
$mqe = mysql_query( $mqc );
$mqn = mysql_fetch_array( $mqe );
$total = $mqn['total'];


/* Rows per page */
$perPage = 10;

/* Range for the pagination */
$range = 5;

/* Actual page */
$page = isset( $_GET['page'] ) &&  
            is_numeric( $_GET['page'] ) && 
            !empty( $_GET['page'] ) ? 
            ( (int)$_GET['page'] * $range ) : 0;

/* Query */
$mqc = "SELECT * FROM table 
            LIMIT " . $page . ", " . $perPage . " ";
$mqe = mysql_query( $mqc );

/* Show results */
while( $l = mysql_fetch_object( $mqe ) )
{
    echo $l->id . ' - ' . $l->title . '<br />';
}

/* Function for pagination */
function pages( $total = false, $perPage = false, $page = false, $range = false )
{
    /* Number of pages */
    $totalPages = ( ceil( $total / $perPage ) ) - 1;
    /* The range * 2 */
    $range = $range;
    /* The real param $_GET['page'] */
    $realGetPage = !empty( $page ) ? ( $page / $range ) : 0;

    /* Show the first page */
    if($realGetPage >= 7)
    {
        echo ' <a href="?page=0"> 1 </a> ... ';
    }

    /* Loop to create the range */
    for($i = ( $realGetPage - $range ); $i < ( $range + $realGetPage ); $i++)
    {
        /* Check out if the value isn't outta range */
        if( $i > 0 && $i < $totalPages )
        {
            /* Highlight the actual page */
            if( ( $i - 1 ) == $realGetPage )
            {
                echo ' [ <a href="?page=' . ( $i - 1 ) . '"> ' . $i . ' </a> ] ';
            }
            else
            {
                echo ' <a href="?page=' . ( $i - 1 ) . '"> ' . $i . ' </a> ';
            }
        }

        /* Store the number of the last page */
        $lastPage = ( $i + 1 );
    }

    /* Hack to show 5678910 */
    if( $lastPage < ( $range * 2 ) + 1 )
    {
        for( $j = $lastPage; $j <= ( $range * 2 ); $j++ )
        {
            if( ($j - 1) == $realGetPage )
            {
                echo ' [ <a href="?page=' . ( $j - 1 ) . '"> ' . $j . ' </a> ] ';
            }
            else
            {
                echo ' <a href="?page=' . ( $j - 1 ) . '"> ' . $j . ' </a> ';
            }
        }

    }

    /* Last page */
    echo ' ...  <a href="?page=' . $totalPages . '"> ' . $totalPages . ' </a> ';
}

/* Show the pagination */
pages( $total, $perPage, $page, $range );

What do you think?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T19:53:28+00:00Added an answer on May 31, 2026 at 7:53 pm

    The main thing I see missing is an ORDER BY clause. Without this, your paged results are (potentially) unpredictable.

    Add an ORDER BY clause and make sure you have appropriate database indexes in place on the sortable column(s).

    I’d also recommend switching to a more up-to-date database connection library with prepared statements and bound parameters. Please see PDO.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've created a PHP pagination system, but I want to hide the _GET variable
I have created one .html page. I have to use pagination in it so
I know I could use pagination gems to accomplish a task like this, but
I'm using the following script in my website in order to create pagination next-previous
I would like to create a pagination system for comments in my website.So far,
I am trying to create a dynamic pagination system for my website and I've
I created a google maps store locator and im using a jquery pagination to
I have created an AJAX function that is used with pagination links to load
I've created following stored procedure in my SQL server 2005 database for general pagination:
I created pagination for my search page. Controller - public function action_index() { if(!empty($_GET['search'])){

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.