I am trying to add a pagination feature to an existing script that works well without the pagination. I have copied a pagination example and could make it work as a separate document. But when I try to include the pagination to script that allows a user modified query, the links do not work when clicked. I suspect the form action should call yet another script instead of the same, burgresults.php script. Here is the code that does not produce functioning links.
<html>
<head>
<title>Product Search Results</title>
</head>
<body>
<form action="burgresults.php" method="post">
Choose Search Type:<br />
<select name="searchtype">
<option value="model">Model</option>
<option value="description">Description</option>
<option value="category">Category</option>
<option value="make">Make</option>
</select>
<br />
Enter Search Term:<br />
<input name="searchterm" type="text">
<br />
<input type="submit" value="Search">
</form>
<h1>Product Search Results</h1>
PHP:
<?php
$searchtype=$HTTP_POST_VARS['searchtype'];
$searchterm=$HTTP_POST_VARS['searchterm'];
$searchterm= trim($searchterm);
if (!$searchtype || !$searchterm)
{
echo 'You have not entered search details. Please go back and try again.';
//exit;
}
$searchtype = addslashes($searchtype);
$searchterm = addslashes($searchterm);
$db = mysql_pconnect('localhost', 'burgsurfer', 'ham789');
if (!$db)
{
echo 'Error: Could not connect to database. Please try again later.';
exit;
}
mysql_select_db('burgweb');
$query = "select * from products where ".$searchtype." like '%".$searchterm."%'";
$result = mysql_query($query);
$num_results = mysql_num_rows($result);
echo '<p>Number of products found: '.$num_results.'</p>';
if (!($limit)){
$limit = 10;} // Default results per-page.
if (!($page)){
$page = 0;} // Default page value.
$numrows = $num_results;
if ($numrows == 0){
echo("No results found matching your query - $query");
exit();}
$pages = intval($numrows/$limit); // Number of results pages.
if ($numrows%$limit) {
$pages++;} // has remainder so add one page
$current = ($page/$limit) + 1; // Current page number.
if (($pages < 1) || ($pages == 0)) {
$total = 1;} // If $pages is less than one or equal to 0, total pages is 1.
else {
$total = $pages;} // Else total pages is $pages value.
$first = $page + 1; // The first result.
if (!((($page + $limit) / $limit) >= $pages) && $pages != 1) {
$last = $page + $limit;} //If not last results page, last result equals $page plus $limit.
else{
$last = $numrows;} // If last results page, last result equals total number of results.
$query = "select * from products where ".$searchtype." like '%".$searchterm."%' limit $page, $limit";
$result = mysql_query($query);
for ($i=0; $i <$limit; $i++)
{
$row = mysql_fetch_array($result);
echo '<p><strong>'.($i+1).'. Model: ';
echo htmlspecialchars(stripslashes($row['model']));
echo '</strong><br />Description: ';
echo stripslashes($row['description']);
echo '<br />Price: ';
echo stripslashes($row['price']);
echo '<br />Category: ';
echo stripslashes($row['category']);
echo '<br />Make: ';
echo stripslashes($row['make']);
echo '<br />Weight: ';
echo stripslashes($row['weight']);
echo '<br />Age: ';
echo stripslashes($row['age']);
echo '<br />Availibilty: ';
if (stripslashes($row['number']) > 0)
{
echo 'In Stock';
}
else
{
echo 'Sold Out';
}
echo '</p>';
}
?>
<p align="center">
PHP:
<php?
if ($page != 0) { // Don't show back link if current page is first page.
$back_page = $page - $limit;
echo("<a href=\"$PHP_SELF?query=$query&page=$back_page&limit=$limit\">back</a>\n");}
for ($i=1; $i <= $pages; $i++) // loop through each page and give link to it.
{
$ppage = $limit*($i - 1);
if ($ppage == $page){
echo("<b>$i</b> \n");} // If current page don't give link, just text.
else{
echo("<a href=\"$PHP_SELF?query=$query&page=$ppage&limit=$limit\">$i</a>\n");}
}
if (!((($page+$limit) / $limit) >= $pages) && $pages != 1) { // If last page don't give next link.
$next_page = $page + $limit;
echo("<a href=\"$PHP_SELF?query=$query&page=$next_page&limit=$limit\">next</a>\n");}
?>
</p>
</body>
</html>