PHPjabbers - tools for webmasters
shopping cart (0 item)
HOME
PRODUCTS
PHP TUTORIALS
PHP FORUM
ABOUT US
CONTACT US
Our products

PHP / MySQL select data and split on pages

This tutorial is going to show you how to SELECT data from a MySQL database, split it on multiple pages and display it using page numbers.

We have MySQL table called “students” holding 90 records with the following fields:
Name – varchar(250)
PhoneNumber – varchar(250)
Instead of doing a single SELECT query and display all the 90 records on a single page we can have 5 pages each containing maximum 20 records. To do this we will need to use the LIMIT clause for SELECT command so we can limit the query to show only 20 records. The LIMIT clause also allows you to specify which record to start from. For example this query

$sql = "SELECT * FROM students ORDER BY name ASC LIMIT 0, 20";

returns 20 records sorted by name starting from the first record. This next query

$sql = "SELECT * FROM students ORDER BY name ASC LIMIT 50, 20";

shows 20 records sorted again by name but this time it will start from the 50th record.
So basically in this clause (LIMIT start, count) ‘start’ specify the starting record and ‘count’ specifies how many records to show.

Next thing to do is to make a PHP file called pagination.php which will show the first 20 records from our table. The code below selects and then prints the data in a table.

<?php 
if (isset($_GET["page"])) { $page  = $_GET["page"]; } else { $page=1; }; 
$start_from = ($page-1) * 20; 
$sql = "SELECT * FROM students ORDER BY name ASC LIMIT $start_from, 20"; 
$rs_result = mysql_query ($sql, $connection); 
?> 
<table>
<tr><td>Name</td><td>Phone</td></tr>
<?php 
while ($row = mysql_fetch_assoc($rs_result)) { 
?> 
            <tr>
            <td><? echo $row["Name"]; ?></td>
            <td><? echo $row["PhoneNumber"]; ?></td>
            </tr>
<?php 
}; 
?> 
</table>

Now, when you open pagination.php in your web browser you will see table showing the first 20 records from your ‘students’ table.

The first 2 lines of the above code

if (isset($_GET["page"])) { $page  = $_GET["page"]; } else { $page=1; }; 
$start_from = ($page-1) * 20;

are used to create a $start_from variable depending on the page that we want to view. Later you will see that we will pass a “page” value using the URL (e.g. pagination.php?page=2) to go to different pages. Next we need to find out the total amount of records in our table and the number of pages that we will need. To do this we run another query using COUNT() function.

$sql = "SELECT COUNT(Name) FROM students"; 
$rs_result = mysql_query($sql,$connection); 
$row = mysql_fetch_row($rs_result); 
$total_records = $row[0];

The $total_records is now equal to the number of records that we have in our database, in our case 90. We have 20 records per page so the total number of pages that will be needed is 5 (4 pages with 20 records and last page will have 10 records).

Calculating the amount of pages needed using PHP can be done using ceil() function.

$total_pages = ceil($total_records / 20);
We divide the total number of records by records per page and then the ceil() function will round up the result. Now we have 2 new variables - $total_records equal to 90 and $total_pages equal to 5.

To print page numbers and associate URLs to each number we will use for() cycle.

<?php 
for ($i=1; $i<=$total_pages; $i++) { 
	echo "<a href='pagination.php?page=".$i."'>".$i."</a> "; 
}; 
?>

Above code will print numbers from 1 to 5 and for each number will create different link.
pagination.php?page=1
pagination.php?page=2
pagination.php?page=3
pagination.php?page=4
pagination.php?page=5
as you can see each link passes different page value which is used in the SELECT query above.

At the end you should have a file like this (remember to add the MySQL connection string):

<?php 
if (isset($_GET["page"])) { $page  = $_GET["page"]; } else { $page=1; }; 
$start_from = ($page-1) * 20; 
$sql = "SELECT * FROM students ORDER BY name ASC LIMIT $start_from, 20"; 
$rs_result = mysql_query ($sql,$connection); 
?> 
<table>
<tr><td>Name</td><td>Phone</td></tr>
<?php 
while ($row = mysql_fetch_assoc($rs_result)) { 
?> 
            <tr>
            <td><? echo $row["Name"]; ?></td>
            <td><? echo $row["PhoneNumber"]; ?></td>
            </tr>
<?php 
}; 
?> 
</table>
<?php 
$sql = "SELECT COUNT(Name) FROM students"; 
$rs_result = mysql_query($sql,$connection); 
$row = mysql_fetch_row($rs_result); 
$total_records = $row[0]; 
$total_pages = ceil($total_records / 20); 
 
for ($i=1; $i<=$total_pages; $i++) { 
            echo "<a href='pagination.php?page=".$i."'>".$i."</a> "; 
}; 
?>

This pagination.php file will print a table with maximum 20 records per page and at the bottom 5 page numbers each pointing to a page showing different 20 records.

Do not forget that for a small fee I can add pagination to all your PHP files. Let me know if you need help with this and I will give you a quote.

COMMENTS
adams

pls send me a code on how i can print a php web page div content on A4. thanks every body
monty

please tell me simple showing table on browser script using php....
the table from mysql db of only 1 field should be arranged by bullets in browser
Thomas

@Jason suggesting mysql_num_rows:
This is NOT a better way of doing it, because the mysql server will transfer the entire table to your PHP script, which is a great waste of bandwidth and will also cause serious performance problems on large tables.
Using COUNT only transfers the number of rows from mysql to your php script.
seesaw

how about if you want to view the row in table as a select(like a choice) or drag down?
Alex

BIG THANKS to everyone who's been part of my successful page built!!!!
ket

Hey man this snippet works perfectly with MySQLi query too.

Good job
ddas

<?php

if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * 20;
$sql = "SELECT * FROM products ORDER BY id ASC LIMIT $start_from, 20";
$rs_result = mysql_query ($sql,$db);

while ($row = mysql_fetch_assoc($rs_result))
{

// place here the data your want to display

};

$sql = "SELECT COUNT(id) FROM products"; // change id and products if needed
$rs_result = mysql_query($sql,$db);
$row = mysql_fetch_row($rs_result);
$total_records = $row[0];
$total_pages = ceil($total_records / 20);

echo "<table class=hidden><tr><td>"; // define class if needed
echo "<span class='hidden'>"; // define class if needed

$pagelink ='<a href="xxxxxxx.php?site=results&page='.($page-1).'"><< prev</a> '; // xxxx = your page url address
$pagelink_2='<a href="xxxxxx.php?site=results&page='.($page+1).'">next >></a> '; // xxxx = your page url address

if($page>1)
echo $pagelink;

if($page<2)
echo "";

for ($i=1; $i<=$total_pages; $i++)
{
if ($i != $page)
echo "<a href='xxxxxxx.php?page=".$i."'>".$i."</a> "; // xxxx = your page url address
if ($i==$page)
echo " <span class='page_frame'>". $i . "</span> "; // defining class in the style sheet you can add colour or border to the displayed number
};

if($page<$total_pages)
echo $pagelink_2;

echo "</span>";
echo "</td></tr></table>";

?>
webfun

what is the value of $connection variable..it's not working for some cell value..please help...
Dorin

Sorina. Thanx for the script, that was exactly what i needed (showing current page number without a link). Although it was need to make some small changes. So here all the script again (change links , etc.)

<?php

if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * 20;
$sql = "SELECT * FROM products ORDER BY id ASC LIMIT $start_from, 20";
$rs_result = mysql_query ($sql,$db);

while ($row = mysql_fetch_assoc($rs_result))
{

// place here the data your want to display

};

$sql = "SELECT COUNT(id) FROM products"; // change id and products if needed
$rs_result = mysql_query($sql,$db);
$row = mysql_fetch_row($rs_result);
$total_records = $row[0];
$total_pages = ceil($total_records / 20);

echo "<table class=hidden><tr><td>"; // define class if needed
echo "<span class='hidden'>"; // define class if needed

$pagelink ='<a href="xxxxxxx.php?site=results&page='.($page-1).'"><< prev</a> '; // xxxx = your page url address
$pagelink_2='<a href="xxxxxx.php?site=results&page='.($page+1).'">next >></a> '; // xxxx = your page url address

if($page>1)
echo $pagelink;

if($page<2)
echo "";

for ($i=1; $i<=$total_pages; $i++)
{
if ($i != $page)
echo "<a href='xxxxxxx.php?page=".$i."'>".$i."</a> "; // xxxx = your page url address
if ($i==$page)
echo " <span class='page_frame'>". $i . "</span> "; // defining class in the style sheet you can add colour or border to the displayed number
};

if($page<$total_pages)
echo $pagelink_2;

echo "</span>";
echo "</td></tr></table>";

?>
sg

Sorina. Thanx for the page showing current page number without a link, but it was need for small changes. So here is all the script again (change the links etc.)

<?php

if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * 20;
$sql = "SELECT * FROM products ORDER BY id ASC LIMIT $start_from, 20";
$rs_result = mysql_query ($sql,$db);

while ($row = mysql_fetch_assoc($rs_result))
{

// place here the data your want to display

};

$sql = "SELECT COUNT(id) FROM products"; // change id and products if needed
$rs_result = mysql_query($sql,$db);
$row = mysql_fetch_row($rs_result);
$total_records = $row[0];
$total_pages = ceil($total_records / 20);

echo "<table class=hidden><tr><td>"; // define class if needed
echo "<span class='hidden'>"; // define class if needed

$pagelink ='<a href="xxxxxxx.php?site=results&page='.($page-1).'"><< prev</a> '; // xxxx = your page url address
$pagelink_2='<a href="xxxxxx.php?site=results&page='.($page+1).'">next >></a> '; // xxxx = your page url address

if($page>1)
echo $pagelink;

if($page<2)
echo "";

for ($i=1; $i<=$total_pages; $i++)
{
if ($i != $page)
echo "<a href='xxxxxxx.php?page=".$i."'>".$i."</a> "; // xxxx = your page url address
if ($i==$page)
echo " <span class='page_frame'>". $i . "</span> "; // defining class in the style sheet you can add colour or border to the displayed number
};

if($page<$total_pages)
echo $pagelink_2;

echo "</span>";
echo "</td></tr></table>";

?>
 
POST A COMMENT
Your name:
Your email: (email address will not be posted on the website)
Comment:
Verification code:
type characters on the image in the text box