php simple math problem

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
jphamac
Forum Newbie
Posts: 6
Joined: Fri Sep 23, 2005 11:56 pm

php simple math problem

Post 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";
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Re: php simple math problem

Post 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:
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
jphamac
Forum Newbie
Posts: 6
Joined: Fri Sep 23, 2005 11:56 pm

Post 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'
jphamac
Forum Newbie
Posts: 6
Joined: Fri Sep 23, 2005 11:56 pm

Post 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";
Post Reply