Hi,
I'm new on this forum, this is my first post. I think that this is best way to introduce myself. And, sorry on my nasty english.
For this example we need MySql database with at least one table.
Table :
Code:
CREATE TABLE `products` (
`id` int(10) unsigned NOT NULL auto_increment,
`name` varchar(255) NOT NULL default '',
`price` decimal(10,2) NOT NULL default '0.00',
PRIMARY KEY (`id`)
) TYPE=MyISAM;
INSERT INTO `products` VALUES ('','Product 1','19.95');
INSERT INTO `products` VALUES ('','Product 2','24.95');
INSERT INTO `products` VALUES ('','Product 3','34.95');
INSERT INTO `products` VALUES ('','Product 4','16.95');
INSERT INTO `products` VALUES ('','Product 5','8.95');
INSERT INTO `products` VALUES ('','Product 6','14.95');
INSERT INTO `products` VALUES ('','Product 7','15.95');
INSERT INTO `products` VALUES ('','Product 8','19.95');
INSERT INTO `products` VALUES ('','Product 9','6.95');
INSERT INTO `products` VALUES ('','Product 10','4.95');
INSERT INTO `products` VALUES ('','Product 11','9.95');
INSERT INTO `products` VALUES ('','Product 12','6.95');
INSERT INTO `products` VALUES ('','Product 13','19.95');
INSERT INTO `products` VALUES ('','Product 14','49.95');
INSERT INTO `products` VALUES ('','Product 15','9.95');
INSERT INTO `products` VALUES ('','Product 16','29.95');
INSERT INTO `products` VALUES ('','Product 17','9.95');
INSERT INTO `products` VALUES ('','Product 18','9.95');
INSERT INTO `products` VALUES ('','Product 19','14.95');
We have table. Also we need just one php file (call it index.php):
[php]<?php
$s_SERVERNAME = 'localhost'; // MySql Server Name (or IP add.)
$db_USERNAME =''; // Your user id
$db_PASSWORD =''; // Your password
$db_NAME =''; // Your db name
$db_TABLE ='products'; // Table with data
$i_ITEMS_PER_PAGE = 4;
$dbLink = mysql_connect ($s_SERVERNAME,$db_USERNAME,$db_PASSWORD);
if(!$dbLink) die('Could not connect to MySQL');
mysql_select_db($db_NAME, $dbLink) or die ('Could not open database');
$iNoOfItemsInTable = mysql_result(mysql_query("SELECT COUNT(*) FROM $db_TABLE"),0);
$iTotalNoOfPages = ceil($iNoOfItemsInTable / $i_ITEMS_PER_PAGE);
if(!isset($_GET['page'])) {
$iCurrentPageNo = 1;
} else {
$iCurrentPageNo = (int)$_GET['page'];
if($iCurrentPageNo < 1 || $iCurrentPageNo > $iTotalNoOfPages) {
$iCurrentPageNo = 1;
}
}
$iFrom = (($iCurrentPageNo * $i_ITEMS_PER_PAGE) - $i_ITEMS_PER_PAGE);
$sOutput = '<html><title>Simple Pagging</title><body>';
$query = "SELECT * FROM $db_TABLE ORDER BY id LIMIT $iFrom, $i_ITEMS_PER_PAGE";
$result = mysql_query($query);
if (mysql_num_rows( $result ) > 0) {
$sOutput .= '<table border="1" align="center"><tr><th>ID</th><th>Name</th><th>Price</th></tr>';
while($aProduct = mysql_fetch_array($result)) {
$sOutput .= '<tr><td>'.$aProduct['id'].'</td><td>'.$aProduct['name'].'</td><td>'.$aProduct['price'].'</td></tr>';
}
$sOutput .='</table>';
if($iCurrentPageNo > 1) {
$linkPrevious = '<a href="index.php?page='.($iCurrentPageNo - 1).'">Previous</a>';
} else {
$linkPrevious = ' ';
}
if($iCurrentPageNo < $iTotalNoOfPages) {
$linkNext = '<a href="index.php?page='.($iCurrentPageNo + 1).'">Next</a>';
} else {
$linkNext = ' ';
}
$linksPage = ' ';
for($i = 1; $i <= $iTotalNoOfPages; $i++) {
if($iCurrentPageNo != $i) {
$linksPage .= "<a href='index.php?page=$i'>$i</a> ";
} else {
$linksPage .= "$i ";
}
}
$sOutput .= '<div style="text-align:center">';
$sOutput .= "<span>$linkPrevious</span>";
$sOutput .= "<span style='margin:10px'>$linksPage</span>";
$sOutput .= "<span>$linkNext</span>";
$sOutput .= '</div>';
} else {
$sOutput .='Empty Page';
}
$sOutput .='</body></html>';
echo $sOutput;
?>[/php]
And that is all for today.
Regards.