PDA

View Full Version : cannt get this pagination working



abanana
06-12-2006, 01:36 PM
hi all, i cannot figure out how to get the pagination to work. :mad:
the problem is it display the first page of the search results correctly, with the links at the bottom of the page. but after i click "next", it ask me "please enter a search term". Obviously it cannot pass the submitted value. would you please tell me how?

here is the code:


<?php
$link = mysql_connect('localhost', 'root', 'pass') or die(mysql_error());

$db_selected = mysql_select_db('database')or die(mysql_error());

$searchterm = @$_GET['searchterm'];
if ($searchterm == "")
{
echo "<p>Please enter a search term</p>";
exit;
}


// how many rows to show per page
$rowsPerPage = 10;

// 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;

$query = "SELECT * FROM table where title like \"%$searchterm%\" LIMIT $offset, $rowsPerPage";
$result = mysql_query($query) or die('Error, query failed');

// how many rows we have in database
$query = "SELECT COUNT(id) AS numrows FROM table where title like \"%$searchterm%\"";
$results = mysql_query($query) or die('Error, query failed');
$row = mysql_fetch_array($results, MYSQL_ASSOC);
$numrows = $row['numrows'];

// how many pages we have when using paging?
$maxPage = ceil($numrows/$rowsPerPage);

// print the link to access each page
$self = $_SERVER['PHP_SELF'];
for($page = 1; $page <= $maxPage; $page++)
{
if ($page == $pageNum)
{
$nav .= " $page "; // no need to create a link to current page
}
else
{
$nav .= " <a href=\"$self?page=$page\">$page</a> ";
}
}

// creating previous and next link
// plus the link to go straight to
// the first and last page

if ($pageNum > 1)
{
$page = $pageNum - 1;
$searchterm = @$_GET['searchterm'];
$prev = " <a href=\"$self. \"?page=\" .$page. \"&searchterm =\" .$searchterm;\">[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;
$searchterm = @$_GET['searchterm'];
$next = " <a href=\"$self. \"?page=\" .$page. \"$searchterm =\" .$searchterm;\">[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
}

echo "<H2>Total titles found:&nbsp" . $numrows . "</H2>";

while($row = mysql_fetch_array($result))
{

echo $row['title'];

echo "<hr>";
}


// print the navigation link

echo "<center>" . $first ."&nbsp" . $prev . "&nbspPage $pageNum of&nbsp $maxPage Pages &nbsp" .

$next ."&nbsp". $last . "</center>";

?>

NancyJ
06-12-2006, 01:42 PM
why cant it pass the submitted value? You're retrieving that from $_GET so there is no reason you couldnt have a link that had the search term in as well as the page number

abanana
06-12-2006, 01:48 PM
thanks for your quick reply, the bit it doesn't work is everything ok when i submit the search, first page display 10 results, but when click "next", nothing but ask me to enter the search term again! why is that?