Gents,
I want to be able to provide my users with a simple search box which allows them to type in and it goes off and searches the main fields of the table and displays the results.
Here is what I have so far.
As you can see, here I am searching by username. I just wanted to check it worked, and it does.
so firstly, I want to be able to search all fields, not just surname. Can I add to it like this?
??
Or do I have to make a query for each one?
Secondly, when the results are displayed, I'd like to make the results a clickable link to show the full result (all fields). Is that possible?
I have set it up so that results are limited to 10 and then spread across pages with next/back links although i'm not sure I'll ever need that.
I want to be able to provide my users with a simple search box which allows them to type in and it goes off and searches the main fields of the table and displays the results.
Here is what I have so far.
Code:
<html>
<body>
<form name="form" action="" method="get">
<input type="text" name="q" />
<input type="submit" name="Submit" value="Search" />
</form>
<?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))
{
echo "<p>We dont seem to have a search parameter!</p>";
exit;
}
//connect to database
mysql_connect("localhost","root","root"); //(host, username, password)
//specify database
mysql_select_db("elite") or die("Unable to select database");
// SQL Query
$query = "SELECT * FROM contacts WHERE surname LIKE \"%$trimmed%\"
ORDER BY id ASC";
$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);
// 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($query) or die("Couldn't execute query");
// display what the person searched for
echo "<p>You searched for: "" . $var . ""</p>";
// begin to show results set
echo "Results";
$count = 1 + $s ;
// display the results returned
while ($row= mysql_fetch_array($result)) {
$title = $row["surname"];
echo "$count.) $title" ;
$count++ ;
}
$currPage = (($s/$limit) + 1);
//break before paging
echo "<br />";
// links to other results
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>  ";
}
// 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%$limit) {
// has remainder so add one page
$pages++;
}
// check to see if last page
if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {
// last page so give NEXT link
$news=$s+$limit;
echo " <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>";
?>
</body>
</html>
As you can see, here I am searching by username. I just wanted to check it worked, and it does.
so firstly, I want to be able to search all fields, not just surname. Can I add to it like this?
Code:
// SQL Query
$query = "SELECT * FROM contacts WHERE surname, first_name, address, email LIKE \"%$trimmed%\"
ORDER BY id ASC";
Or do I have to make a query for each one?
Secondly, when the results are displayed, I'd like to make the results a clickable link to show the full result (all fields). Is that possible?
I have set it up so that results are limited to 10 and then spread across pages with next/back links although i'm not sure I'll ever need that.