hi all
below is my code for displaying member names from database in pagination form.
everything is working fine i.e. [First], [Previous], 1 2 3 4…, [Next], [Last]
I wish to have all those page numbers in a combo box so that the user can navigate the result pages by selecting any page number from the combo box.
Can anyone help me in this matter?
Thanks in advance
Below is my code:
[CODE]<?php
//Connect to DB
mysql_connect(“localhost”,”username”,”password”) or die(“Unable to connect to SQL server”);
mysql_select_db(‘mic52co_DatabaseName’) or die(“Unable to SELECT DB”);
// how many rows to show per page
$rowsPerPage = 3;
// by default we show first page
$pageNum = 1;
// if $_GET[‘page’] defined, use it as page number
if(isset($_GET[‘page’]))
{
$pageNum = $_GET[‘page’];
}
// counting the offset
$offset = ($pageNum – 1) * $rowsPerPage;
$query = “SELECT * FROM user LIMIT $offset, $rowsPerPage”;
$result = mysql_query($query) or die(‘Error, query failed’);
echo “<table border=’1′>”;
echo “<tr bgcolor=’#C0C0C0′> <th> <b><font face=’Tahoma’ size=’2′> UserID </font></b> </th> <th><b><font face=’Tahoma’ size=’2′> Full Name </font></b></th> <th><b><font face=’Tahoma’ size=’2′> Email </font></b></th> </tr>”;
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo “<tr><td width=’150′ ><font face=’Tahoma’ size=’2′>”;
echo $row[‘userid’];
echo “</font></td><td width=’205′><font face=’Tahoma’ size=’2′>”;
echo $row[‘fullname’];
echo “</font></td><td width=’205′><font face=’Tahoma’ size=’2′>”;
echo $row[’email’];
echo “</font></td></tr>”;
}
echo “</table>”;
// how many rows we have in database
$query = “SELECT COUNT(userid) AS numrows FROM user”;
$result = mysql_query($query) or die(‘Error, query failed’);
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row[‘numrows’];
// how many pages we have when using paging?
$maxPage = ceil($numrows/$rowsPerPage);
// print the link to access each page
$self = $_SERVER[‘PHP_SELF’];
$nav = ”;
for($page = 1; $page <= $maxPage; $page++)
{
if ($page == $pageNum)
{
$nav .= ” $page “; // no need to create a link to current page
}
else
{
$nav .= ” <a href=”$self?page=$page”>$page</a> “;
}
}
// creating previous and next link
// plus the link to go straight to
// the first and last page
if ($pageNum > 1)
{
$page = $pageNum – 1;
$prev = ” <a href=”$self?page=$page”>[Previous]</a> “;
$first = ” <a href=”$self?page=1″>[First]</a> “;
}
else
{
$prev = ‘ ’; // we’re on page one, don’t print previous link
$first = ‘ ’; // nor the first page link
}
if ($pageNum < $maxPage)
{
$page = $pageNum + 1;
$next = ” <a href=”$self?page=$page”>[Next]</a> “;
$last = ” <a href=”$self?page=$maxPage”>[Last]</a> “;
}
else
{
$next = ‘ ’; // we’re on the last page, don’t print next link
$last = ‘ ’; // nor the last page link
}
// print the navigation link
echo $first . $prev . $nav . $next . $last;
?>