Jump to content

multiple checkboxes grouping


silverglade

Recommended Posts

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

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 ;

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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! :) :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.