allisius
allisius

Reputation: 415

Why my pagination is not working?

So I was trying to do pagination with php, but I can't quite get it done, there is an error of undefined index page in it somewhere, I have no idea why...here is my code:

        <?php
            $perpage = 10;

            if (empty($_GET['page'])) {
                $page = 1;
            }else{
                $page = $_GET['page'];
            }

            $limitstart = $_GET['page'] - 1 * $perpage;

            $query = "SELECT * FROM images ORDER BY id DESC LIMIT '".$limitstart."', '".$perpage."' ";

            $result = mysqli_query($con,$query) or die(mysqli_error($con));

            while($row=mysqli_fetch_array($result)) {
        ?>

I appreciate your help in any way, thank you.

Upvotes: 0

Views: 50

Answers (1)

Peter van der Wal
Peter van der Wal

Reputation: 11806

$limitstart = $_GET['page'] - 1 * $perpage;

is the same as (remember your math class)

$limitstart = $_GET['page'] - (1 * $perpage);

You would like to use

$limitstart = ($page - 1) * $perpage;

(also note the usage of your $page-variable)

Upvotes: 3

Related Questions