silverglade Posted September 4, 2011 Share Posted September 4, 2011 I was wondering if this is possible. I have a list of criteria each with their own checkboxes, and I want to query a database when one or more of the checkboxes is submitted. So if 3 are checked, I want to query the database to find those 3 items. If one only is checked, I want to query the dataabse for one item. any ideas greatly appreciated thank you. this is basically a criteria based search Quote Link to comment https://forums.phpfreaks.com/topic/246389-multiple-checkboxes-grouping/ Share on other sites More sharing options...
wright67uk Posted September 4, 2011 Share Posted September 4, 2011 If each checkbox were to have a value and a variable $box1 = $_POST["box1"]; $box2 = $_POST["box2"]; $box3 = $_POST["box3"]; Then could your query be; mysql_query("select data WHERE $box1, $box2 = 'no' $box3 = 'yes' ") and then set up your database to have to have columns with either yes or no within in them? You will have to work on the sql side of things, as I'm not sure about the correct synyax. Quote Link to comment https://forums.phpfreaks.com/topic/246389-multiple-checkboxes-grouping/#findComment-1265278 Share on other sites More sharing options...
silverglade Posted September 4, 2011 Author Share Posted September 4, 2011 I think that means if I were to have 15 search criteria checkboxes, wouldn't that be a lot of query statements? like for checkbox 1 , yes, checkbox 2 no 3 no 4 no 5 no..... and then for checkbox 2, it would have to be 1 no 3 no 2 yes. or what if I wanted to check 3 and query the database with 3 terms. Then it could be 1 yes, 2 yes, 3 yes, all the rest no. the combinations are too much. please any more advice appreciated. thanks. Quote Link to comment https://forums.phpfreaks.com/topic/246389-multiple-checkboxes-grouping/#findComment-1265281 Share on other sites More sharing options...
wright67uk Posted September 4, 2011 Share Posted September 4, 2011 How about putting each result into an array and then mysql_query( "select * from ourtablename where col1=" . $_POST['box1'][2][3]. " and col2='" . $_POST['box2'][2][3] . "'"); Quote Link to comment https://forums.phpfreaks.com/topic/246389-multiple-checkboxes-grouping/#findComment-1265287 Share on other sites More sharing options...
cyberRobot Posted September 4, 2011 Share Posted September 4, 2011 Could you provide a sample of what your database look like? If you have a database where one column contains one of the checkbox values, the query would use "OR". <?php $sql = "SELECT * FROM your_table_name WHERE column_to_search='" . $_POST['checkbox1'] . "' or column_to_search='" . $_POST['checkbox2'] . "'"; mysql_query($sql); ?> Note that you could use a look to generate the WHERE clause to test for the various checkbox values. Also you'll want to validate the checkbox values before using them against the database. Otherwise, you'll want to use the mysql_real_escape_string() function: http://php.net/manual/en/function.mysql-real-escape-string.php Quote Link to comment https://forums.phpfreaks.com/topic/246389-multiple-checkboxes-grouping/#findComment-1265336 Share on other sites More sharing options...
silverglade Posted September 4, 2011 Author Share Posted September 4, 2011 Ok , I would like to have checkboxes with the following database criteria. Upon submission of the form, it should query the database for matches where they were checked. But if I check one, I want results just for that one. If I check 3, I want results for only those 3. Here is the database I have. Any coding advice greatly appreciated. thank you. CREATE TABLE `users` ( `id` int(11) NOT NULL auto_increment, `status` varchar(20) NOT NULL, `lastname` varchar(50) NOT NULL, `dob` varchar(20) NOT NULL, `username` varchar(20) NOT NULL, `password` varchar(150) NOT NULL, `email` varchar(30) NOT NULL, `activationkey` varchar(100) NOT NULL, `country` varchar(30) NOT NULL, `state` varchar(30) NOT NULL, `town` varchar(30) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `username` (`username`), UNIQUE KEY `email` (`email`), UNIQUE KEY `activationkey` (`activationkey`) ) ENGINE=MyISAM AUTO_INCREMENT=63 DEFAULT CHARSET=latin1 AUTO_INCREMENT=63 ; Quote Link to comment https://forums.phpfreaks.com/topic/246389-multiple-checkboxes-grouping/#findComment-1265445 Share on other sites More sharing options...
cyberRobot Posted September 5, 2011 Share Posted September 5, 2011 Ok , I would like to have checkboxes with the following database criteria. Upon submission of the form, it should query the database for matches where they were checked. But if I check one, I want results just for that one. If I check 3, I want results for only those 3. You should be able to modify the code I provided earlier to fit your needs. I'm not sure which column is being used to search for matches, I also don't know what the form code looks like. Basically, all you need to do is something like: <?php $sql_combine = ''; //variable to add " OR " between the various checkbox queries; note that it's set to nothing since we don't want the an or before the first test $sql = "SELECT * FROM users WHERE "; if($_POST['checkbox1']) { $sql.=$sql_combine."status='".$_POST['checkbox1']."'"; $sql_combine=' OR '; } if($_POST['checkbox2']) { $sql.=$sql_combine."status='".$_POST['checkbox2']."'"; $sql_combine=' OR '; } mysql_query($sql); ?> You'll need to replace "status" with the column that corresponds with the checkboxes and change the $_POST['checkbox1'] variable to whatever you named the checkboxes. Also, if the form uses the GET method, you'll need to use the $_GET array. Quote Link to comment https://forums.phpfreaks.com/topic/246389-multiple-checkboxes-grouping/#findComment-1265493 Share on other sites More sharing options...
silverglade Posted September 5, 2011 Author Share Posted September 5, 2011 awesome thanks very much for taking the time to help me out with that code as I would not have come up with it on my own. I will see how it does, at least I know what I should be doing now. I have to work on my logic skills , and get more creative with applying what I already know. Thanks very much for your help! :) Quote Link to comment https://forums.phpfreaks.com/topic/246389-multiple-checkboxes-grouping/#findComment-1265538 Share on other sites More sharing options...
cyberRobot Posted September 5, 2011 Share Posted September 5, 2011 No problem, hope everything works out. Quote Link to comment https://forums.phpfreaks.com/topic/246389-multiple-checkboxes-grouping/#findComment-1265673 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.