Trill Designs: Simple Pagination - Trill Designs

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Simple Pagination

#1 User is offline   Jaan Icon

  • Site Admin
  • Icon
  • Group: Administrators
  • Posts: 84
  • Joined: 23-November 09
  • LocationEstonia

Posted 25 January 2010 - 09:00 PM

Hey there.. It has been a long time since I wrote a tutorial.. but now I thought I'll do it.. So let's start :)

First we have to connect to our database:

// Connect to your database
$mysql_con = mysql_connect("localhost", "Username", "Password");

// Let's check if there were any errors
if(!$mysql_con){
	
	// If there was ań error, if there was, lets display an error
	die(mysql_error());
	
}

// Select your database
$mysql_db = mysql_select_db("MyDatabase");

// If there were any errors with selecting your database, display an error
if(!$mysql_db){
	
	die(mysql_error());
	
}


Now we are connected to database.. Let's get our information.
First we need to get our page number:

// If the page wasn't set, lets set $page to number 1 for the first page
if($page == ""){
	
	$page = "1";
	
}else{
	
	// If page is set, lets get it
	$page = $_GET['page'];
	
}


Now we need to get all messages from database and count them:

// Now lets get all messages from your database
$sql = "SELECT * FROM messages";
$query = mysql_query($sql);

// Lets count all messages
$num = mysql_num_rows($query);


When these things are done.. lets set our numbers:

// Lets set how many messages we want to display
$per_page = "10";

// Now we must calculate the last page
$last_page = ceil($num/$per_page);

// And set the first page
$first_page = "1";


Now it's time for "Next" , "Previous" First and last page.. :) :

// Here we are making the "First page" link
echo "<a href='?page=".$first_page."'>First page</a> ";

// If page is 1 then remove link from "Previous" word
if($page == $first_page){
	
	echo "Previous ";
	
}else{
	
	if(!isset($page)){
		
		echo "Previous ";
		
	}else{
		
		// But if page is set and it's not 1.. Lets add link to previous word to take us back by one page
		$previous = $page-1;
		echo "<a href='?page=".$previous."'>Previous</a> ";
	
	}
	
}

// If the page is last page.. lets remove "Next" link
if($page == $last_page){
	
	echo "Next ";	
	
}else{
	
	// If page is not set or it is set and it's not the last page.. lets add link to this word so we can go to the next page
	if(!isset($page)){
		
		$next = $first_page+1;
		echo "<a href='?page=".$next."'>Next</a> ";
		
	}else{
	
		$next = $page+1;
		echo "<a href='?page=".$next."'>Next</a> ";
	
	}
	
}

// And now lets add the "Last page" link
echo "<a href='?page=".$last_page."'>Last page</a>";


Posted Image Posted Image

Now lets do some math and get our messages :

// Math.. It gets us the start number of message that will be displayed
$start = ($page-1)*$per_page;

// Now lets set the limit for our query
$limit = "LIMIT $start, $per_page";

// It's time for getting our messages
$sql = "SELECT * FROM messages $limit";
$query = mysql_query($sql);

echo "<br /><br />";

// And lets display our messages
while($row = mysql_fetch_array($query) or die(mysql_error())){
	
	echo $row['message']."<br />";
	
}


Voila.. it wasn't that hard right ? :)

Here's the full code with comments:

<?php

// Connect to your database
$mysql_con = mysql_connect("localhost", "Username", "Password");

// Let's check if there were any errors
if(!$mysql_con){
	
	// If there was ań error, if there was, let's display an error
	die(mysql_error());
	
}

// Select your database
$mysql_db = mysql_select_db("MyDatabase");

// If there were any errors with selecting your database, display an error
if(!$mysql_db){
	
	die(mysql_error());
	
}

// If the page wasn't set, lets set $page to number 1 for the first page
if($page == ""){
	
	$page = "1";
	
}else{
	
	// If page is set, let's get it
	$page = $_GET['page'];
	
}

// Now lets get all messages from your database
$sql = "SELECT * FROM messages";
$query = mysql_query($sql);

// Lets count all messages
$num = mysql_num_rows($query);

// Lets set how many messages we want to display
$per_page = "10";

// Now we must calculate the last page
$last_page = ceil($num/$per_page);

// And set the first page
$first_page = "1";

// Here we are making the "First page" link
echo "<a href='?page=".$first_page."'>First page</a> ";

// If page is 1 then remove link from "Previous" word
if($page == $first_page){
	
	echo "Previous ";
	
}else{
	
	if(!isset($page)){
		
		echo "Previous ";
		
	}else{
		
		// But if page is set and it's not 1.. Lets add link to previous word to take us back by one page
		$previous = $page-1;
		echo "<a href='?page=".$previous."'>Previous</a> ";
	
	}
	
}

// If the page is last page.. lets remove "Next" link
if($page == $last_page){
	
	echo "Next ";	
	
}else{
	
	// If page is not set or it is set and it's not the last page.. lets add link to this word so we can go to the next page
	if(!isset($page)){
		
		$next = $first_page+1;
		echo "<a href='?page=".$next."'>Next</a> ";
		
	}else{
	
		$next = $page+1;
		echo "<a href='?page=".$next."'>Next</a> ";
	
	}
	
}

// And now lets add the "Last page" link
echo "<a href='?page=".$last_page."'>Last page</a>";

// Math.. It gets us the start number of message that will be displayed
$start = ($page-1)*$per_page;

// Now lets set the limit for our query
$limit = "LIMIT $start, $per_page";

// It's time for getting our messages
$sql = "SELECT * FROM messages $limit";
$query = mysql_query($sql);

echo "<br /><br />";

// And lets display our messages
while($row = mysql_fetch_array($query) or die(mysql_error())){
	
	echo $row['message']."<br />";
	
}

?>


I hope it helped you a little :)

Regards,
Jaan
0

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users