だから私はFrostによって丁寧に提供されたクエリの例を持っています. 私は 1 週間ウェブを精査してきましたが、非常に多くの異なる例があり、それらの多くを試してみましたが、ほとんどまたはまったく成功しませんでした. 私は最終的にあなたが以下に示すものを構築することができました. それらが機能しない方法は、「次へ」をクリックすると2ページに移動しますが、結果が表示されず、3ページを期待してもう一度クリックしてもそこに移動せず、2ページにとどまることです.私は本当に撮りたくありませんでした.誰の貴重な時間でも、私はとても失われています。「私の」コードは次のとおりです。
<?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));
}
?>