以下脚本仅显示第一页,并且指向下一页的链接,但不在任何地方。有人能帮我吗?
$var = @$_GET['q'] ;
$trimmed = trim($var);
$limit = 10;
if ($trimmed == "")
{
echo "<p>What are you looking for?...</p>";
exit;
}
if (!isset($var))
{
echo "<p>We dont seem to have a search parameter!</p>";
exit;
}
mysql_connect('xxx', 'yyy', 'zzz');
mysql_select_db('yyy') or die('Unable to select database');
$query = "select * from table where NAME like '%$trimmed%' order by NAME";
$numresults = mysql_query($query);
$numrows = mysql_num_rows($numresults);
if ($numrows == 0)
{
echo "<h4>Results</h4>";
echo "<p>Sorry, your search: "" . $trimmed . "" returned zero results</p>";
echo "<p><a href=\"http://www.google.com/search?q="
. $trimmed . "\" target=\"_blank\" title=\"Look up
" . $trimmed . " on Google\">Click here</a> to try the search on google</p>";
}
if (empty($s))
{
$s = 0;
}
$query .= " limit $s,$limit";
$result = mysql_query($query) or die("Couldn't execute query");
echo "<p>You searched for: "" . $var . ""</p>";
echo "Results";
$count = 1 + $s;
while ($row= mysql_fetch_array($result))
{
$title = $row["NAME"];
echo "$count.- $title" ;
$count++ ;
}
$currPage = (($s/$limit) + 1);
echo "<br />";
if ($s >= 1)
{
// bypass PREV link if s is 0
$prevs = ($s - $limit);
print " <a href=\"$PHP_SELF?s=$prevs&q=$var\"><<
Prev 10</a> ";
}
$pages = intval($numrows/$limit);
if ($numrows % $limit)
{
$pages++;
}
if (!((($s+$limit)/$limit) == $pages) && $pages != 1)
{
$news = $s + $limit;
print "<a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 >></a>";
}
$a = $s + ($limit);
if ($a > $numrows)
{
$a = $numrows;
}
$b = $s + 1;
echo "<p>Showing results $b to $a of $numrows</p>";
答案 0 :(得分:1)
在您的代码中,每次重新加载页面或单击下一页链接时,$s
都会重置。您应该在代码的开头有$s = $_REQUEST['s']
。
答案 1 :(得分:0)
PHP_SELF
是一个$_SERVER
变量。无论如何,你应该在这里使用$_SERVER['SCRIPT_NAME']
。
echo "<a href=\"{$_SERVER['SCRIPT_NAME']}?s=$news&q=$var\">Next 10 >></a>";
在一些值得注意的改进中,你应该真正考虑使用urlencode()
清理你在查询字符串中放置的变量。