forum pagination help!
Posted: Tue Apr 05, 2011 1:08 pm
Please help with this forum pagination heres the code:
Please don't post lengthy code like that without enclosing it with PHP Code tags. Use the button on the form where you are posting. Thank you.
But it just links to the table but no results!
Please don't post lengthy code like that without enclosing it with PHP Code tags. Use the button on the form where you are posting. Thank you.
Code: Select all
<?php
include 'connect.php';
// get value of id that sent from address bar
$id=$_GET['id'];
$sql="SELECT * FROM topic WHERE id='$id'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
// find out how many rows are in the table
$sql = "SELECT COUNT(*) FROM reply";
$result = mysql_query($sql) or trigger_error("SQL", E_USER_ERROR);
$r = mysql_fetch_row($result);
$numrows = $r[0];
// number of rows to show per page
$rowsperpage = 10;
// find out total pages
$totalpages = ceil($numrows / $rowsperpage);
// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
// cast var as int
$currentpage = (int) $_GET['currentpage'];
} else {
// default page num
$currentpage = 1;
} // end if
// if current page is greater than total pages...
if ($currentpage > $totalpages) {
// set current page to last page
$currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) {
// set current page to first page
$currentpage = 1;
} // end if
// the offset of the list, based on current page
$offset = ($currentpage - 1) * $rowsperpage;
?>
<table width='800' border='0' align='center' cellpadding='10'>
<tr>
<td width='20' height='50' align='center' bgcolor='#990000'><font color='#FFFFFF'><b>ID</b></font></td><td width='200' height='50' align='center' bgcolor='#990000'><font color='#FFFFFF'><b>Username:<br />Date & Time:</b></font></td><td align='center' bgcolor='#990000'><font color='#FFFFFF'><b>Topic & Message:</b></font></td>
</tr>
<tr>
<td height='50' align='center' valign='top' bgcolor='#AAAAAA'></td>
<td height='50' align='center' valign='top' bgcolor='#AAAAAA'><b><? echo $rows['username']; ?></b><br /><? echo $rows['date']; ?><br /><? echo $rows['time']; ?></td>
<td height='50' align='center' bgcolor='#AAAAAA'><b><a href="viewtopic.php?id=<? echo $rows['id']; ?>"><? echo $rows['topic']; ?></a></b></td>
</tr>
<tr>
<td height='50' align='center' bgcolor='#AAAAAA' colspan='3'><? echo $rows['detail']; ?></td>
</tr>
<tr>
<td></td>
</tr>
<?php
$sql2 = "SELECT * FROM reply WHERE reply_id='$id' LIMIT $offset, $rowsperpage";
$result2 = mysql_query($sql2) or trigger_error("SQL", E_USER_ERROR);
while($rows=mysql_fetch_array($result2)){
?>
<tr>
<td height='50' align='center' valign='top' bgcolor='#AAAAAA'><b><? echo $rows['a_id']; ?></b></td>
<td height='50' align='center' valign='top' bgcolor='#AAAAAA'><b><? echo $rows['a_username']; ?></b><br /><? echo $rows['a_date']; ?><br /><? echo $rows['a_time']; ?></td>
<td height='50' align='center' bgcolor='#AAAAAA'><? echo $rows['a_reply']; ?></td>
</tr>
<?
}
?>
</table>
<br />
<BR>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form" method="post" action="addreply.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td valign="top"><strong>Reply</strong></td>
<td valign="top">:</td>
<td><textarea name="a_reply" cols="45" rows="3"></textarea></td>
</tr>
<tr>
<td> </td>
<td><input name="id" type="hidden" value="<? echo $id; ?>"></td>
<td><input type="submit" name="Submit" value="Submit"> <input type="reset" name="Reset" value="Reset"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<?php
/****** build the pagination links ******/
// range of num links to show
$range = 3;
// if not on page 1, don't show back links
if ($currentpage > 1) {
// show << link to go back to page 1
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'>First</a> ";
// get previous page num
$prevpage = $currentpage - 1;
echo " - ";
// show < link to go back to 1 page
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'>Previous</a> ";
} // end if
// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
// if it's a valid page number...
if (($x > 0) && ($x <= $totalpages)) {
// if we're on current page...
if ($x == $currentpage) {
// 'highlight' it but don't make a link
echo " [<b>$x</b>] ";
// if not current page...
} else {
// make it a link
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
} // end else
} // end if
} // end for
// if not on last page, show forward and last page links
if ($currentpage != $totalpages) {
// get next page
$nextpage = $currentpage + 1;
// echo forward link for next page
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>Next</a> ";
echo " - ";
// echo forward link for lastpage
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>Last</a> ";
} // end if
/****** end build pagination links ******/
?>