I have some questions about pagination. I seem to have the pagination code working, and my display code is working, but the two do not work together.
I have the pagionation code:
$tbl_name="directory_tbl"; //your table name
// How many adjacent rows should be shown on each side?
$adjacents = 3;
/*
First get total number of rows in data table.
If you have a WHERE clause in your query, make sure you mirror it here.
*/
$query = mysql_query("SELECT * FROM $tbl_name") or die(mysql_error());
$total_rows = mysql_num_rows($query);
/* Setup vars for query. */
$targetpage = "paged.php"; //your file name (the name of this file)
$limit = 2; //how many items to show per page
if (!(isset($page)))
{
$page = 1;
} else
{$page = $_GET['page'];
}
if($page)
$start = ($page - 1) * $limit; //first item to display on this page
else
$start = 0; //if no page var is given, set start to 0
/* Get data. */
$sql = mysql_query("SELECT last_name FROM $tbl_name LIMIT $start, $limit") or die(mysql_error());
$result = mysql_query($sql);
/* Setup page vars for display. */
if ($page == 0) $page = 1; //if no page var is given, default to 1.
$prev = $page - 1; //previous page is page - 1
$next = $page + 1; //next page is page + 1
$lastpage = ceil($total_rows/$limit); //lastpage is = total rows / items per page, rounded up.
$lpm1 = $lastpage - 1; //last page minus 1
echo $total_rows; echo ' total rows in the table<br />';
echo $limit; echo ' number of rows to be displayed<br />';
echo $lastpage; echo ' pages to be displayed<br />';
// <---- these three echo statements show 6 rows - 2 limit - last page 3 but everything is displayed on a single page
/*
Now apply rules and draw the pagination object.
We're actually saving the code to a variable in case we want to draw it more than once.
*/
$pagination = "";
if($lastpage > 1)
{
$pagination .= "<div class=\\"pagination\\">";
//previous button
if ($page > 1)
$pagination.= "<a href=\\"$targetpage?page=$prev\\">« previous</a>";
else
$pagination.= "<span class=\\"disabled\\">« previous</span>";
//pages
if ($lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\\"current\\">$counter</span>";
else
$pagination.= "<a href=\\"$targetpage?page=$counter\\">$counter</a>";
}
}
elseif($lastpage > 5 + ($adjacents * 2)) //enough pages to hide some
{
//close to beginning; only hide later pages
if($page < 1 + ($adjacents * 2))
{
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\\"current\\">$counter</span>";
else
$pagination.= "<a href=\\"$targetpage?page=$counter\\">$counter</a>";
}
$pagination.= "...";
$pagination.= "<a href=\\"$targetpage?page=$lpm1\\">$lpm1</a>";
$pagination.= "<a href=\\"$targetpage?page=$lastpage\\">$lastpage</a>";
}
//in middle; hide some front and some back
elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
{
$pagination.= "<a href=\\"$targetpage?page=1\\">1</a>";
$pagination.= "<a href=\\"$targetpage?page=2\\">2</a>";
$pagination.= "...";
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\\"current\\">$counter</span>";
else
$pagination.= "<a href=\\"$targetpage?page=$counter\\">$counter</a>";
}
$pagination.= "...";
$pagination.= "<a href=\\"$targetpage?page=$lpm1\\">$lpm1</a>";
$pagination.= "<a href=\\"$targetpage?page=$lastpage\\">$lastpage</a>";
}
//close to end; only hide early pages
else
{
$pagination.= "<a href=\\"$targetpage?page=1\\">1</a>";
$pagination.= "<a href=\\"$targetpage?page=2\\">2</a>";
$pagination.= "...";
for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\\"current\\">$counter</span>";
else
$pagination.= "<a href=\\"$targetpage?page=$counter\\">$counter</a>";
}
}
}
//next button
if ($page < $counter - 1)
$pagination.= "<a href=\\"$targetpage?page=$next\\">next »</a>";
else
$pagination.= "<span class=\\"disabled\\">next »</span>";
$pagination.= "</div>\
";
}
Then I have my display code:
$query="SELECT * FROM directory_tbl ORDER BY last_name";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
echo "<h1>List of the Clergy</h1><br /><br />";
$i=0;
while ($i < $num) {
$field01=mysql_result($result,$i,"first_name");
$field02=mysql_result($result,$i,"mid_int");
$field03=mysql_result($result,$i,"last_name");
$field04=mysql_result($result,$i,"email1");
$field05=mysql_result($result,$i,"email2");
$field06=mysql_result($result,$i,"address1");
$field07=mysql_result($result,$i,"address2");
$field08=mysql_result($result,$i,"city");
$field09=mysql_result($result,$i,"state");
$field10=mysql_result($result,$i,"zip_code");
$field11=mysql_result($result,$i,"fax");
$field12=mysql_result($result,$i,"ph_office");
$field13=mysql_result($result,$i,"ph_home");
$field14=mysql_result($result,$i,"ph_church");
$field15=mysql_result($result,$i,"ph_cell");
$field16=mysql_result($result,$i,"w_name1");
$field17=mysql_result($result,$i,"w_name2");
echo "<div class=\\"col585\\"><hr /><div class=\\"left col40\\" >Name: <b>$field01 $field02 $field03</b><br />Email Address: $field04<br />Email Address: $field05<br />";
echo "Address: $field06<br />Address: $field07<br />City: $field08, $field09 $field10<br /><br />";
echo "Wife's Name: <b>$field16 $field17</b><br /></div>";
echo "<div class=\\"right col40\\" >Phone Numbers<br />";
$phone=$field11;
echo 'Fax: ';
echo format_phone($phone)."<br />" ;
$phone=$field12;
echo "Office: ";
echo format_phone($phone)."<br />" ;
$phone=$field13;
echo "Home: ";
echo format_phone($phone)."<br />" ;
$phone=$field14;
echo "Church: ";
echo format_phone($phone)."<br />" ;
$phone=$field15;
echo "Cell: ";
echo format_phone($phone)."<br />" ;
echo "</div><br /></div>";
$i++;
}
Then I have <?=$pagination?> as the last entry (I am guessing this is PHP short code to display the page numbers).
At the bottom of my page, I have the page numbers displayed as I would expect.
What am I missing?