PDA

View Full Version : Looking For Some Help With Pagination.



This_Is_Craig
01 Nov 2009, 10:23 AM
I've been reading through some tutorials on pagination, and I'm trying to implement it into a current project, but I'm not having the best luck with it. I already have some PHP pulling info from a database and everything how I'd like, so that's all set, but I'm not sure how to implement that into some PHP for pagination.

The PHP I have right now that I'd like to paginate is this:




<?PHP

$user_name = "####";
$password = "####";
$database = "####";
$server = "####";

$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);

?>
<div id="listing_top"></div>
<div id="listings">

<?PHP

if ($db_found) {

$SQL = "SELECT * FROM tbl_rest_list ORDER BY Name";
$result = mysql_query($SQL);

while ($db_field = mysql_fetch_array($result)) {

if(!empty($db_field['url'])) {
echo '<div class="name">' . "<h3>" . $html = '<a href="' . $db_field['url'] . '">' . $db_field['Name'] . '</a>' . "<h3>" . "</div>";
}
else {
echo "<div>" . "<h3>" . $html = $db_field['Name'] . "<h3>" . "</div>";
}

echo "<div>" . $db_field['Street'] . "</div>";

echo "<div>" . $db_field['City'] . ", " . $db_field['State'] . "</div>";

echo "<div>" . "<em>Phone: </em>" . "<strong>" . $db_field['Phone'] . "</strong>" . "</div>";

echo "<div>" . $db_field['Email'] . "</div>";

echo '<div id="type">' . "<em>Type of Food: </em>" . $db_field['Type'] . '</div>';
}
mysql_close($db_handle);

}
else {
echo "Database NOT Found ";
mysql_close($db_handle);
}
?>

</div>

<div id="listing_bottom"></div>



Thanks in advance for any help. You guys have already helped me a ton in understanding PHP. I really appreciate it.

jpstokes
09 Nov 2009, 09:25 AM
I'm assuming that you have some sort of list that you'd like to page. The following query is what I use to do paging...


$displayNumber = 20;
$lowerBound = ($page_number - 1) * $displayNumber;
$sql = "select * from table_name limit $lowerBound, $displayNumber";

You'll need to set a page variable and increment/decrement this variable as you go from page to page. Then in your php you capture the page number and the code does the rest.

**note - $displayNumber = the number of items to show in your list.