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 4036708
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:15:23+00:00 2026-05-20T12:15:23+00:00

I have a SQL query that outputs a bunch of rows that match a

  • 0

I have a SQL query that outputs a bunch of rows that match a certain criteria. I’m trying to add pagination to this process so that it’s easier to use.

My process is like this:

  • Figure out the lower limit of this page, figure out higher limit of this page (ex. 1 – 20, 21 – 40, etc.)
  • Pull those rows from the MySQL table
  • List those rows
  • If there are more than 20, add floor($totalRows / 20) pages
  • ???
  • Pagination

My pagination seems to be working semi-properly, but the last page (whatever it may be) lists one less than the amount of rows it should. Here’s my code:

    $page = $_GET["p"];
    $bL = (($page - 1) * 20) + 1;
    $tL = $page * 20;

    $total = mysql_num_rows(mysql_query("SELECT id FROM coupons WHERE zone = '$zone' AND dateExp > CURDATE()"));

    if($tL > $total)
    {
        $tL = (($page - 1) * 20) + ($total % 20);
    }

    $gotRows = "SELECT * FROM coupons WHERE zone = '$zone' AND dateExp > CURDATE() ORDER BY retailerName LIMIT " . $bL . ", " . $tL;

   //list all those rows

    $numPages = floor($total / 20) + 1;
    echo "<p id='results'>" . $total . " result" . $plural . " &#183; Displaying " . $bL . " to " . $tL . " </p>";
    echo "</table>";
    $pG = 1;
    while($pG <= $numPages)
    {
        if($pG == $page)
        {
            echo $pG;
        }
        else
        {
            echo "<a href='zone?z=" . $zone . "&p=" . $pG . "'>" . $pG . "</a>";
        }
        if($pG != $numPages)
        {
            echo "&#183";
        }
        $pG = $pG + 1;
    }

Any help?

Edit: Nevermind, I solved the rows problem. But my problem with the page links on the bottom being wrong still persists. On page 1, it works properly – “1” is not a link, “2” is. On page 2, however, only “1” as a link shows. “2” doesn’t show.

  • 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-20T12:15:24+00:00Added an answer on May 20, 2026 at 12:15 pm

    This logic confuses me. If $tl is greater than the total, would it not simply be equal to the total?

    if($tL > $total)
    {
        $tL = (($page - 1) * 20) + ($total % 20);
    }
    

    Instead why not:

    $tL = ($tL > $total) ? $total : $tl;
    

    Also, I would use SQL to find your total instead of mysql_num_rows. It should prove a little more reliable (especially when you stop using the really old mysql extension). Maybe it will also return the correct total.

    SELECT COUNT(id) FROM coupons WHERE zone = '$zone' AND dateExp > CURDATE()
    

    Other than this I am not seeing anything obvious that would explain an incorrect number of rows.

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

Sidebar

Related Questions

Guys i have following sql query , that does some mathematical calculation and outputs
I have an sql query that returns rows from two tables with same column
I have an SQL query that takes the following form: UPDATE foo SET flag=true
I have a sql query that runs super fast, around one second, when not
I have a SQL query that takes a very long time to run on
We have a SQL query that pulls a large number of fields from many
I have a SQL query that is supposed to pull out a record and
I have a Linq to SQL query that was working just fine with SQL
I have a result from an SQL query that I would like to sort
I have a query that runs super fast when executed in the sql editor

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.