gigabyt3r Posted October 26, 2009 Share Posted October 26, 2009 Hi, I have several checkboxs on my form, which when no checkboxs are ticked then it displays all the vehicle models, or you can select individual models but it doesn't seem to work the way I want it to. Here is my PHP code; //SELECT ALL MAKES IF NON ARE SELECTED if (!isset($_POST['Ford']) || (!$_POST['Mercedes']) || (!$_POST['Vauxhaull']) || (!$_POST['Citroen']) || (!$_POST['Peugeot']) || (!$_POST['Iveco']) || (!$_POST['FIAT']) || (!$_POST['Nissan']) || (!$_POST['Renault']) || (!$_POST['Volkswagen']) || (!$_POST['DAF, Isuzu, MAN, Moffet, Scania, Toyota, LDV, Land Rover, Daihatsu, Mitsubishi'])) { $Make = "Ford', 'Mercedes', 'Vauxhaull', 'Citroen', 'Peugeot', 'Iveco', 'FIAT', 'Nissan', 'Renault', 'Volkswagen', 'DAF', 'Isuzu', 'MAN', 'Moffet', 'Scania', 'Toyota', 'LDV', 'Land Rover', 'Daihatsu', 'Mitsubishi"; } //Ford Only if (isset($_POST['Submit']) && isset($_POST['Ford'])) { $Make = $Wheelbase."Ford"; } //Mercedes ONLY if (isset($_POST['Submit']) && isset($_POST['Mercedes'])) { // add comma if necessary to separate if (!empty($Make)) { $Make = $Make."', '"; } $Make = "Mercedes"; } //Vauxhaull ONLY if (isset($_POST['Submit']) && isset($_POST['Vauxhaull'])) { // add comma if necessary to separate if (!empty($Make)) { $Make = $Make."', '"; } $Make = $Make."Vauxhaull"; } //Citroen ONLY if (isset($_POST['Submit']) && isset($_POST['Citroen'])) { // add comma if necessary to separate if (!empty($Make)) { $Make = $Make."', '"; } $Make = $Make."Citroen"; } //Peugeot ONLY if (isset($_POST['Submit']) && isset($_POST['Peugeot'])) { // add comma if necessary to separate if (!empty($Make)) { $Make = $Make."', '"; } $Make = $Make."Peugeot"; } //Iveco ONLY if (isset($_POST['Submit']) && isset($_POST['Iveco'])) { // add comma if necessary to separate if (!empty($Make)) { $Make = $Make."', '"; } $Make = $Make."Iveco"; } //FIAT ONLY if (isset($_POST['Submit']) && isset($_POST['FIAT'])) { // add comma if necessary to separate if (!empty($Make)) { $Make = $Make."', '"; } $Make = $Make."FIAT"; } //Nissan ONLY if (isset($_POST['Submit']) && isset($_POST['Nissan'])) { // add comma if necessary to separate if (!empty($Make)) { $Make = $Make."', '"; } $Make = $Make."Nissan"; } //Renault ONLY if (isset($_POST['Submit']) && isset($_POST['Renault'])) { // add comma if necessary to separate if (!empty($Make)) { $Make = $Make."', '"; } $Make = $Make."Renault"; } //Volkswagen ONLY if (isset($_POST['Submit']) && isset($_POST['Volkswagen'])) { // add comma if necessary to separate if (!empty($Make)) { $Make = $Make."', '"; } $Make = $Make."Volkswagen"; } //Other makes ONLY if (isset($_POST['Submit']) && isset($_POST['DAF, Isuzu, MAN, Moffet, Scania, Toyota, LDV, Land Rover, Daihatsu, Mitsubishi'])) { // add comma if necessary to separate if (!empty($Make)) { $Make = $Make."', '"; } $Make = $Make."DAF', 'Isuzu', 'MAN', 'Moffet', 'Scania', 'Toyota', 'LDV', 'Land Rover', 'Daihatsu', 'Mitsubishi"; } And here is the actual query being submit (I only selected the Mercedes tickbox). SELECT * FROM vehicles WHERE Make IN ('Ford', 'Mercedes', 'Vauxhaull', 'Citroen', 'Peugeot', 'Iveco', 'FIAT', 'Nissan', 'Renault', 'Volkswagen', 'DAF', 'Isuzu', 'MAN', 'Moffet', 'Scania', 'Toyota', 'LDV', 'Land Rover', 'Daihatsu', 'Mitsubishi', 'Mercedes') Which seems its searching for all the makes, then adding Mercedes on to the end again. However when i tick the "Ford" box then the query works and it only returns Fords, even though the querys are the same! Does anyone know whats wrong with my query? Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/179062-stumped/ Share on other sites More sharing options...
JonnoTheDev Posted October 26, 2009 Share Posted October 26, 2009 What is this? $_POST['DAF, Isuzu, MAN, Moffet, Scania, Toyota, LDV, Land Rover, Daihatsu, Mitsubishi'] Your code structure is poor as your form element naming & value pairs must be. Use proper arrays i.e. <?php // the form is submitted if(strlen($_POST['submit']) && $_POST['submit'] == 'submit') { if(is_array($_POST['make'])) { // options are selected $makes = array(); foreach($_POST['make'] as $make) { $makes[] = "'".$make."'"; } $result = mysql_query("SELECT * FROM models WHERE manufacturer IN (".implode(",",$makes).")"); } } ?> <form> Ford: <input type="checkbox" name="make[]" value="ford" /><br /> Vauxhall:<input type="checkbox" name="make[]" value="vauxhall" /><br /> Toyota: <input type="checkbox" name="make[]" value="toyota" /><br /> <input type="submit" name="submit" value="submit" /> </form> Quote Link to comment https://forums.phpfreaks.com/topic/179062-stumped/#findComment-944743 Share on other sites More sharing options...
gigabyt3r Posted October 26, 2009 Author Share Posted October 26, 2009 $_POST['DAF, Isuzu, MAN, Moffet, Scania, Toyota, LDV, Land Rover, Daihatsu, Mitsubishi'] Thats the name of a button Quote Link to comment https://forums.phpfreaks.com/topic/179062-stumped/#findComment-944745 Share on other sites More sharing options...
JonnoTheDev Posted October 26, 2009 Share Posted October 26, 2009 $_POST['DAF, Isuzu, MAN, Moffet, Scania, Toyota, LDV, Land Rover, Daihatsu, Mitsubishi'] Thats the name of a button That is crazy. Can you understand my implementation. Very simple. Quote Link to comment https://forums.phpfreaks.com/topic/179062-stumped/#findComment-944747 Share on other sites More sharing options...
gigabyt3r Posted October 26, 2009 Author Share Posted October 26, 2009 I don't understand how to implement what you said Im confused Quote Link to comment https://forums.phpfreaks.com/topic/179062-stumped/#findComment-944761 Share on other sites More sharing options...
JonnoTheDev Posted October 26, 2009 Share Posted October 26, 2009 If you take a look at the size of your conditional statement it is really poor. By storing data in an array you can easily check if values exist. By using an array in your form for storing checkbox entries (where users can select multiple values) it makes your code much more readable and easier to work with. Just think if you added another make of car i.e Fiat. You would need to alter your conditional statements in your code as well as add to your form. Wheras using an array only requires you to add the new value to your form (i.e a new checkbox field) if (!isset($_POST['Ford']) || (!$_POST['Mercedes']) || (!$_POST['Vauxhaull']) || (!$_POST['Citroen']) || (!$_POST['Peugeot']) || (!$_POST['Iveco']) || (!$_POST['FIAT']) || (!$_POST['Nissan']) || (!$_POST['Renault']) || (!$_POST['Volkswagen']) || (!$_POST['DAF, Isuzu, MAN, Moffet, Scania, Toyota, LDV, Land Rover, Daihatsu, Mitsubishi'])) { $Make = "Ford', 'Mercedes', 'Vauxhaull', 'Citroen', 'Peugeot', 'Iveco', 'FIAT', 'Nissan', 'Renault', 'Volkswagen', 'DAF', 'Isuzu', 'MAN', 'Moffet', 'Scania', 'Toyota', 'LDV', 'Land Rover', 'Daihatsu', 'Mitsubishi"; } Learn how you use arrays, looping through values, converting to strings, etc Quote Link to comment https://forums.phpfreaks.com/topic/179062-stumped/#findComment-944765 Share on other sites More sharing options...
JonnoTheDev Posted October 26, 2009 Share Posted October 26, 2009 You are tring to work to much using strings. This is not the best choice of data type. Quote Link to comment https://forums.phpfreaks.com/topic/179062-stumped/#findComment-944770 Share on other sites More sharing options...
gigabyt3r Posted October 27, 2009 Author Share Posted October 27, 2009 I inserted the bit of code below in replace of mine and it works great, apart from when you first load the page no vehicles are shown and I would like all vehicles to be displayed, not sure which part I need to change // the form is submitted if(strlen($_POST['submit']) && $_POST['submit'] == 'submit') { if(is_array($_POST['make'])) { // options are selected $makes = array(); foreach($_POST['make'] as $make) { $makes[] = "'".$make."'"; } $result = mysql_query("SELECT * FROM models WHERE manufacturer IN (".implode(",",$makes).")"); } } Quote Link to comment https://forums.phpfreaks.com/topic/179062-stumped/#findComment-945318 Share on other sites More sharing options...
gigabyt3r Posted October 27, 2009 Author Share Posted October 27, 2009 Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/179062-stumped/#findComment-945356 Share on other sites More sharing options...
JonnoTheDev Posted October 27, 2009 Share Posted October 27, 2009 This is where you need to finish off your script yourself. The code snippet is an example of how to run a query based on selections made from checkboxes. To display your vehicles write a query that gets them from your database. Loop over the records and display to screen. I'm guessing that these will have the checkboxes next to each so include the checkbox element in the loop. Quote Link to comment https://forums.phpfreaks.com/topic/179062-stumped/#findComment-945400 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.