所以我有一个由Frost慷慨提供的查询示例,它帮助我学到了很多东西(我非常喜欢新手),但我仍然试图添加分页。我已经在网上搜索了一个星期,并且有很多不同的例子,我尝试了很多这些例子很少或没有成功。我终于能够构建你在下面看到的内容,问题是当你点击搜索时它确实只返回20条记录,但页面不起作用。他们不工作的方式是,当你点击“下一步”时,它将转到第二页,但没有结果显示,如果你再次点击期望第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));
}
?>
答案 0 :(得分:0)
你有多个问题。第一个是您使用表单提交来获取选项中的值(这很好),但链接“下一页”只提供page_id而不是所有其他选项。 (您可以将它们放在下一个链接中(因为您仍然使用_GET)或将提交的值存储在会话中。我更喜欢后者。)
另一个问题是,你做了一些分页计算,但是你从不使用你计算的数字。你必须输入一个mysql LIMIT语句。 (正确的点是在“ORDER BY”之后。对于那个替换
" ORDER BY `Pet_Name`";
通过
" ORDER BY `Pet_Name` LIMIT ". $mysql_limit .",". $page_limit;
但请记住,这只会修复问题2,你仍然需要把那些_GET变量放到“下一页”中
答案 1 :(得分:0)
更改
print '<a href="registry.php?page_number='.$suivant.'">next page</a>'; //The link for the next pages
以强>
print '<a href="registry.php?page_number='.$next.'">next page</a>'; //The link for the next pages