I have created a pagination script that works quite well so far, however I have got stuck at the moment on how to allow a user to select the number of results they wanted.
The problem is with these lines I have added
$userschoice1 = $_GET['$userschoice'];
if (isset($userschoice1))
{$rowsPerPage = $userschoice1;}
else
{$rowsPerPage = 5;}
echo "$rowsPerpage";
echo "<a href=\"{$_SERVER['PHP_SELF']}?\$userschoice=2\">2</a> <br/>";
echo "<a href=\"{$_SERVER['PHP_SELF']}?\$userschoice=10\">10</a> <br/>";
echo "<a href=\"{$_SERVER['PHP_SELF']}?\$userschoice=20\">20</a> <br/>";
They work fine for the first page but if we select say 20 the first page shows 20 results and the database as 26 records but when you click on the next link instead of showing the next 6 records it shows numbers 5 – 10.
Now I understand that the problem is that once this link is clicked the $userschoice variable goes back to being unset and so the default 5 figure is used.
Is there a way around this?? Or what is a better way of adding a users choice to the script below???
<?php
$pagenum = $_GET[''];
$dbcnx = @mysql_connect("");
if (!$dbcnx) {
echo( "<P>Unable to connect to the database server at this time.</P>" );
exit();
}
if (!@mysql_select_db("tpvhc", $dbcnx) ) {
echo( "<P>Unable to locate the Correct database at this time.</P>" );
exit();
}
// how many rows to show per page
$userschoice1 = $_GET['$userschoice'];
if (isset($userschoice1))
{$rowsPerPage = $userschoice1;}
else
{$rowsPerPage = 5;}
echo "$rowsPerpage";
// by default we show first page
$pageNum = 1;
// if $_GET['page'] defined, use it as page number
if(isset($_GET['page']))
{
$pageNum = $_GET['page'];
}
// counting the offset
$offset = ($pageNum - 1) * $rowsPerPage;
echo "$offset";
$query = " SELECT Cottage_ID, Cottage_Name, Bedrooms FROM Cottage " . " LIMIT $offset, $rowsPerPage";
$result = mysql_query($query) or die("<b>Query Failed:</b> " . $query);
echo '<table class="table">';
echo '<tr><th>Cottage ID</th><th>Cottage Name</th><th>Bedrooms</th></tr>';
while($row = mysql_fetch_array($result))
{
echo '<tr>';
echo '<td>' . $row['Cottage_ID'] . '</td>';
echo '<td>' . $row['Cottage_Name'] . '</td>';
echo '<td>' . $row['Bedrooms'] . '</td>';
echo '<tr>';
}
echo '<table>';
$maxPage = ceil($numrows/$rowsPerPage);
// how many rows we have in database
$query = "SELECT COUNT(Cottage_ID) AS numrows FROM Cottage";
$result = mysql_query($query) or die("<b>Query Failed:</b> " . $query);
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row['numrows'];
echo "$numrows";
// how many pages we have when using paging?
$maxPage = ceil($numrows/$rowsPerPage);
// print the link to access each page
$self = $_SERVER['PHP_SELF'];
$nav = '';
//showOffset is the number of pages we show after the current page
$showOffset = 3;
//This says if the current pagenum + the offset is greater than the last page the offset_val = the last page
//otherwise offset_val equals the current pagenum plus the showoffset
//This then palces one of 2 values into a variable called $offset_val
if($pageNum + $showOffset > $maxPage)
{$offset_val = $maxPage;}
else
{$offset_val = ($pageNum + $showOffset);}
//for(initialize counter; condition until counter is reached; increment counter)
for($page = $pageNum; $page <= $offset_val; $page++ )
{
if ($page == $pageNum){
$nav .= " $page "; // no need to create a link to current page
}
else{
$nav .= " <a href=\"$self?page=$page\">$page</a> ";
}
}
//end of page links code
// creating previous and next link
// plus the link to go straight to
// the first and last page
if ($pageNum > 1)
{
$page = $pageNum - 1;
$prev = " <a href=\"$self?page=$page\">[Prev]</a> ";
$first = " <a href=\"$self?page=1\">[First Page]</a> ";
}
else
{
$prev = ' '; // we're on page one, don't print previous link
$first = ' '; // nor the first page link
}
if ($pageNum < $maxPage)
{
$page = $pageNum + 1;
$next = " <a href=\"$self?page=$page\">[Next]</a> ";
$last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";
}
else
{
$next = ' '; // we're on the last page, don't print next link
$last = ' '; // nor the last page link
}
// print the navigation link
echo $first . $prev . $nav . $next . $last;
echo "<br/>";
echo "<a href=\"{$_SERVER['PHP_SELF']}?\$userschoice=2\">2</a> <br/>";
echo "<a href=\"{$_SERVER['PHP_SELF']}?\$userschoice=10\">10</a> <br/>";
echo "<a href=\"{$_SERVER['PHP_SELF']}?\$userschoice=20\">20</a> <br/>";
?>
</p>
</body>
</html>
Cheers