You are invited to Log in or Register a free Frihost Account!

Very basic pagination

After having helped Diablosblizz with a very basic pagination script recently, I decided to share this wonderful small piece of code with everyone. This is very lightweight, showing only numbers of pages.

Assuming you currently have a list of for example, users. This list might eventually get very large so you decided to have several pages with a small number of users on each page.
To show them all, you have
$query = 'SELECT * FROM `subs`';
$result = mysql_query($query) or die(mysql_error());
while ($data = mysql_fetch_array($result)) {
  echo '<br /><a href="user.php?id='.$data['id'].'">'.$data['username']."</a> \n";

No adding pagination here is simple. Before this previous code, add...
$per_page = 50; // Number of users to display per page

// Count all of the users
$num_users = mysql_query('SELECT COUNT(*) FROM `members`');
$num_users = mysql_fetch_row($num_users);
$num_users = $num_users[0];
// Number of pages required
$num_pages = ceil($num_users / $per_page);

// What page are we currently on?
$cur_page = isset($_GET['page']) ? $_GET['page'] : 1;
//Whats our starting point?
$start = ($cur_page - 1) * $per_page;

Next, change the current query. Add this after the very end of your query (whether it be select, where, and, order by, etc..)
 LIMIT '.$start.', '.$per_page

Lastly, you'll want to actually show your links to other pages. This can only go after the previous large block of code.
// MORE PAGINATION (the actual page links)
$i = 1;
while($i <= $num_pages) {
  echo '<a href="?page='.$i.'">'.$i.'</a>&nbsp;';

I hope this is useful to some of you Very Happy

6 blog comments below

Very nice mathiaous.. the script actually can help me with a project im working on now that involves a list.. great!
m-productions on Tue Sep 04, 2007 4:50 pm
Great tip! I remember that phpBB uses more or less the same code, just way more complex (they seem to like complex and incomprehensive code).
Bondings on Sat Sep 08, 2007 1:06 am
lol, why can no-one spell my name? Razz

They need to accommodate for lots of people though Bondings. I noticed when I actually had time for frims that rather than being able to use a simple nice of code to suit yourself, a complex piece has to be used to provide several options etc. A hard coded simple piece of code though is much quicker and easier to work with Smile Yay for small and simple!
mathiaus on Mon Sep 10, 2007 10:14 pm
Ahaha! mathiaus, many thanks for that script! To bad I closed down my site because I lost everything and it was time for something new.

I was surprised to see my name. :O! Smile
Diablosblizz on Tue Sep 25, 2007 1:47 am
I expect you know this...but "PHP" is not short for "Pre Hyptertext Processor" (as it says when I click the "PHP" section of your blog), but rather "PHP: Hypertext Preprocessor" - it is a recursive acronym (
LukeakaDanish on Sun Oct 21, 2007 3:06 pm
You expect the 'simpletons' to understand that? Razz
mathiaus on Sun Oct 21, 2007 5:04 pm

© 2005-2011 Frihost, forums powered by phpBB.