Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Wildcard Question

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Wildcard Question

Postby shibumi » Sun Nov 24, 2002 9:50 pm

This is a little example of a zip code finder I am working on. The problem I am having is (I think) the wildcard or % symbol. When I type in 98 for example on the form page, I get all the zip codes in 98***, but if I type in any more numbers (like the whole zip code) I get nothing. I would like to be able to input all five numbers on the form page and have it still return results based on only the first two digits. Was hoping someone could lend a bit of insight on this one, I have been staring at it all day... Anyway, here's what I have so far:

Code: Select all
$db = mysql_connect("xxxxxx", "xxxxxx", "xxxxxx");
mysql_select_db("xxxxxx",$db);
// display individual record
if ($id) {
$result = mysql_query("SELECT * FROM zip WHERE id=$id",$db);
$myrow = mysql_fetch_array($result);
printf("Name: %s\n<br>", $myrow["name"]);
printf("Address: %s\n<br>", $myrow["address"]);
printf("City: %s\n<br>", $myrow["city"]);
printf("State: %s\n<br>", $myrow["state"]);
printf("Zip Code: %s\n<br>", $myrow["zipcode"]);
} else {
// show store list
$twodigits = substr($_POST['zip_code'], 2);
$result = mysql_query("SELECT * FROM zip WHERE zipcode LIKE '$twodigits%'",$db);
if ($myrow = mysql_fetch_array($result)) {
// display list of records
do {
printf("<a href=\"%s?id=%s\">%s</a><br>\n", $PHP_SELF, $myrow["id"], $myrow["name"]);
} while ($myrow = mysql_fetch_array($result));
} else {
// no records to display
echo "Sorry, no records were found!";
}
}
shibumi
New php-forum User
New php-forum User
 
Posts: 2
Joined: Sun Nov 24, 2002 9:41 pm
Location: Seattle, Wa

Postby serg_dz » Mon Nov 25, 2002 12:59 am

To my mind problem is there:

Code: Select all
$twodigits = substr($_POST['zip_code'], 2);


As far as I remember the syntax of substr is:

string substr ( string string, int start [, int length])

example:
Code: Select all
$rest = substr("abcdef", 1, 3); // returns "bcd"


Try this, may be this is an answer.
serg_dz
New php-forum User
New php-forum User
 
Posts: 13
Joined: Thu Oct 10, 2002 10:37 pm
Location: Ukraine

Postby shibumi » Mon Nov 25, 2002 2:02 am

Intersting approach. I am pretty new to the whole php coding, so this is part of my learning experience. Anyway, I am not familiar with the substr much. I just did some reading on it and got a little more out of it, though it didn't quite work out with my code. When I replaced the code you pointed out:

Code: Select all
$twodigits = substr($_POST['zip_code'], 2);


With the substr you provided, it simply displayed all results in the database. I will look into it more, but if you have any suggestions I would welcome them. Thanks! SO much to learn...
shibumi
New php-forum User
New php-forum User
 
Posts: 2
Joined: Sun Nov 24, 2002 9:41 pm
Location: Seattle, Wa


Return to PHP coding => General

Who is online

Users browsing this forum: Google [Bot], Google Feedfetcher and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.