Get support from Xavier Media
It is currently Sun Dec 08, 2013 12:26 pm

All times are UTC




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Simple Pagging
PostPosted: Sun Nov 12, 2006 1:55 pm 

Points:
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 = '&nbsp;';
}

if($iCurrentPageNo < $iTotalNoOfPages) {
$linkNext = '<a href="index.php?page='.($iCurrentPageNo + 1).'">Next</a>';
} else {
$linkNext = '&nbsp;';
}

$linksPage = '&nbsp;';
for($i = 1; $i <= $iTotalNoOfPages; $i++) {
if($iCurrentPageNo != $i) {
$linksPage .= "<a href='index.php?page=$i'>$i</a>&nbsp;";
} else {
$linksPage .= "$i&nbsp;";
}
}

$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.


Report this post
Top
  
Reply with quote  
 Post subject: Thanks for the script
PostPosted: Wed Nov 15, 2006 8:47 pm 

Points:
Thanks for the script! :D

I am going to have to take a close look at it later - I have been needing to look into a paging script...


Report this post
Top
  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Registered users: No registered users


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  

Portal » Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
[
SEO MOD © 2007 StarTrekGuide ]