The search function on my website already works (sort of). It's a simple search by zip-code, get database results. I took the code from a tutorial site and to tweaked it to fit the needs of my site. (I'm very new to PHP so forgive the poor execution).
I've been able to add items to the database with mySQL and have had no problems with that, and when I type a zip code in, it works as i'd imagine it. The only problem is, it's not matching up exactly to the zip code. E.G. If I type in the number "6" into the search field. I'll get every single zip code with the number 6 in it.
My question is, is there any way to set some kind of command that makes it so someone's search has to be exactly the 5 numbers in the zip code, in order for those results to display.
Thanks again for any help!
Here is the code I am using. It's attached to a text field form as "action=search.php". (Sorry, I know it's probably very ugly.)
Oh and one extra last thing. If anyone can teach me how to get something like a "Next 10 Results >>" link after a certain amount of results have been displayed, that would be amazing as well!
Code: Select all
<?php
// Get the search variable from URL
$var = @$_GET['q'] ;
$trimmed = trim($var); //trim whitespace from the stored variable
// rows to return
$limit=10;
?>
<?php
if ($trimmed == "")
{
echo "<p>Please enter a search...</p>";
exit;
}
if (!isset($var))
{
echo "<p>We dont seem to have a search parameter!</p>";
exit;
}
//connect to your database ** EDIT REQUIRED HERE **
mysql_connect("localhost", "root", "*******"); //(host, username, password)
//specify database ** EDIT REQUIRED HERE **
mysql_select_db("*******") or die("Unable to select database"); //select which database we're using
// Build SQL Query
$query = "select * from daycares where dc_zip LIKE \"%$trimmed%\"
order by dc_zip"; // EDIT HERE and specify your table and field names for the SQL query
$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);
// 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($query) or die("Couldn't execute query");
// display what the person searched for
echo "<p>You searched for: "" . $var . ""</p>";
$count = 1 + $s ;
while ($row= mysql_fetch_array($result)) {
$name = $row["dc_name"];
$phone = $row["dc_phone"];
$city = $row["dc_city"];
$streets = $row["dc_streets"];
$zip = $row["dc_zip"];
$state = $row["dc_state"];
$license = $row["dc_license"];
$thumb = $row["img_url"];
$url = $row["page_url"];
?>
<!--Begin Result 1-->
<div class="title">
<p class="daycarename"><a href="<?php echo $url; ?>"><?php echo $name; ?></a></p>
<p class="number"><?php echo $phone; ?></p>
</div>
<div class="listing">
<a href="<?php echo $url; ?>"><img src="<?php echo $thumb; ?>" /></a>
<div class="description">
<p><strong>City:</strong> <?php echo $city; ?></p>
<p><strong>Major Cross Streets:</strong> <?php echo $streets; ?></p>
<p><strong>Zip Code:</strong> <?php echo $zip; ?></p>
<p><strong>State:</strong> <?php echo $state; ?></p>
<p><strong>License Number:</strong> <?php echo $license; ?></p>
</div>
<div class="learnmore">
<a href="<?php echo $url; ?>">Learn More...</a>
</div>