PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Ok... this is what I came up with but for some reason it won't show me more results when I click on one of the page numbers or next it just shows the pagination stuff and the static stuff. Tt doesn't show the 'prev' button either when its on a page > 1. Then when I click on the '1' page to go back to the results I have already been shown it doesn't show those anymore either. What the heck?!?
<?php
/* Set current, prev and next page */
$page = (!isset($_GET['page']))? 1 : $_GET['page'];
$prev = ($page - 1);
$next = ($page + 1);
/* Max results per page */
$max_results = 5;
/* Calculate the offset */
$from = (($page * $max_results) - $max_results);
/* Query the db for total results. You need to edit the sql to fit your needs */
$result = mysql_query("select id from members");
$total_results = mysql_num_rows($result);
$total_pages = ceil($total_results / $max_results);
$pagination = '';
/* Create a PREV link if there is one */
if($page > 1)
{
$pagination .= '<a href="test.php?page='.$prev.'">Previous</a> ';
}
/* Loop through the total pages */
for($i = 1; $i <= $total_pages; $i++)
{
if(($page) == $i)
{
$pagination .= $i;
}
else
{
$pagination .= "<a href=\"test.php?page='$i'\">$i</a>";
}
}
/* Print NEXT link if there is one */
if($page < $total_pages)
{
$pagination .= "<a href=\"test.php?page='$next'\">Next</a>";
}
/* Now we have our pagination links in a variable($pagination) ready to print to the page. I pu it in a variable because you may want to show them at the top and bottom of the page */
/* Below is how you query the db for ONLY the results for the current page */
$result=mysql_query("select *
from members
LIMIT $from, $max_results ");
while ($i = mysql_fetch_array($result))
{
print "<table>";
print "<tr>";
print "<td>$i[user_name]</td>";
print "<td>$i[pass]</td>";
print "</tr>";
print "</table>";
}
print "<table>";
print "<tr>";
print "<td>$pagination</td>";
print "</tr>";
print "</table>";
?>