Jump to content

finding a value in a db that's within a range


glennn.php

Recommended Posts

ok, last one - ya'll have been very helpful to me, thanks...

 

i've been given a form configuration to do the backend on, and one of the options is to select from a range of numbers, or from multiple ranges of numbers, i.e.

 

checkboxes:

1 - 200

201 - 500

501 - 1000

1001 - 2000...

 

the values in the database field "enrollment" would be something like 158 or 854 or 1510; i need the search to find ALL the records where 'enrollment' is between 501-1000, etc, and i have no idea how to even configure the checkbox values OR do the query.

 

i can do simple arrays, but this one's over my head.

 

can someone lend a hand? suggest an easy way to do this search?

 

Thanks,

GN

 

 

use the BETWEEN operator in MySQL

 

for every checkbox that's checked just concatenate your SQL statement with

$sql .= "enrollment BETWEEN 1 and 200";

$sql .= "enrollment BETWEEN 201 and 500";

$sql .= "enrollment BETWEEN 501 and 1000";

$sql .= "enrollment BETWEEN 1001 and 2000";

 

 

here's how i'd like to fit the array into what i have for all the other arrays i have:

 

an example (eight others like this):

$offering = $_POST['highest_deg'];
if ($offering != '') {
$offerings = implode(',',$offering);
} else {
if ($offering == '') {
$offerings = '';
}
}
if ($offerings != '') {
$where[] = "offering IN ($offerings)";
} 

select * from table WHERE $where...

 

so i'm trying something like:

 

$enrollment = $_POST['enrollment'];
if ($enrollment != '') {
$enrollment = implode(',',$enrollment);
} else {
if ($enrollment == '') {
$enrollment = "";
}
}

if ($enrollment != '') {
$where[] = "enrollment BETWEEN $enrollment";
} 

 

but i can't see how to configure this array - the others just need an IN operator for 01,02... i made the checkbox values

 

1 and 200

201 and 500

501 and 1000...

but that didn't quite work out with more than one selection...



                      <tr>
                        <td width="20" align="left" valign="middle"><input type="checkbox" name="enrollment[]" value="1 and 200"
                         <?php echo in_array('1 and 200', $_POST['enrollment']) ? ' checked="checked"' : ''; ?> />
                        </td>
                        <td align="left" valign="middle">1 - 200 </td>
                      </tr>
                      <tr>
                        <td width="20" align="left" valign="middle"><input type="checkbox" name="enrollment[]" value="201 and 500"
                        <?php echo in_array('201 and 500', $_POST['enrollment']) ? ' checked="checked"' : ''; ?> />
                        </td>
                        <td align="left" valign="middle">201 - 500 </td>
                      </tr>



i've tried

 

enrollment BETWEEN $enrollment

 

returns

 

enrollment BETWEEN 201 and 500, 501 and 1000;

 

with errors - is there a reason i can't use strings like with an IN operator? problem with spaces ...?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.