glennn.php Posted September 11, 2008 Share Posted September 11, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/123717-finding-a-value-in-a-db-thats-within-a-range/ Share on other sites More sharing options...
Zane Posted September 11, 2008 Share Posted September 11, 2008 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"; Quote Link to comment https://forums.phpfreaks.com/topic/123717-finding-a-value-in-a-db-thats-within-a-range/#findComment-638827 Share on other sites More sharing options...
glennn.php Posted September 11, 2008 Author Share Posted September 11, 2008 oh, right. cool, thanks... Quote Link to comment https://forums.phpfreaks.com/topic/123717-finding-a-value-in-a-db-thats-within-a-range/#findComment-638829 Share on other sites More sharing options...
Zane Posted September 11, 2008 Share Posted September 11, 2008 are we Solved? Quote Link to comment https://forums.phpfreaks.com/topic/123717-finding-a-value-in-a-db-thats-within-a-range/#findComment-638842 Share on other sites More sharing options...
glennn.php Posted September 11, 2008 Author Share Posted September 11, 2008 not yet, thanks - i'll close it if this works - i'm still working on the array... Quote Link to comment https://forums.phpfreaks.com/topic/123717-finding-a-value-in-a-db-thats-within-a-range/#findComment-638843 Share on other sites More sharing options...
Zane Posted September 11, 2008 Share Posted September 11, 2008 word Quote Link to comment https://forums.phpfreaks.com/topic/123717-finding-a-value-in-a-db-thats-within-a-range/#findComment-638845 Share on other sites More sharing options...
glennn.php Posted September 11, 2008 Author Share Posted September 11, 2008 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... Quote Link to comment https://forums.phpfreaks.com/topic/123717-finding-a-value-in-a-db-thats-within-a-range/#findComment-638856 Share on other sites More sharing options...
Zane Posted September 11, 2008 Share Posted September 11, 2008 what does your checkbox-ing form look like.......the HTML Quote Link to comment https://forums.phpfreaks.com/topic/123717-finding-a-value-in-a-db-thats-within-a-range/#findComment-638859 Share on other sites More sharing options...
glennn.php Posted September 11, 2008 Author Share Posted September 11, 2008 <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> Quote Link to comment https://forums.phpfreaks.com/topic/123717-finding-a-value-in-a-db-thats-within-a-range/#findComment-638862 Share on other sites More sharing options...
glennn.php Posted September 11, 2008 Author Share Posted September 11, 2008 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 ...? Quote Link to comment https://forums.phpfreaks.com/topic/123717-finding-a-value-in-a-db-thats-within-a-range/#findComment-638884 Share on other sites More sharing options...
Zane Posted September 11, 2008 Share Posted September 11, 2008 you need to make your script do this enrollment BETWEEN 201 and 500 OR BETWEEN 501 and 1000 OR etc... Quote Link to comment https://forums.phpfreaks.com/topic/123717-finding-a-value-in-a-db-thats-within-a-range/#findComment-638885 Share on other sites More sharing options...
glennn.php Posted September 11, 2008 Author Share Posted September 11, 2008 ok thanks... Quote Link to comment https://forums.phpfreaks.com/topic/123717-finding-a-value-in-a-db-thats-within-a-range/#findComment-638887 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.