Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[TUT] Pagination using PHP
#1
In this tutorial, I will try to show you a way of creating Pagination script for your Content...
For this tutorial, you need an ready table to read from. However if you don't have a table around here is a SQL query command string so you can create a Table...


Here is the complete code, it loads data from the table "rainbow" and splits the information into pages...

PHP Code:
<?php

$db 
= array(
    
"host" => "localhost"# host
    
"user" => "root"# login username
    
"pass" => ""# login password
    
"base" => "tut" # database name
);

if(!
$connect = @mysql_connect($db['host'], $db['user'], $db['pass'])) {
    
# echo mysql_error(), "<br />"; # mysql error
    
die("Not Connected!");
}elseif(!
$database = @mysql_select_db($db['base'])) {
    
# echo mysql_error(), "<br />"; # mysql error
    
die("Database {$db['base']} can not be found!");
}

$rows_per_page 2;

$sql "SELECT `id` FROM `rainbow`";
if(!
$query = @mysql_query($sql)) {
    die(
mysql_error());
}

$rows mysql_num_rows($query);
$pages ceil(($rows $rows_per_page));

mysql_free_result($query);

if(isset(
$_GET['page']) && is_numeric($_GET['page']) && $_GET['page'] > 1) {
    
$page $_GET['page'];
    
$offset = ($rows_per_page $page) - $rows_per_page;
    echo 
$offset;
}else{
    
$page 1;
    
$offset 0;
}

$sql "SELECT * FROM `rainbow` LIMIT {$offset}{$rows_per_page};";
if(!
$query = @mysql_query($sql)) {
    die(
mysql_error());
}

echo 
"Pages: "$pages" Rows: "$rows"<br />";

while(
$row mysql_fetch_array($query)) {
    echo 
"<div><span>"$row['value'], "</span> => <span>"$row['hash'], "</span></div>";
}

$prev $page 1;
$next $page 1;

if(
$page 1) {
    echo 
"<a href=\"page.php?page={$prev}\">Previous</a>";
}

echo 
" | Page: {$page} | ";

if(
$page $pages) {
    echo 
"<a href=\"page.php?page={$next}\">Next</a>";
}

mysql_free_result($query);

mysql_close($connect)

?>

Not let me explain you the code...
The first lines;

PHP Code:
$db = array(
    
"host" => "localhost"# host
    
"user" => "root"# login username
    
"pass" => ""# login password
    
"base" => "tut" # database name
);

if(!
$connect = @mysql_connect($db['host'], $db['user'], $db['pass'])) {
    
# echo mysql_error(), "<br />"; # mysql error
    
die("Not Connected!");
}elseif(!
$database = @mysql_select_db($db['base'])) {
    
# echo mysql_error(), "<br />"; # mysql error
    
die("Database {$db['base']} can not be found!");


Are used to establish a connection to your MySql database and select the give Database, in this case that would be the Database with the name "tut".
The $db array holds the login information to your database, with the function mysql_connect we try to connect to the database, otherwise we kill the script
using the function die.
Next we try to select the database, using the function mysql_select_db... On error we kill the script using the function die again!

So far so good, let us continue with defining how much entries we want to show per page;

PHP Code:
$rows_per_page 2

After that, we can run a query to get the totlat number of entries in the Table.

PHP Code:
$sql "SELECT `id` FROM `rainbow`";
if(!
$query = @mysql_query($sql)) {
    die(
mysql_error());
}

$rows mysql_num_rows($query);
$pages ceil(($rows $rows_per_page));

mysql_free_result($query); 

We execute the query command $sql using the function mysql_query, if the query fails we kill the script using the same function as the one above, die.
Then we declare the variable $rows holding a value of total entries, the function mysq_num_rows will return the number of entries inside of the table "rainbow".
To get the pages we use simple math, $rows divided by $rows_per_page, for the case we get an floating point, we caluclate the pages number using the ceil function..
This function will round up the returned number of our Calculation above...
And laslty we free up the used memory with the function mysql_free_result.

Now that we have the number of Pages, we can continue with checking on which page the user is, that is done over the GET method.

PHP Code:
if(isset($_GET['page']) && is_numeric($_GET['page']) && $_GET['page'] > 1) {
    
$page $_GET['page'];
    
$offset = ($rows_per_page $page) - $rows_per_page;
}else{
    
$page 1;
    
$offset 0;


First we check if the user has requested a page, if the request is a number and if it's over 1.
If all of the above is set, we declare the variable $page, then caluclate the offset for our next query...
To calculate the offset we first multiply $rows_per_page with the requested $page, and then subtract it with the $rows_per_page again so that our offset is set to the right number.

Otherwise we declare $page to 1 and $offset to 0.

We have our pages and the offset, we can now run the query.

PHP Code:
$sql "SELECT * FROM `rainbow` LIMIT {$offset}{$rows_per_page};";
if(!
$query = @mysql_query($sql)) {
    die(
mysql_error());
}

echo 
"Pages: "$pages" Rows: "$rows"<br />"

It's the same command as in our first query, only that this time we use an additional command named, LIMIT.
This will tell our mysql_query function to start loading from $offset and just load the number of $rows_per_page.
If the query fails, we again kill the script.

We can now display our loaded entries, using the function mysql_fetch_array, which runs within a while loop.

PHP Code:
while($row mysql_fetch_array($query)) {
    echo 
"<div><span>"$row['value'], "</span> => <span>"$row['hash'], "</span></div>";


To give our user the ability to browse through the pages, we declare the number of the previous and the next page.

PHP Code:
$prev $page 1;
$next $page 1

Easy huh...

At this point the script is working, we now show the links to the user...

PHP Code:
if($page 1) {
    echo 
"<a href=\"page.php?page={$prev}\">Previous</a>";
}

echo 
" | Page: {$page} | ";

if(
$page $pages) {
    echo 
"<a href=\"page.php?page={$next}\">Next</a>";


We first check if the $page is over 1, if it is we output a Link to the previous page.
Then we show the user on which page s/he is, and finally we check if the $page is under the amount of $pages.
If it is, we show the link to the next page.

Now we just clean some things;

PHP Code:
mysql_free_result($query);

mysql_close($connect

We free up the memory of the last $query and close the connection to mysql using the function mysql_close.

Thank you for reading... Blackhat
Reply
#2
Very interesting and i like it. Good job
Reply
#3
Great tutorial mate. Rep given to reflect as much.
The Rules!
FTW Forum <-- Home of the Damned! --> Join me On MM


Reply
#4
(02-04-2010, 02:11 AM)tartou2 Wrote: Very interesting and i like it. Good job

Thank you! ;)

(02-04-2010, 03:14 AM)DAMINK™ Wrote: Great tutorial mate. Rep given to reflect as much.

lol Thank you very much DAMNIK, glads you like it!
Reply
#5
Hello,

Actually your pagination tut is excellent its very simple to understand, Even the new guys like me also can understand easily it helped me a lot but still i need some more as i would like to have the pagination in as the below format


| First |Previous | 1 | 2 | 3 | 4 | 5 |....| 10 | |Next | Last |

i have seen in this format in this forum it looks better because we can view data easily even it is more. any suggestion to do in this way please
Reply
#6
Interesting way of doing it, I use a different method but this one works just as good. Although this has way more code to it.
(11-23-2010, 08:40 PM)Guest Wrote: Alright, did the 'Support' get removed from SupportForums?
You're not a good Emotional Helper.
Reply
#7
Nice tut. Very interesting and Helpful Smile
Reply
#8
Nice tutorial, very helpful. Thanks!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  PHP Framework List: An Ultimate Guide to 102 PHP Frameworks for Web Developers tk-hassan 0 941 07-27-2020, 11:26 PM
Last Post: tk-hassan
  PHP Video Tutorials (PHP For Beginners) Eleqtriq 4 3,666 10-10-2011, 01:00 PM
Last Post: Greyersting
  [TUT] PHP Shell Scripting Gaijin 8 8,366 08-05-2011, 09:34 PM
Last Post: BreShiE
  [TUT] PHP Password Protected Page PurpleHaze 23 6,969 05-20-2011, 02:56 AM
Last Post: stephen5565
  PHP Tut 4 Minus-Zero 1 925 07-21-2010, 09:39 PM
Last Post: `P R O D I G Y™

Forum Jump:


Users browsing this thread: 2 Guest(s)