Jump to content

Recommended Posts

Im struggling with this pagination part of the replies of the forum it looks to be the final part! Heres the code:

 

<?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 ******/
?>

 

When I click next page or any link it goes to the table but blank page. I have also added '?id=$id' before ?currentpage on the link but this just doubles up the ?currentpage on the address page. Please help im really struggling!

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/232780-forum-pagination/
Share on other sites

ok I dont understand what you just put, its just the pagination part where I click on the link to goto next or previous page the next load of results dont show up?

I meant that when you post code on the forums put


tags around it.  This doesn't have anything to do with your problem, sorry.

Link to comment
https://forums.phpfreaks.com/topic/232780-forum-pagination/#findComment-1197726
Share on other sites

well I added ?id=$id to it:

 

// if not on page 1, don't show back links
if ($currentpage > 1) {
// show << link to go back to page 1
echo " <a href='viewtopic.php?id=$id?currentpage=1'>First</a> ";
// get previous page num
$prevpage = $currentpage - 1;
echo " - ";
// show < link to go back to 1 page
echo " <a href='viewtopic.php?id=$id?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='viewtopic.php?id=$id?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='viewtopic.php?id=$id?currentpage=$nextpage'>Next</a> ";
echo " - ";
// echo forward link for lastpage
echo " <a href='viewtopic.php?id=$id?currentpage=$totalpages'>Last</a> ";
} // end if
/****** end build pagination links ******/
?>

 

but then every time I click on next page or 2 it shows just the first page results and keeps adding currentpage= to the link everytime I click one of the links, im desperate last part of my forum to get it up and online. Please help! thanks

Link to comment
https://forums.phpfreaks.com/topic/232780-forum-pagination/#findComment-1197781
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.