Page 1 of 1

forum pagination help!

Posted: Tue Apr 05, 2011 1:08 pm
by simmsy
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.

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>&nbsp;</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 ******/
?>
But it just links to the table but no results!

Re: forum pagination help!

Posted: Tue Apr 05, 2011 4:29 pm
by social_experiment

Code: Select all

$rows['a_id'];
Trying to extract the values from the array while outside a 'while' loop is your problem. To retrieve the values this approach is better

Code: Select all

<?php
$result=mysql_query($sql);
while ($rows=mysql_fetch_array($result)) {
 echo $rows['a_id'];
 // etc
}
// pagination goes here.
?>
Hth

Re: forum pagination help!

Posted: Wed Apr 06, 2011 2:31 am
by simmsy
I know but doing it this way doesnt make a difference 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?

Re: forum pagination help!

Posted: Wed Apr 06, 2011 11:00 am
by social_experiment
Ok, yes what i suggested might not be your problem.

When using pagination you have to pass the following values along : $offset & $rowsperpage. Your 'link' to the next page needs to look different

Code: Select all

<?php
 <a href="page.php?offset=10&rowsperpage=20">Next</a>
?>
Your query will be modified accordingly on each page, depending on the values retrieved from the query string. The value you are passing in the links have no affect on the query and from there your dilemma.

Re: forum pagination help!

Posted: Wed Apr 06, 2011 11:34 am
by simmsy
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

Re: forum pagination help!

Posted: Wed Apr 06, 2011 4:36 pm
by social_experiment

Code: Select all

<?php
$recordsPerPage = 4;
		
// create the count query
$portfolioQuery = "SELECT COUNT(id) FROM portfolio";
$records = $this->_count_Sql($portfolioQuery);
		
// calculate the amount of pages
if ($records > $recordsPerPage) {
$pages = ceil($records/$recordsPerPage);

else {
$pages = 1;
}
		
// get the value of $start
if (isset($_GET['s']) && is_numeric($_GET['s'])) {			
$start = $_GET['s'];				
}
else { $start = 0; }

//----
// query would be "SELECT * FROM table LIMIT $start, $recordsPerPage
//----
// create current page
$currentPage = ($start/$recordsPerPage) + 1;
			
// create the numbers and links for the pages
if ($pages > 1) {
echo '<div class="pages">';
	
// create 'page' links
for ($i = 1; $i <= $pages; $i++) {
if ($i != $currentPage) {
echo "&nbsp;<a href=\"". $_SERVER['PHP_SELF'] . "?s=" .
($recordsPerPage * ($i-1)) ."\" title=\"To page $i\">". $i ."</a>&nbsp;";
}
else {
echo "&nbsp;<span>". $i ."</span>&nbsp;";
}
}
				
echo '</div>';
}
?>
Here is an example that will help you figure out where you are going wrong. Hth