Need some assistanct getting pagination links to work
Posted: Sun Jun 17, 2007 2:21 pm
Hello,
I have a page which displays 25 records from a MySQL database on a page, and I want to have a link to the next 25 records, and a link back to the previous 25 records when that is available. My links as I have the code so far aren't working.
Any offers of assistance would be greatly appreciated!
Here is the PHP code:
I have a page which displays 25 records from a MySQL database on a page, and I want to have a link to the next 25 records, and a link back to the previous 25 records when that is available. My links as I have the code so far aren't working.
Any offers of assistance would be greatly appreciated!
Here is the PHP code:
Code: Select all
<?php
//start a session
session_start();
//check for validity of user
$db_name="DB";
$table_name ="Table";
$connection = @mysql_connect("localhost", "username", "password")
or die (mysql_error());
$db = @mysql_select_db($db_name, $connection) or die (mysql_error());
$limit = 25;
// Sets how many results shown per page
$query_count = "SELECT count(*) FROM $table_name";
// Sets what we want to pull from the database
$result_count = mysql_query($query_count);
// Pulls what we want from the database
$totalrows = mysql_num_rows($result_count);
// This counts the number of users
if(empty($page)){ // Checks if the $page variable is empty (not set)
$page = 1; // If it is empty, we're on page 1
}
$limitvalue = $page * $limit - ($limit);
// Ex: (2 * 25) - 25 = 25 <- data starts at 25
//build and issue query
$sql ="SELECT id, f_name, l_name, company FROM $table_name ORDER BY company LIMIT $limitvalue, $limit";
$result = @mysql_query($sql, $connection) or die(mysql_error());
$limit = 25;
$query_count = "SELECT count(*) FROM $table_name";
$result_count = mysql_query($query_count);
$totalrows = mysql_num_rows($result_count);
if(empty($page)){
$page = 1;
}
$limitvalue = $page * $limit - ($limit);
if(mysql_num_rows($result) == 0){
echo("Nothing to Display!");
}
//create list block of results
$contact_list="<ul class='vertical_list'>";
while ($row = mysql_fetch_array($result)) {
$id = $row['0'];
$f_name=$row['1'];
$l_name=$row['2'];
$company=$row['3'];
$contact_list .="<li>
<a href=\"show_contact.php?id=$id\">$company,$l_name, $f_name, </a>";
}
$contact_list .="</li></ul>";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US" dir="ltr">
<head>
<meta http-equiv="Content-type" content="text/html; charset=ISO-8859-1" />
<title>PHP Pagination</title>
</head>
<body>
<!-- begin outer wrapper div -->
<div id="wrapper_outer">
<!-- begin header div -->
<!-- top nav list -->
<!-- inner content wrapper div -->
<div id="wrapper_inner">
<div id="main_content">
<!-- old browser help -->
<p>Select a contact from the list below, to view a contact's record</p>
<? echo "$contact_list"; ?>
<?php
if(!isset($_GET['page'])){
$page = 1;
} else {
$page = $_GET['page'];
}
$numofpages = $totalrows / $limit;
for($i = 1; $i <= $numofpages; $i++){
if($i == $page){
echo($i." ");
}else{
echo("<a href=\"$PHP_SELF?page=$i\">$i</a> ");
}
}
if(($totalrows % $limit) != 0){
if($i == $page){
echo($i." ");
}else{
echo("<a href=\"$PHP_SELF?page=$i\">$i</a> ");
}
}
if(($totalrows - ($limit * $page)) > 0){
$pagenext = $page++;
echo("<a href=\"$PHP_SELF?page=$pagenext\">NEXT".$limit."</a>");
}else{
echo("NEXT".$limit);
}
mysql_free_result($result);
?>
</div>
</div>
</div>
</body>
</html>