I developed a
keyword search
page to display
results with
page
navigation. the
problem is I
can only show
the first 10
search results
on first page.
then when i
click
"Next
10" to
link next page,
it just sticks
at the first
page and not
going anywhere.
Anyone can give
me a hand?
Thanks. Code is
here.This is
the
form:[code}<
form
name="form
"
action="Te
stresult.php&qu
ot;
method="ge
t">
<input
type="text
"
name="q&qu
ot; />
<input
type="subm
it"
name="Subm
it"
value="Sea
rch"
/>
</form>
div>
Here is the
code for the
search :
Code:
<?php
// Get the
search variable
from URL
$var =
@$_GET['q']
;
$trimmed =
trim($var);
//trim
whitespace from
the stored
variable
// rows to
return
$limit=10;
// check for an
empty string
and display a
message.
if ($trimmed ==
"")
{
echo
"<p>
Please enter a
search...</p
>";
exit;
}
// check for a
search
parameter
if
(!isset($var))<
br />
{
echo
"<p>
We dont seem to
have a search
parameter!</
p>";
exit;
}
$hostname =
"localhost
"; // Our
DB server.
$username =
"xx";
// The username
you created for
this
database.
$password =
"xx";
// The password
you created for
the
username.
$usertable =
"xx";
// The name of
the table you
made.
$dbName =
"xx";
// This is the
name of the
database you
made.
MYSQL_CONNECT($
hostname,
$username,
$password) OR
DIE("DB
connection
unavailable&quo
t;);
mysql_select_db
(
"$dbName&q
uot;) or die(
"Unable to
select
database")
;
// Build SQL
Query
$query =
"select *
from table
where username
like
\"%$trimme
d%\"
order by
username";
// EDIT HERE
and specify
your table and
field names for
the SQL
query
$numresults=my
sql_query($quer
y);
$numrows=mysql
_num_rows($numr
esults);
// If we have
no results,
offer a google
search as an
alternative
if ($numrows ==
0)
{
echo
"<h4>
;Results</h4
>";
echo
"<p>
Sorry, your
search:
""
; . $trimmed .
""
; returned zero
results</p&g
t;";
// google
echo
"<p>
<a
href=\"htt
p://www.google.
com/search?q=&q
uot;
. $trimmed .
"\"
target=\"_
blank\"
title=\"Lo
ok up
" .
$trimmed .
" on
Google\"&g
t;Click
here</a>
to try the
search on
google</p>
;";
}
// next
determine if s
has been passed
to script, if
not use 0
if
(empty($s))
{
$s=0;
}
// get
results
$query .=
" limit
$s,$limit"
;
$result =
mysql_query($qu
ery) or
die("Could
n't execute
query");
// display what
the person
searched for
echo
"<p>
You searched
for:
""
; . $var .
""
;</p>&quo
t;;
// begin to
show results
set
echo
"Results&q
uot;;
$count = 1 + $s
;
// now you can
display the
results
returned
while ($row=
mysql_fetch_arr
ay($result))
{
$title =
$row["user
name"];
echo
"$count.)&
amp;nbsp;$title
" ;
$count++ ;
}
$currPage =
(($s/$limit) +
1);
//break before
paging
echo
"<br
/>";
// next we
need to do the
links to other
results
if ($s>=1)
{ // bypass
PREV link if s
is 0
$prevs=($s-$lim
it);
print
" 
;<a
href=\"$PH
P_SELF?s=$prevs
&q=$var\&qu
ot;><
<
Prev
10</a>&am
p;nbsp 
;";
}
// calculate
number of pages
needing
links
$pages=intval($
numrows/$limit)
;
// $pages now
contains int of
pages needed
unless there is
a remainder
from
division
if
($numrows%$limi
t) {
// has
remainder so
add one page
$pages++;
}
// check to see
if last page
if
(!((($s+$limit)
/$limit)==$page
s) &&
$pages!=1) {
// not last
page so give
NEXT link
$news=$s+$limit
;
echo
" 
;<a
href=\"$PH
P_SELF?s=$news&
amp;q=$var\&quo
t;>Next 10
>>
;</a>&quo
t;;
}
$a = $s +
($limit) ;
if ($a >
$numrows) { $a
= $numrows ;
}
$b = $s + 1
;
echo
"<p>
Showing results
$b to $a of
$numrows</p&
gt;";
?>
|