Logan Young – The Journey to .NET Mastery

November 30, 2011

Paging with PHP

Filed under: Personal — loganyoung @ 9:25 am

I trolled around the interwebz for a good 3 hours the other night looking for this.
I wanted to page through my mysql query’s resultset rather than display what could potentially be thousands of records on a single page. I wanted to limit the results per page to a certain, low number and display “Previous/Next Page” links at the end of that limited count.

Although I have to say that such a task is done very simply in asp.NET, I thoroughly enjoyed the challenge of doing it in PHP.

The first thing we need to do is find where the start of the page will be in the resultset. To do this, we pass a value to use in the mysql query. This value will just contain a number to indicate which page we’re on.

$page = $_GET['page'];
if (!isset($page)) {
header(‘Location: /mysite/index.php?page=1′);
}

$sql = ‘SELECT * FROM articles LIMIT ‘.($page – 1) * 10.’, 10′;
$result = $dbh->prepare($sql);
$result->execute();

foreach ($result->fetch() as $row) {
// Build each
here to display results.
}

$sql = ‘SELECT COUNT(*) FROM articles’
$result = $dbh->execute($sql);
$count = $result->fetch();

if ($page > 1) {
echo ‘Previous‘;
}
if ($count > $page) {
echo ‘Next‘;
}

The code might not be 100% accurate as I just wrote this off the top of my head. I hope it gives you an idea how to do this anyway.

Good luck!

- L

October 16, 2010

Logan’s Archive basic design

Filed under: Uncategorized — loganyoung @ 8:49 pm
Basic design

This is the basic design of the new website with dummy content

September 27, 2010

New Address

Filed under: Uncategorized — loganyoung @ 10:06 am

Hey everybody!

I’ve recently started my web hosting company so I’m going to be moving my blog onto my own domain.
I’ll be designing it myself and I’m really excited to get going, watch this space for more info.

-L

Next Page »

Theme: Rubric. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.