PAGING IN PHP
<head>
<title>Paging Using PHP</title>
</head>
<body>
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$rec_limit = 5;
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db('database name');
/* Get total number of records */
$sql = "SELECT count(id) FROM table name ";
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
$row = mysql_fetch_array($retval);
$rec_count = $row[0];
if( isset($_GET{'page'} ) )
{
$page = $_GET{'page'} + 1;
$offset = $rec_limit * $page ;
}
else
{
$page = 0;
$offset = 0;
}
$left_rec = $rec_count - ($page * $rec_limit);
$sql = "SELECT * ".
"FROM table ".
"LIMIT $offset, $rec_limit";
$retval = mysql_query( $sql, $conn );
echo "<table border='2'>";
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval))
{
echo "<tr><td>".$row['id']."</td>" ;
echo "<td>".$row['name']."</td> ";
echo "<td>".$row['name']."</td> ";
echo "<td >"."<img src='".$row['path']."'"."</td> </tr>";
}
if( $page > 0 )
{
$last = $page - 2;
echo "<a href=\"?page=$last\">Last 1 Records</a> |";
echo "<a href=\"?page=$page\">Next 1 Records</a>";
}
else if( $page == 0 )
{
echo "<a href=\"?page=$page\">Next 1 Records</a>";
}
else if( $left_rec < $rec_limit )
{
$last = $page - 2;
echo "<a href=\"?page=$last\">Last 1 Records</a>";
}
mysql_close($conn);
?>
No comments:
Post a Comment