Traverse between pages - getting records from mysql database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
asanga
Forum Newbie
Posts: 3
Joined: Sat Jun 11, 2005 1:29 am
Location: Colombo

Traverse between pages - getting records from mysql database

Post by asanga »

I need to traverse between pages by getting records from mysql database. its fine I can filter data from the database I can travese But I couldn't stop after all records are displayed. pls help me.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Code: Select all

$PageNo = $_GET['pg'];
$RowsPerPage = 10;
$StartRecord = ($PageNo-1)*$RowsPerPage;

$SQL = "SELECT * FROM `tablename` WHERE $Condition");
$Res = mysql_query($SQL);

$TotalCount = mysql_num_rows($Res);
$MaxPages   = 25;
$StartPgNo  = (ceil($PageNo / $MaxPages) * $MaxPages) - $MaxPages +1;
$LastPgNo   = ceil($TotalCount / $RowsPerPage);
$StopPgNo   = min($LastPgNo,$StartPgNo + $MaxPages);

$SQL = "
SELECT * FROM `tablename` WHERE $Condition
LIMIT $StartRecord,$RowsPerPage
";
$Res = mysql_query($SQL) or die(mysql_error());
Post Reply