Page 1 of 1

php simple math problem

Posted: Fri Oct 14, 2005 8:39 am
by jphamac
I have a "search by zipcode" form. when a visitor types in a zip code, it gets the range of 100 higher and 100 lower. Everything works good except when i use a zipcode with a zero in front.

So when i type in '01423'

the result of the search: 14301, 14253, 14201

Please help.


Here's the code:

Code: Select all

$wildcard = 01423;

$a = $wildcard - 100;
$b = $wildcard + 100;


//Getting 
$c_query="SELECT * FROM user 
		WHERE zip
		BETWEEN $a AND $b 
		ORDER by city ASC";

Re: php simple math problem

Posted: Fri Oct 14, 2005 8:58 am
by Buddha443556
Try this ...

Code: Select all

$wildcard = 01423;

$a = sprintf('%05d', ($wildcard - 100));
$b = sprintf('%05d', ($wildcard + 100));


//Getting 
$c_query="SELECT * FROM user 
		WHERE zip
		BETWEEN '$a' AND '$b' 
		ORDER by city ASC";
EDIT: Thinking in Perl. :oops:

Posted: Fri Oct 14, 2005 9:50 am
by pickle
The problem is that PHP strips off zeros from the beginning of numbers. You could store $wildcard as a string, get the length, then tack on an opening zero if the length isn't the same after the math.

Posted: Fri Oct 14, 2005 10:09 am
by jphamac
Buddha443556

no... I'm still getting the same results.


thank you for your help. I don't know if this makes a difference but the 'zip' column in the db table is 'char'

Posted: Fri Oct 14, 2005 10:51 am
by jphamac
ok, I got it to works with:


Code: Select all

$c_query="SELECT * FROM authuser
WHERE zip >= $a 
AND zip <= $b
ORDER by city ASC";