0

I am trying to add pagination to my CRUD PHP app but it's not working properly. Trying to limit the data from my SQL database to 5 per page, but it's getting stuck to the first page. If I press the second one, stays on the first page data.

$page = (isset($_GET['page']) ? $_GET['page'] : 1);
$perPage = (isset($_GET['per-page']) && ($_GET['per-page']) <= 50 ? $_GET['per-page'] : 5);
$start = ($page > 1) ? ($page * $perPage) - $perPage : 0;

$sql = "select * from movies limit ".$start." , ".$perPage." ";

$total = $db->query("select * from movies")->num_rows;

$pages = ceil($total / $perPage);

And here I am looping through the pages in the HTML code below the PHP one.

<center>
                        <ul class="pagination">
                            <?php for($i = 1; $i <= $pages; $i++): ?>
                            <li>
                                <a href="?pages = <?php  echo $i; ?>"><?php echo $i; ?></a>
                            </li>
                        <?php endfor; ?>
                        </ul>
                    </center>

I am using bootstrap class for pagination which is added with a script in the head. Any idea why I stay at the first page even when I press the second one?

Here is a photo from my web project, in the url bar u can see i am at the second page, but still showing the data from first. In my database i have 10 elements, so it should be 5 and 5.

paging

TimoSolo
  • 7,068
  • 5
  • 34
  • 50

1 Answers1

1

Looks like you are using the wrong variable name in your link. It should be:

<a href="?page=<?php  echo $i; ?>">

(note, page not pages. Also, remove the spaces which get encoded as %20 in the url)

Be vary careful of putting user-submitted data into your query though, google "SQL injection" to find out more.

TimoSolo
  • 7,068
  • 5
  • 34
  • 50
  • 1
    Thanks for the advice, it worked right away with your correction. Must have slipped my eyes that letter. Thanks again! – Iancu Jianu Jan 06 '20 at 15:37