Reputation: 101
Hi all I've been teaching myself pagination, though I have a general question about using a link to view the next set of results.
I am able to display the data in a table using the code below. With my second code example I can get the links displayed e.g. 1,2,3,4 Next. And the numbers change depending on how many records should be displayed. Though my problem arrives when I press the link to view the next page of results. The page reloads but the same set of results is displayed. Any assistance or help would be greatly appreciated.
The code below is used to count the record and a set a variable for the amount of records displayed on screen at any one time.
<?
$per_page = 4;
$start = 0;
$result = mysql_query("select * from blogentry WHERE approve = 'Y' order by timeleft ASC");
//count records
$record_count = mysql_num_rows($result);
//count max pages
$max_pages = $record_count / $per_page; //may come out as decimal
if (!$start)
$start = 0;
$get = mysql_query("select * from blogentry WHERE approve = 'Y' order by timeleft ASC LIMIT $start, $per_page");
while ($row = mysql_fetch_assoc($get))
{
$usn1 = $row['username'];
$tml1 = $row['timeleft'];
$bge1 = $row['blogentry'];
$irm1 = $row['ResponceMess'];
echo "<table>"
Table code intentionally left out
echo "</table>"
The code below is used to display the pagination though when the link is pressed the same results is displayed:
//setup prev and next variables
$prev = $start - $per_page;
$next = $start + $per_page;
if(!($start<=0))
echo "<a href'messages.php?start=$prev'>Prev</a>";
//set variable for first page
$i=1;
//show page numbers
for ($x = 0; $x < $record_count; $x = $x + $per_page)
{
if ($start != $x)
echo " <a href='messages.php?start=$x'>$i</a> ";
else
echo " <a href='messages.php?start=$x'><b>$i</b></a> ";
$i++;
}
//show next button
if (!($start >= $record_count - $per_page))
echo " <a href='messages.php?start=$next'>Next</a>";
?>
Upvotes: 0
Views: 109
Reputation: 2685
You need to check the $_GET['start'] variable and see if it isset. If it is set then make $start = $_GET['start']. Otherwise your query will always start with the first result instead of starting on the result you want. You could just add something like:
$start = $_GET['start'] ? $_GET['start'] : 0 ;
to the top of your page instead of:
$start = 0;
also this query :
$get = mysql_query("select * from blogentry WHERE approve = 'Y' order by timeleft ASC LIMIT $start, $per_page");
should be:
$get = mysql_query("select * from blogentry WHERE approve = 'Y' order by timeleft ASC LIMIT $start, $start + $per_page");
This is because the Limit Clause takes the start index and end index instead of the start index and the number of records you want.
Upvotes: 1
Reputation: 21
The problem is that you are hardcoding the $start
var and you are not retreiving the value from the $_GET... try the next:
$start = isset($_GET['start']) ? $_GET['start'] : 0;
Most clear to understand:
if(isset($_GET['start'])) {
$start = $_GET['start'];
}else{
$start = 0;
}
Upvotes: 1