June 23, 200818 yr I have this form: It is a HTML form that allows the user to select ONE OR MORE options in each box. I have the user's choices posting to a php document that will then run an SQL query to return the firearms that match their criteria. I have all the user's criteria assigned to PHP variables and ready to plug into an SQL Query. But that's where I need some help! It needs to work as such: SELECT *(all fields) FROM firearms (table) WHERE action= bolt OR lever AND make= tikka OR weatherby OR gamo AND calibre= 222 OR 270 OR 308 OR 338 AND stock= wood OR synthetic AND barrel= stainless steel Obviously this is not the correct syntax, and the WHERE criteria will change as users select different choices. Can anyone help me to create the correct query that will satisfy the user's changing criteria? Here is a snippet of the PHP I've been playing with showing my $variable arrays: $option_action = $_POST['option_action']; $option_make = $_POST['option_make']; $option_calibre = $_POST['option_calibre']; $option_stock = $_POST['option_stock']; $option_barrel = $_POST['option_barrel']; $query = "SELECT * FROM firearms where make IN ('$option_make[0]', '$option_make[1]', '$option_make[2]', '$option_make[3]', '$option_make[4]') ;
June 23, 200818 yr Not sure how your fields are in the database ... but here's a quickie to help you: //suppose you have a field with multiple selection options named FIELD here's a quick way to build the where statement for it: //$_POST['field'] will always be an array $field = $_POST['field']; //first check if the array has more than 1 element if (count($field) > 1) { $where_field = ' AND field IN('.implode(',',$field).') '; //see php.net for implode } else //array has only 1 element so no need for commas separating the fields { $where_field = " AND field = ' ".$field[0]." ' "; } After you check all the fields you can build your sql like this: SELECT ........ FROM ...... WHERE 1=1 //dummy where so you can have all the other WHERES using AND $where_field1 $where_field2 etc Hope this makes sense and helps. Good luck
June 24, 200818 yr Author I didn't manage to get this working completely. I edited your ELSE statement to this to get it working: $where_option_action = " AND action = '$option_action[0]' "; But I still can't seem to get the IF statement working. I broke it into two lines to help me understand it: $temp = implode(',',$option_action); $where_option_action = " AND action IN ('$temp') "; I cannot work out how to get the $temp variable to become read as a variable. The SQL IN command requires formatting like this: (' criteria ') to work, so I need two more 's around the $temp, right? Sorry if I'm on the wrong track, I'm new to this stuff. Jarrad
June 24, 200818 yr I cannot work out how to get the $temp variable to become read as a variable. 2 ways you can do that: - use double quotes: $var = " here is another $variable"; //this will not work $var = 'here is another $variable'; - concatenate (use the dot . ): $variable = "ABCD"; $var = "this is a string ' ".$variable. " ' blah blah"; echo $var; will output "this is a string 'ABCD' blah blah" The SQL IN command requires formatting like this: (' criteria ') to work, so I need two more 's around the $temp, right? Yes, you should use double quotes for the sql string and single quotes for the $temp.
June 24, 200818 yr Author Thankyou for your reply. I'll revisit this today and see if I can get it working properly. Much appreciated, Jarrad
June 25, 200818 yr Author Attached is the code showing how I have got it to work with the first of 5 option fields: $option_action = $_POST['option_action']; $option_make = $_POST['option_make']; $option_calibre = $_POST['option_calibre']; $option_stock = $_POST['option_stock']; $option_barrel = $_POST['option_barrel']; if (count($option_action) > 1) { $temp = implode("', '",$option_action); $where_option_action = " AND action IN ('".$temp."')"; } else { $where_option_action = " AND action = '$option_action[0]' "; } $query = "SELECT * FROM firearms WHERE 1=1 $where_option_action "; I will replicate this for the other option_fields and try to optimize it into a function (If I can! ) Thanks again for your help Mihai, its great to see people still have time for the 'newbies'! Regards, Jarrad
Create an account or sign in to comment