PHP/MySQL/Ajax checkbox filter

Hello there!

Im developing a PHP webpage having multiple checkboxes divided into 4 parts i.e. brand|budget|key featues|additional features. In Key feature I have 19 checkboxes, the value thereof have been fetched with Ajax to PHP in a single variable i.e. one string. Now in PHP I want to generate mysql query filtering data on the basis of checkbox selection. I am able to filter one checkbox each but how can I able to filter using multiple checboxes checked and generate query accordingly. As far as mysql data is concerned, ive only two table one for products and one for product specifications.

If you’re sending checkboxes selection to php as 1 string, I believe there is some kind of delimiter? For example, if you have your string constructed like this:

1,5,8,12

You can filter data in php like this:
[php]$keys = $_REQUEST[‘keys’];
if($keys!=’’){
$r = mysql_query(“SELECT * FROM products WHERE id IN (”.$keys.")");
}[/php]

If delimiter is not comma, you can just str_replace() your delimiter to comma. Or use explode() to split string into array, and then generate sql query as you need.

Hello phphelp!
Thanks for your reply… I’ve already availed what you have suggested but not getting the desired result. Actually what Im trying to do is basically filtering the data on the basis of checkbox selected. Suppose if a checkbox selected is Hi-Fi System it will fetch data from db as well as if checkbox selected is Portable it will also fetch data from db but how will i code to fetch data from db when both are checked. PS: There is no submit button in frontend but as any checkbox is checked the request is being sent to php via ajax and response generated and ofcourse it is just one part of the picture what Im trying to do. There is lot of thing which is yet to go but rightnow I am stuck on it…not getting clue what to do??? Thankx

I assumed that you send all selected checkboxes to php (every time when any of checkboxes is clicked). If so, this sql query will select products for multiple checboxes:

SELECT * FROM products WHERE KeyFeature IN (1,5,8,12) AND Brand IN (1,2) AND Budget IN (2,3)[code]

Thankx phphelp!

Ya, im aware of sql in clause with where but ive my data not only in one column but it is within 6-7 columns. Where Im lacking is that i cant figure out how to process if ive two checkbox values either in string or in array, meaning thereby if user check ‘A’ checkbox value suppose ‘a’ will post/pass and next if user check ‘B’ value ‘b’ will post/pass then how to handle both with ifelse so that if user click third one i know how to proceed further…hope i am clear to you. BTW Thanks for your help…appreciate your response…

Not sure if I understand your ajax sending process, but what I suggest is: when user check the A checkbox, and then check the B checkbox - every time you need to send to php ALL the selected values, not just a or b. So, if user check the C checkbox, you need to send to php script the string of: a,b,c

Otherwise, you need to implement some custom caching. Say store user selection in cookie, so that when they check C checkbox, you first read cookie (where by this moment you should have a,b) and then use all a,b,c for querying database.

Thankx.

FYI Im sending checkbox values appending to url comma delimiting/separating and getting at other side in php very accurately on each checkbox click like if a then only a and a which is already checked (due to ajax page does’nt refresh and stay there) and then check b, i will get on other side a,b and so on. However, I have used both techniques string or array but only able to filter one checkbox each. But now I am left with only below given idea which is according to me a static one but I want dynamic.

And BTW if i have 5 checkbox then how many combinations will be…!!!

[php]
$key_feature=$_GET[‘url_3’]//string coming from frontend with all values that are checked comma separated

if($key_feature=="‘Hi-Fi System’" || $key_feature=="‘Portable’" || $key_feature=="‘Hi-Fi System’,‘Portable’")
{
$sql=“select count(product_id) from t_audio_detail where type in($key_feature)”;
$res_a=mysql_query($sql);
}
else if($key_feature=="‘usb_port’" || $key_feature=="‘component_video_output’" || $key_feature=="‘usb_port’,‘component_video_output’")
{
if($key_feature=="‘usb_port’")
{
$sql=“select count(product_id) from t_audio_detail where usb_port=‘Yes’”;
}
else if($key_feature=="‘component_video_output’")
{
$sql=“select count(product_id) from t_audio_detail where component_video_output=‘Yes’”;
}
else if($key_feature=="‘usb_port’,‘component_video_output’")
{
$sql=“select count(product_id) from t_audio_detail where usb_port=‘Yes’ and component_video_output=‘Yes’”;
}
$res_a=mysql_query($sql);
}
if($res_a) {$l=mysql_fetch_array($res_a); echo $l[‘count(product_id)’];} else {echo $l=‘0’;} die();
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service