So I have an example of a query that was graciously provided by Frost which has helped me learn SO much ( I am very much a newbie) but I am stuck trying to add pagination. I have been scouring the web for a week now and there are so many different examples and I have tried so many of them with little or no success. I was finally able to build what you see below, the problem is that when you hit search it does indeed only return 20 records but the pages don’t work. The way they dont work is that when you click ‘next’ it will go to page two but no results show up, and if you click again expecting page 3 it wont go there it stays on page 2. I really did not want to take anyone’s valuable time but I am just so very lost. Here is “my” code:
<?php
//we select all the License records
$req_limit = mysql_query("Select License from OPLR");
$result = mysql_numrows($req_limit);//mysql_numrows() give us the result of the request
// now we will use the result to limit the displayed messages
$page_limit = '20'; //we chose the number of messages by page
// here we divide the total by the number of messages we choose
$page_number = $result / $page_limit;
// we round the number of pages to avoid commas.
$total_number = ceil($page_number);
// here we take of one page because the first page will be displyed is number one
$number = $total_number - 1;
// if the variable number_page is equal or defferent to 0
if(isset($_GET['page_number']) || $_GET['page_number'] != '0' )
{
// multiplies the page limit with the current number page on the url
$mysql_limit = $page_limit * $_GET['page_number'];
}
else{ // no variable number_page
$mysql_limit = '0'; // the limit is 0
}
?>
<?php
/*****************************
* Simple SQL Search Tutorial by Frost
* of Slunked.com
******************************/
// Set up our error check and result check array
$error = array();
$results = array();
// First check if a form was submitted.
// Since this is a search we will use $_GET
if (isset($_GET['search'])) {
$searchTerms = trim($_GET['search']);
$searchTerms = strip_tags($searchTerms); // remove any html/javascript.
if (strlen($searchTerms) < 0) {
$error[] = "Search terms must be longer than 3 characters.";
}else {
$searchTermDB = mysql_real_escape_string($searchTerms); // prevent sql injection.
}
// If there are no errors, lets get the search going.
if (count($error) < 1) {
$searchSQL = "SELECT License, Pet_Name, Owner_Name, Species, Notes, Unowned FROM OPLR WHERE ";
// grab the search types.
$types = array();
$types[] = isset($_GET['License'])?"`License` LIKE '%{$searchTermDB}%'":'';
$types[] = isset($_GET['Pet_Name'])?"`Pet_Name` LIKE '%{$searchTermDB}%'":'';
$types[] = isset($_GET['Owner_Name'])?"`Owner_Name` LIKE '%{$searchTermDB}%'":'';
$types[] = isset($_GET['Species'])?"`Species` LIKE '%{$searchTermDB}%'":'';
$types[] = isset($_GET['Unowned'])?"`Unowned` LIKE 'y'":'';
$types = array_filter($types, "removeEmpty"); // removes any item that was empty (not checked)
if (count($types) < 1)
$types[] = "`Pet_Name` LIKE '%{$searchTermDB}%'"; // use the Pet_Name as a default search if none are checked
$andOr = isset($_GET['matchall'])?'AND':'OR';
$searchSQL .= implode(" {$andOr} ", $types) . " ORDER BY `Pet_Name`"; // order by Pet Name.
$searchResult = mysql_query($searchSQL) or trigger_error("There was an error.<br/>" . mysql_error() . "<br
/>SQL Was: {$searchSQL}");
if (mysql_num_rows($searchResult) < 1) {
$error[] = "The search term provided {$searchTerms} yielded no results.";
}else {
$results = array(); // the result array
$i = 1;
while ($row = mysql_fetch_assoc($searchResult)) {
$results[] = "<B>License Number: {$row['License']}</B><br />Pet Name: {$row['Pet_Name']}<br /> Owner Name: {$row['Owner_Name']}<br />Species: {$row['Species']}<br />Notes: {$row['Notes']}<br /><br />";
$i++;
}
}
}
}
?>
<?php echo (count($error) > 0)?"The following had errors:<br /><span id=\"error\">" . implode("<br />",
$error) . "</span><br /><br />":""; ?>
<form method="GET" action="<?php echo $_SERVER['PHP_SELF'];?>" name="searchForm" id="searchform">
<fieldset>
<center><p>Search For:
<input type="text" name="search" value="<?php echo isset($searchTerms)?htmlspecialchars
($searchTerms):''; ?>" /></p></center>
</fieldset>
<fieldset>
<div id="checks"><center>
License Number:
<input type="checkbox" name="License" value="on" <?php echo isset($_GET['License'])?"checked":''; ?> />
|
Species:
<input type="checkbox" name="Species" value="on" <?php echo isset($_GET['Species'])?"checked":''; ?> />
|
Unowned:
<input type="checkbox" name="Unowned" value="on" <?php echo isset($_GET['Unowned'])?"checked":''; ?> />
<br />
Match All Selected Fields? <input type="checkbox" name="matchall" value="on" <?php echo isset($_GET['matchall'])?"checked":''; ?> />
<br /><br />
<input type="submit" name="submit" value="Search!" /></center></div>
</fieldset>
</form>
<?php echo (count($error) > 0)?"The following had errors:<br /><span id=\"error\">" . implode("<br />",
$error) . "</span><br /><br />":""; ?>
<?php echo (count($results) > 0)?"Your search: {$searchTerms} <br> <b>Returned: </b><br /><br />" . implode("",
$results):"";
?>
<?php
// If the page number different of 0 and if the page_number is unset
if( $number != '0' && empty($_GET['page_number']))
{
print '<a href="registry.php?page_number=1">Next page</a>'; // we set the page_number to 1
}
// in this condition, the variable page_number is set and its value is less than $number
elseif($number !='0' && isset($_GET['page_number']) && $_GET['page_number'] < $number)
{
$next = $_GET['page_number'] + 1; // add 1 to the current page number
print '<a href="registry.php?page_number='.$suivant.'">next page</a>'; //The link for the next pages
// go back to the precedent page, we used a java-script code to do it print ' <a href="javascript: history.back();">Previous page</a>';
}
// here, the link that will be displayed when the page number is reched
elseif( $number !='0' && isset($_GET['page_number']) && $_GET['page_number'] >= $number )
{
print '<a href="javascript: history.back();">Previous page</a>';
}
function removeEmpty($var) {
return (!empty($var));
}
?>
You have multiple problems. First one is that you use a form submit to get the values from the options (which is fine) but the link “next page” just gives a page_id and not all the other options. (You could either put them in the next link (as you use _GET anyways) or store the submitted values in a sessionn. I’d prefer the latter).
The other problem is, that you do some pagination calculation, but u never use the number you calculated. You have to put in an mysql LIMIT statement. ( There right point for that would be after the “ORDER BY”. For that replace
by
But remember, this will only fix problem 2, you still have to put those _GET Variables into the “next page”