Jayden
Jayden

Reputation: 457

PHP/MySQL SELECT LIMITER issue

I posted this question before but I did not know the cause but now I do. The other question is way out of hand.

I am making a page function (next page etc..) and need my $limiter to select 9 querys per page. The issue I am having is that the second page (9,9) does not come in.

if(isset($_GET["p"]) && is_numeric($_GET["p"]) && $_GET["p"] > 1) {
        $currentPage = $_GET["p"];
        $limiter = $currentPage * 9;
} else {
        $currentPage = 1;
        $limiter = 0;
}

$finalQuery = "SELECT * FROM forum_replies WHERE thread_id = '1' ORDER BY id ASC LIMIT " . $limiter . ",9";

So if page is 1, $limiter is 0. So.. 0,9 comes in

Next should be 9,9 but the code completley skips that so I am missing out on page two

Page three works which is 18,9

So.. it goes 0,9 - 18,9, 27,9 etc.. completely skipping 9,9. What would I add to my if(isset)) function to make it select all if it and not skip 9,9.

Upvotes: 0

Views: 93

Answers (3)

ObiVanKaPudji
ObiVanKaPudji

Reputation: 96

Try changing this:

if(isset($_GET["p"]) && is_numeric($_GET["p"]) && $_GET["p"] > 1) {

to this:

if(isset($_GET["p"]) && is_numeric($_GET["p"]) && $_GET["p"] >= 1) {

And this:

$limiter = $currentPage * 9;

to this:

$limiter = ($currentPage-1) * 9;

because if $currentPage is 1, you want your limits to be 0,9.

Upvotes: 0

Abhipranay Chauhan
Abhipranay Chauhan

Reputation: 126

If i am not wrong i think your calculations for $limiter variable is incorrect. it should be $limiter = ($currentPage - 1) * 9;

if(isset($_GET["p"]) && is_numeric($_GET["p"]) && $_GET["p"] > 1) {
    $currentPage = $_GET["p"];
    $limiter = ($currentPage -1) * 9;
} else {
    $currentPage = 1;
    $limiter = 0;
}

$finalQuery = "SELECT * FROM forum_replies WHERE thread_id = '1' ORDER BY id ASC LIMIT " . $limiter . ",9";

Upvotes: 1

DevZer0
DevZer0

Reputation: 13535

you are missing page 1 because of your if condition.

if(isset($_GET["p"]) && is_numeric($_GET["p"]) && $_GET["p"] > 1) {

this is set to skip the paging logic unless of the page is greater than 1. So it misses page = 1.

based on your code only if p is 0 your code will render 0,9 if the p = 1 then your code renders 9,9

Upvotes: 0

Related Questions