|
Re: How to use this Google style pagination function?
Ah, I see. I actually did the code for the pages I'm working on, including a blank to pick the number of pages, but I didn't understand what you wanted.
Not quite so tidy as Graham's, as expected  , and there may be some noob errors in it. Credit to some unknown person for some of the ideas on pagination, although most of the code is made from scratch. The file is huge (at least for me) and I've tried to redact the really long parts, using * * * for omissions.
This is the $_GET results page for a search (by 'name' field) using one to three words for the search and 1 to 999 results per page. Applicable part of the form is at the bottom.
- Code: Select all
<?php
// search.php
require_once('../../../mysql_foodvalues_connect.php');
$pagetitle = 'DHR: Display Nutrient Content of Specific Foods ';
$headline = 'Results of Search for Nutrient Content<br /> of Specific Foods';
$b = 'result_search_food';
$photo = "nutrient.gif";
include_once('./header_article_food_stripped.inc.php');
// rows to return
if (isset($_GET['limit'])) {
$limit = $_GET['limit'];
} else {
$limit=30;
}
require_once('./../../../mysql_foodvalues_connect.php');
$var = @$_GET['name'] ;
$var1 = @$_GET['name1'] ;
$var2 = @$_GET['name2'] ;
$limit = @$_GET['limit'];
$t = NULL;
$t1 = NULL;
$t2 = NULL;
$u = 'qqqq';
$u1 = 'qqqqq';
$u2 = 'qzqzqz';
(These are just placeholders to initiate the variables, I didn't want them floating around loose before I used them)
if (!isset($var)) {
echo "Please enter the name of a food in the first box and SUBMIT.";
exit();
} else {
$t = trim($var);
}
if (isset($var1)) $t1 = trim($var1);
if (isset($var2)) $t2 = trim($var2);
if (isset($limit)) $limit = trim($limit);
if ($t2 == 'frozen') $u2 = 'frz'; if ($t1 == 'frozen') $u1 = 'frz'; if ($t == 'frozen') $u = 'frz';
if ($t2 == 'baked') $u2 = 'bkd'; if ($t1 == 'baked') $u1 = 'bkd'; if ($t == 'baked') $u = 'bkd';
if ($t2 == 'boiled') $u2 = 'bld'; if ($t1 == 'boiled') $u1 = 'bld'; if ($t == 'boiled') $u = 'bld';
* * * (about 90 more rows like this - the database uses a lot of abbreviations)
if (!preg_match("/^[A-Za-z]{1,20}$/", $t)) {
die("<div class=\"column23r \"><h4>Alert!</h4><br>Each food search term
must be one word composed of letters only. No hyphens, quotation marks,
numbers, etc. We apologize for this restriction. Please re-enter. <br><br>If
you are searching for a possessive name, such as Stubb's, just enter the name
(Stubb) and the possessive form will be automatically included in the search.
Also, make sure to fill the boxes in order: if you just have one word, use box 1,
and if you just have two terms, use the first two boxes.</div>");
}
* * * (regex for other input omitted)
if (!preg_match("/^[0-9]{1,3}$/", $limit)) {
die("<div class=\"column23r \"><h4>Alert!</h4><br>Please enter a number between 1 and 999 in the last box.</div>");
}
if (isset($t2, $t1, $t)) {
$query = "SELECT id FROM usda WHERE (name LIKE \"%$t%\" OR name LIKE
\"%$u%\") AND (name LIKE \"%$t1%\" OR name LIKE \"%$u1%\") AND (name
LIKE \"%$t2%\" OR name LIKE \"%$u2%\") ";
}elseif
* * * (other clauses omitted)
}
$numresults=mysql_query($query);
if ($numresults) {
$numrows=mysql_num_rows($numresults);
} else {
echo '<span class="b">We are sorry, but your search returned no results. Try]
again with a broader search.</span>';
}
if (empty($s)) {
$s=0;
}
*** (this sets an artificial variable $s to enable variable pagination)
echo '<br class="sp25"><div class="center_panel"><h3>Results</h3><br><span class="b">You searched for  ';
if (isset($var2, $var1, $var)) {
echo ("\"$var $var1 $var2\". This search returned $numrows results.</span> ");
}elseif (
* * *
echo '<br />For a new search, you can use the form at the bottom of this page.';
if (isset($var2, $var1, $var)) {
$query = "SELECT id, name, cat FROM usda WHERE (name LIKE \"%$t%\" OR
name LIKE \"%$u%\") AND (name LIKE \"%$t1%\" OR name LIKE \"%$u1%\")
AND (name LIKE \"%$t2%\" OR name LIKE \"%$u2%\") ORDER BY cat, name
LIMIT $s,$limit";
* * *
}
$result = @mysql_query($query) or die (mysql_error());
//RESULTS DISPLAY
$ab = <<< END
<br /><br />If you see USDA abbreviation you don't recognize, click here for a
popup window with a list of abbreviations and meanings.<br />
<form>
<input class="submitusda" type="button" onClick="myPopup()" value="USDA abbreviations">
<noscript> <a href="./usda_abbr.html">USDA</a></noscript>
</form>
END;
echo $ab;
$count = (1 + $s);
$a = $s + ($limit);
if ($a > $numrows) $a = $numrows;
$b = ($s + 1);
if ($numrows > 0) echo "<br><span class=\"b\">Showing results $b to $a
of $numrows:</span><br><br />";
if ($numrows == 0) {
echo "<div class=\"column23r \">If you entered more than one word, try
using just the single most important word. You might also try browsing through
the entire <a href=\"searchbycategory.php\">category.</a> If all else fails, you
can try to find the food by using the Google search box, above. If you will, send
us an email telling us what food you were unable to find, and we\'ll see if we
can add it.</div>";
}
while ($row = mysql_fetch_array ($result)) {
$id = $row['id'];
$food_name = $row['name'];
$ar = explode(',',$food_name);
$br = implode(' ',$ar);
***(Puts spaces after commas for database entries. I've since learned an easier way to do this)
echo "<span class=\"ls\">$count.) <a href=\"result_display.php?id=" . $id . "\">$br</a></span><br>";
$count++ ;
}
$current_page = (($s/$limit) + 1);
echo "<br />";
$a = $s + ($limit) ;
if ($a > $numrows) { $a = $numrows ; }
$b = $s + 1 ;
if ($s>=1) {
$prevs=($s-$limit);
if (isset($var2, $var1, $var)) {
echo "<div class=\"fl\"><a href=\"$PHP_SELF?s=$prevs&name=$var&name1=$var1&name2=$var2&
limit=$limit\"> << Previous $limit</a></div>";
}elseif (isset($var1, $var)) {
echo "<div class=\"fl\"><a href=\"$PHP_SELF?s=$prevs&name=$var&name1=$var1&limit=$limit\"> <<
Previous $limit</a>>/div>";
} else {
echo "<div class=\"fl\"><a href=\"$PHP_SELF?s=$prevs&name=$var&limit=$limit\"> << Previous $limit</a></div>";
}
}
$pages=intval($numrows/$limit);
if ($numrows%$limit) {
$pages++;
}
if (!((($s+$limit)/$limit)==$pages) && $pages!=1)
$news=$s+$limit;
if ($current_page < $pages) {
if (isset($var2, $var1, $var)) {
echo "<div class=\"fr\"><a href=\"$PHP_SELF?s=$news&name=$var&name1=$var1&name2
=$var2&limit=$limit\">Next $limit >></a></div><br>";
}elseif (isset($var1, $var)) {
echo "<div class=\"fr\"><a href=\"$PHP_SELF?s=$news&name=$var&name1=$var1&limit=$limit\">
Next $limit >></a></div><br>";
} else {
echo "<div class=\"fr\"><a href=\"$PHP_SELF?s=$news&name=$var&limit=$limit\">Next $limit
>></a></div><br>";
}
}
?>
<div class = "clear"></div></div>
<!-- FORM -->
<div class="center_panel">
<h3>Search Foods By Name</h3>
<form name="form" action="result_search_food.php" method="get" >
* * * Substantive input form
<div class="fl">How many results would you like to display per page? (We suggest 30.)</div>
<div class="fr"><input type="text" name="limit" size="3" maxlength="3" value="
<?php
if (isset($_GET['limit'])) {
$limsafe = htmlentities($_GET['limit']);
echo $limsafe;
}else{
echo '30';
}
?>
"> </div><br><br><br><div><input type="submit" class="submit" name="Submit" value="Search"></div>
</form>
* * *
<p>The last box should contain a number between 1 and 999.</p>
<div class = "clear"></div></div>
<?php
include_once('./footer_article_food.inc');
?>
|