Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/179062-stumped/
Share on other sites

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>

Link to comment
https://forums.phpfreaks.com/topic/179062-stumped/#findComment-944743
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/179062-stumped/#findComment-944765
Share on other sites

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).")");
   }
}

Link to comment
https://forums.phpfreaks.com/topic/179062-stumped/#findComment-945318
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/179062-stumped/#findComment-945400
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.