Welcome to WebmasterWorld Guest from 23.20.18.183

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Retrieving numbers within a certain range

     
6:28 am on Aug 24, 2009 (gmt 0)

New User

5+ Year Member

joined:June 25, 2009
posts: 34
votes: 0


I have a form where users can submit an air quantity as a number. The database has rows such as id, air_quantity, product name etc.

What I am trying to do is when a user subitted the number, it should actually return values within a certain range which would be (id -1, and id +2).

So, the output would be 4 rows (one below, the closest search value, two rows above the search value).

What would be the easiest way to get such an output?

Any help would be great!

7:12 am on Aug 24, 2009 (gmt 0)

Junior Member

5+ Year Member

joined:July 8, 2008
posts:80
votes: 0


can you pls explain the question a bit more
7:13 am on Aug 24, 2009 (gmt 0)

Preferred Member

10+ Year Member

joined:Jan 4, 2005
posts:621
votes: 0


$bottom = $_POST['air_quantity'] - 1
$top1 = $_POST['air_quantity'] + 1
$top2 = $_POST['air_quantity'] + 2

select * from table_name where air_quantity = $_POST['air_quantity'] or
air_quantity = $bottom or air_quantity = $top1 or air_quantity = $top2

Will that work?

Not pretty but it should do the trick.

11:31 pm on Aug 24, 2009 (gmt 0)

New User

5+ Year Member

joined:June 25, 2009
posts: 34
votes: 0


Thanks, I tried this but it doesn't give me the results I wish.

The problem is, I have certain values such as 45, 55, 80 in the database and if someone puts in 46 it doesn't give me any results.
It should look for the closest matching and then look for the one below and the two upper values and return the 4 values.

6:25 am on Aug 25, 2009 (gmt 0)

Junior Member

5+ Year Member

joined:July 8, 2008
posts:80
votes: 0


$usr_val = $_POST['num']; //number entered by user

$val_below = $usr_val-1;
$val_upper1 = $usr_val+1;
$val_upper2 = $usr_val+2;

$query = "select * from table_name where column_name = '$usr_val' or column_name = '$val_below' or column_name = '$val_upper1' or column_name = '$val_upper2'";

run this query it should work.

6:43 am on Aug 25, 2009 (gmt 0)

New User

5+ Year Member

joined:June 25, 2009
posts: 34
votes: 0


Thanks Nil111, I fixed it and it runs smoooth!