Jump to content

Difficulty getting a "pagination" code to work...


Seaholme

Recommended Posts

Hey,

 

I put "pagination" in little bunny ear things because it's a stupid made-up word, however it was the name of the tutorial I used to try and get my forum to automatically generate further rows in new pages after its counted a certain number of rows.

 

Basically I can get the "Next" and "Last" buttons to appear, and it does show 10 rows... only those rows are completely blank (it's not filling in any of the data from the database) and perceptibly the "next" and "last" buttons change the URL but don't seem to be changing the number of blank rows displayed from 10 -- and there aren't 20 posts total on the board, so  that leads me to conclude that they don't work to show the remaining posts on the next page :/

 

I'm hesitant to ask here as so little of this actually -IS- working, so it seems to me like a lot of work will be required to fix it, but if people could point out any glaring errors to help me at least get some of it functioning, that would be really appreciated.

 

I've included pretty much all of the PHP from that page below. I guess it should be quite clear which bits are from the tutorial and which bits are mine, on account of it talking in the third person!

 

// fetch line id from URL
$id=$_GET['id']; 

// find all posts on this board
$sql="SELECT * FROM $tbl_name WHERE forumid=$id ORDER BY lastreply DESC";


// ORDER BY id descending 
$result=mysql_query($sql);
?>


<table width="70%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td width="50%" align="center" bgcolor="#E6E6E6"><strong>Topic</strong></td>
<td width="12%" align="center" bgcolor="#E6E6E6"><strong>Views</strong></td>
<td width="12%" align="center" bgcolor="#E6E6E6"><strong>Author</strong></td>
<td width="13%" align="center" bgcolor="#E6E6E6"><strong>Replies</strong></td>
<td width="13%" align="center" bgcolor="#E6E6E6"><strong>Last Reply</strong></td>
</tr>


<?php

if (!(isset($pagenum))) 
{ 
$pagenum = 1; 
} 

//Here we count the number of results 
//Edit $data to be your query 
$data = mysql_query("SELECT * FROM $tbl_name WHERE forumid=$id ORDER BY lastreply DESC") or die(mysql_error()); 
$rows = mysql_num_rows($data); 

//This is the number of results displayed per page 
$page_rows = 10; 

//This tells us the page number of our last page 
$last = ceil($rows/$page_rows); 

//this makes sure the page number isn't below one, or more than our maximum pages 
if ($pagenum < 1) 
{ 
$pagenum = 1; 
} 
elseif ($pagenum > $last) 
{ 
$pagenum = $last; 
} 

//This sets the range to display in our query 
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows; 


$data_p = mysql_query("SELECT * FROM $tbl_name $max") or die(mysql_error()); 
//This is where you display your query results
while($info = mysql_fetch_array( $data_p )) 
{ 
?>

<tr>
<td bgcolor="#FFFFFF"><a href="view_topic.php?id=<? echo $rows['id']; ?>"><? echo $rows['topic']; ?></a><BR></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['view']; ?></td>
<td align="center" bgcolor="#FFFFFF"><a href=profile.php?id=<? echo $rows['name']; ?>><? echo $rows['displayname']; ?> (#<? echo $rows['name']; ?>)</a></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['reply']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['lastreply']; ?> by (#<? echo $rows['lastreplyauthor']; ?>)</td>
</tr>

<?php
echo "<br>";
} 
echo "<p>";
// This shows the user what page they are on, and the total number of pages
echo " --Page $pagenum of $last-- <p>";
// First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page.
if ($pagenum == 1) 
{
} 
else 
{
echo " <a href='{$_SERVER['PHP_SELF']}?id=$id&pagenum=1'> <<-First</a> ";
echo " ";
$previous = $pagenum-1;
echo " <a href='{$_SERVER['PHP_SELF']}?id=$id&pagenum=$previous'> <-Previous</a> ";
} 
//just a spacer
echo " ---- ";
//This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
if ($pagenum == $last) 
{
} 
else {
$next = $pagenum+1;
echo " <a href='{$_SERVER['PHP_SELF']}?id=$id&pagenum=$next'>Next -></a> ";
echo " ";
echo " <a href='{$_SERVER['PHP_SELF']}?id=$id&pagenum=$last'>Last ->></a> ";
} 


// Start looping table row 
while($rows=mysql_fetch_array($result)){ 
?>

<?php


// Exit looping and close connection 
}
mysql_close();
?>


<tr>
<td colspan="5" align="right" bgcolor="#E6E6E6"><a href="create_topic.php?id=<? echo $id; ?>">
<strong>Create New Topic</strong></a></td>
</tr>
</table>
<?php


//close max/min if statement

 

Thanks!

it is not enough to add the page number of the url. what you need also is the row number from the page will read and the maximum number of data rows to be read. So this line (at the top of your code):

$sql="SELECT * FROM $tbl_name WHERE forumid=$id ORDER BY lastreply DESC";

 

should look like this (note the LIMIT part):

$sql="SELECT * FROM $tbl_name WHERE forumid=$id ORDER BY lastreply LIMIT $start_numbet, $max_number DESC";

 

so you either include these number in the url and retrieve them through $_GET, or you calculate them outside the function based on the page number and the $page_rows value.

 

hope this helps

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.