PHP pagination help. Are there are good books or videos on the topic?

I’m pretty close to finishing my own simple blog script. Will then move on to this other complex one I had shelved. All I need for now is to learn pagination and I’ll be diving into OOP in detail.

Are there any good books, videos or resources that can teach me pagination from scratch? I’d like to be able to retrieve a certain amount of data from the mysql database on the main page and then go in for a button system (1,2,3,4,5) or maybe just a simple next button to get the next page.

Thanks. Any help will be greatly appreciated.

I’ve managed.

Here is the code for anybody that needs it.

// setting a variable and an algorithm for use in the sql query string

    if (isset ($_GET['page'])) {
    $page = $_GET['page'];
    if ($page == '' || $page == '1')
    else {	
    	$page1 = ($page*5)-5;

// the actual sql query string to retrieve results

    	$sql = "SELECT * from data order by id DESC limit $page1,5";
    	$results = $pdo->query($sql);
    	$data = $results->fetchAll(PDO::FETCH_OBJ);
// get count for pagination
    	$sql2 = "SELECT * from data";
    	$results2 = $pdo->query($sql2);
    	$data2 = $results2->fetchAll(PDO::FETCH_OBJ);
    	$cou = count($data2);
    	$a = ($cou/5);    // count divided by 5 because we want 5 results per page
    	$a = ceil ($a);	// using ceiling function to round it up
// use of for statement to get the numbering and echoing $b from the for statement into the anchor tag
for($b=1; $b<=$a; $b++)
    		echo '<a href="?page=' .$b .'">' . $b .'</a>';

I’ve now been unsuccessfully trying to put in place next and previous buttons.
Any ideas?

I would think that your method of getting the count for pagination could run into performance issues when you get more posts in the database. Right now you’re selecting all columns from the database, retrieving all rows, sticking them in an array, then counting the contents of the array. Instead I would suggest

$sql2 = "SELECT COUNT(id) from data";
$results2 = $pdo->query($sql2);
$cou = $results2->fetch();

Can’t help you on the next/previous thing, but run down the forum a bit, there was a post just before Christmas about it. Need to consider when you have missing ids.

Yes, I have managed a bit of it that includes adding an anchor tag with

?next=<?php echo $_GET ['page'] +1; ?>

But then again, you have problems when the ids run out. And same for the previous button with a minus sign . Probably gonna have to use something more complex. Tried playing around a bit more like:

if ($_GET [‘page’] == 0) {
$_GET [‘page’] ==1;

but that messes up the landing page for some reason even though there is no page number specified. I was thinking it would take effect only if there was $_GET[‘page’] specified.

Thanks for the tip on pagination.

no need to use php function because already there is an option in bootstrap table i.e pagination table concept

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.