Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
124 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...