I have read and reread a number of pagnation scripts and tutorials. I finally found one that mostly worked. The only problem seems to be is, that when the link to say, page (2), (or further) I can presume, is clicked and the page reloads I get an error of not being able to make the query.
Its the same query I made on the first page and with page (2) as $_get it should retrieve the remainder.
I am stuck, here is my code, can anyone figure it out?
PHP Code:
####### Pagnation script #########
// Sets how many results shown per page.
$limit = 5;
// Sets what we want to pull from the database.
$query_count = "SELECT * FROM threads WHERE thread_id = '$_POST[th_id]'";
$result_count = mysqli_query($connection,$query_count);
if(!$result_count){
echo"unable to get result_count @ this time";
exit();
}
$totalrows= mysqli_num_rows($result_count);
// Checks if the $page variable is empty (not set). If it is empty, we're on page 1
if(empty($_GET['page'])){
$page = 1;
}
####### ### ### ### ## #########
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>threads</title>
</head>
<body background='/pslocal131/graphics/back8.jpg' bgcolor='' topmargin='50' text='#ffffff'>
<?php
echo"<table border='1' bordercolor='#083194' background='' bgcolor='#3152a5' cellpadding='0' cellspacing='0' width='900' height='' align='center'><tr><td>";
# This file loads the header to the document.
include_once("header.inc");
# This section creates navigation and New topic button for new threads.
echo"<table border='0' background='/pslocal131/graphics/gray.gif' bgcolor='' cellpadding='2' cellspacing='0' width='850' height='25' align='center'><tr><td width='785' height='25'>";
nav_pslocal131();
echo"</td><td width='65' height='25' align='right'>";
$new='new_thread';
if(@$_SESSION['logname'] != "admin" && $_POST[th_id] == 1){ $new = "<font size='2' color='#000000' face=''>Admin only</font>";}
else {$new('test');}
echo"</td></tr></table>";
# This table gives a description of the threads being displayed.
echo"<table border='1' background='/pslocal131/graphics/gray.gif' bgcolor='' cellpadding='0' cellspacing='0' width='850' height='30' align='center'><tr><td>";
echo"<td background='' bgcolor='' width='400' height='30' align='center'><font size='3' color='#29006b' face''><b>subject</b></font></td>";
echo"<td background='' bgcolor='' width='150' height='30' align='center'><font size='3' color='#29006b' face''><b>author</b></font></td>";
echo"<td background='' bgcolor='' width='100' height='30' align='center'><font size='3' color='#29006b' face''>last post</font></td>";
echo"<td background='' bgcolor='' width='100' height='30' align='center'><font size='3' color='#29006b' face''>replies</font></td>";
echo"<td background='' bgcolor='' width='100' height='30' align='center'><font size='3' color='#29006b' face''>views</font></td>";
echo"</td></tr></table> $nothing";
# This section retrieves results for display and also for pagnation.
// Ex: (1 * 5) - 5 = 0 <- data starts at 0
$limitvalue = $page * $limit - ($limit);
// Selects all the data from table.
// LIMIT $row_to_start_at, $how_many_rows_to_return
$query = "SELECT * FROM threads WHERE thread_id='$_POST[th_id]' ORDER BY id DESC LIMIT $limitvalue,$limit";
$result = mysqli_query($connection,$query);
if(!$result){
echo"unable to get result limitvalue, limit @ this time";
exit();
}
if(mysqli_num_rows($result) == 0){
$nothing = "<center><font size='3' color='' face=''>There are no threads in this forum.</font></center>";
}
while ($row = mysqli_fetch_array($result)){
$tp_id=$row[id];
$author=$row[author];
$th_subject=$row[subject];
$body=$row[body];
$views=$row[views];
# This section is the product of the above query.
echo"<table border='1' background='' bgcolor='' cellpadding='0' cellspacing='0' width='850' height='80' align='center'><tr>";
echo"<td background='' bgcolor='' width='400' align='left'>";view_post();
echo"</td><td background='' bgcolor='' width='150' height='80' align='center'><font size='3' color='ffffff' face''><b>$author </b></font></td>";
echo"<td background='' bgcolor='' width='100' height='80' align='center'><font size='3' color='ffffff' face''>$date<br>$time </font></td>";
echo"<td background='' bgcolor='' width='100' height='80' align='center'><font size='3' color='ffffff' face''>$total </font></td>";
echo"<td background='' bgcolor='' width='100' height='80' align='center'><font size='3' color='ffffff' face''>$views </font></td>";
echo"</tr></table>";
}
### Pagnation ########
// Fancy way of subtracting 1 from $page
if($page != 1){#1
$pageprev = $page--;
// If we are not on the first page this outputs PREV in link form.
$prev=("<a href='/pslocal131/threads.phppage=$pageprev'>$limit PREV</a>");
}#1
else{#2
// If we're on page 1, PREV is not a link
$prev=("$limit PREV");
}#2
// We divide our total amount of rows (for example 12) by the limit (5).
$numofpages = $totalrows / $limit;
// This will yield 2.4, which can be rounded down to 2. The next few lines create 2 pages,
// and then check to see if we have extra rows remaining for a 3rd page.
// This for loop will add 1 to $i at the end of each pass until $i is greater than $numofpages (2.4).
for($i = 1; $i <= $numofpages; $i++){#1
// This if statement will not make the current page number available in link form. It will, however, make all other pages available in link form.
if($i == $page){#2
$doc=(" ($i) ");
}#2
else{#3
$doc=("<a href='/pslocal131/threads.php?page=$i'> ($i) </a>");
}#3
}#1
// The following decides if there are any remaning pages to create. It returns the remainder after dividing two numbers.
// If there is no remainder, it returns zero.
if(($totalrows % $limit) != 0){#1
// This is the statement that turns pages into link form that is used above.
if($i == $page){#2
$doc=(" ($i) ");
}#2
else{#3
$doc=("<a href='/pslocal131/threads.php?page=$i'> ($i) </a>");
}#3
}#1
// This statement checks to see if there are more rows remaining, meaning there are pages in front of the current one.
if(($totalrows - ($limit * $page)) > 0){#1
// Fancy way of adding 1 to page
$pagenext = $page + 1;
// If there are pages remaining, this outputs NEXT in link form.
$next=("<a href='/pslocal131/threads.php?page=$pagenext'>NEXT $limit</a>");
}#1
else{#2
// If we're on the last page possible, NEXT will NOT be displayed in link form.
$next=("NEXT $limit");
}#2
### ### ### ### ### ##
mysqli_close($connection);
include_once("footer.inc");
echo"<table border='0' background='' bgcolor='' cellpadding='0' cellspacing='0' width='850' height='30' align='center'>";
echo"<tr><td align='left'>total threads: $totalrows</td><td align='right'>$prev $doc $next </td></tr></table>";
echo"<br><br>";
echo"</td></tr></table>";