0

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.

moonwave99
  • 21,957
  • 3
  • 43
  • 64
allisius
  • 395
  • 3
  • 18
  • 1
    `empty()` should be `isset()` instead. empty is a pretty much useless function and generally doesn't work as people would expect it to. `$foo = '0'; empty($foo)` evaluates to true, for instance – Marc B Nov 18 '13 at 20:51
  • you don't need quotation for your limit values – steven Nov 18 '13 at 20:52
  • It's not an error, it's a notice, which is *very* different. That has [already been answered here](http://stackoverflow.com/q/4261133/938236). – Francisco Presencia Nov 18 '13 at 20:57

1 Answers1

3

$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)

Peter van der Wal
  • 11,141
  • 2
  • 21
  • 29