Jump to content

Recommended Posts

Hey guys,

 

Basically I've got a code which is meant to give me a second page after 10 entries and show 10 entries a page, however I've been struggling with it for some time and it just won't work. Right now it'll change the URL and move onto the next page (supposedly) but no matter what page I'm on all it does is show every single entry. Can anybody help me work out what's going wrong? :(

 

<?php
include('connect.php');
include('header.php');
include('loggedin.php');

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


// set maximum and minimum IDs
if($id > '6'){
echo "Oops, no such line exists!";
} elseif($id < '1'){
echo "Oops, no such line exists!";
} else {


// find all posts on this board
$sql="SELECT * FROM $tbl_name WHERE forumid=$id ORDER BY lastreply LIMIT $pagenum, $max 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

$pagenum = $_GET['pagenum'];

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 WHERE forumid='$id' ORDER BY lastreply DESC") 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 $info['id']; ?>"><? echo $info['topic']; ?></a><BR></td>
<td align="center" bgcolor="#FFFFFF"><? echo $info['view']; ?></td>
<td align="center" bgcolor="#FFFFFF"><a href=profile.php?id=<? echo $info['name']; ?>><? echo $info['displayname']; ?> (#<? echo $info['name']; ?>)</a></td>
<td align="center" bgcolor="#FFFFFF"><? echo $info['reply']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $info['lastreply']; ?> by (#<? echo $info['lastreplyauthor']; ?>)</td>
</tr>
<?php
} 
// 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 Page</a> ";
echo " ";
echo " // Page $pagenum of $last //";
$previous = $pagenum-1;
echo " <a href='{$_SERVER['PHP_SELF']}?id=$id&pagenum=$previous'> Previous Page</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 Page </a> ";
echo "  ";
echo " // Page $pagenum of $last //";
echo " <a href='{$_SERVER['PHP_SELF']}?id=$id&pagenum=$last'> Last Page </a> ";
echo "  ";
} 


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

<?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
}

// This shows the user what page they are on, and the total number of pages

// 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 Page</a> ";
echo " ";
echo " // Page $pagenum of $last //";
$previous = $pagenum-1;
echo " <a href='{$_SERVER['PHP_SELF']}?id=$id&pagenum=$previous'> Previous Page</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 Page </a> ";
echo "  ";
echo " // Page $pagenum of $last //";
echo " <a href='{$_SERVER['PHP_SELF']}?id=$id&pagenum=$last'> Last Page </a> ";
echo "  ";
} 


include('footer.php');

?>

 

Please let me know if you need more info.

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/209801-nextlast-page-help/
Share on other sites

$pagenum is wrong in that generally you want to get the index of the very last item queried.  or count of it rather.  on page 1 you are looking for all of them with a limit of 10, on page 2 you are looking for all of them from the 2nd entry down to the 11th and so on.

Link to comment
https://forums.phpfreaks.com/topic/209801-nextlast-page-help/#findComment-1095576
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.