How to match up a search term exactly?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
roothewhirl
Forum Newbie
Posts: 1
Joined: Wed Dec 23, 2009 1:08 pm

How to match up a search term exactly?

Post by roothewhirl »

Hi there everyone. I've been browsing these boards a long time, but this is my first post here. Thanks to anyone in advance who can figure this out!

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>
 
User avatar
manohoo
Forum Contributor
Posts: 201
Joined: Wed Dec 23, 2009 12:28 pm

Re: How to match up a search term exactly?

Post by manohoo »

There are multiple ways of validating a zip code. The easiest one might be like this:

Code: Select all

 
<?php
 
$zip = $row["dc_zip"];
 
function validateZip($zip)
{
    if (!preg_match('/^[0-9]{5}([-][0-9]{4})?$/',$zip)) {
        echo "invalid ZIP code";
    } 
    else {
        echo $zip. " is a valid ZIP code";
        return $zip;
    }
}
validateZip ($zip);
 
?>
 
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Re: How to match up a search term exactly?

Post by daedalus__ »

you could use javascript to check the length as they press keys like this:

Code: Select all

 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 
<head>
    <title>Conforming XHTML 1.1 Template</title>
    <script type="text/javascript"> 
    function validate_zip()
    {
        form = document.getElementById("blah");
        msg = document.getElementById("zip_msg");
    
        if (form.zip.value.length < 5 || form.zip.value.length > 5)
        {
            msg.innerHTML = "zip code must be five digits" + form.zip.value.length;
        }
        else if (form.zip.value.length == 5)
        {
            msg.innerHTML = "check mark!" + form.zip.value.length;
        }
    }
    </script>
</head>
<body>
    <h1>crappy Example of Javacsript form validation</h1>
    
    <form id="blah" method="post" action="mob.php">
    <span>Zip:</span><input type="text" name="zip" length="5" onkeyup="javascript&#058; validate_zip()" /><span id="zip_msg"></span>
    </form>
</body>
</html>
 
 
its a pretty basic example though
Post Reply