Jump to content

[SOLVED] If I have an empty value in a form dropdown


agge

Recommended Posts

Think that we have one dropdown form where we can choose countries from like this.

 

<select name="country" id="dropdown">
<option value="Choose country">Choose</option>
<option value="USA">USA</option>
<option value="CANADA">CANADA</option>

 

IF I have an SELECT I can easily set country = '$country' IF user have choosen some country, but when user not choose any country the selected one is "choose", is it possible do add All countries to $country so the SELECT formula include all countrys and not just get empty?  ??? ???

yeah, you would have to loop through each country and add it to the WHERE clause i believe. if thats what your asking.

 

you could just have an array with the countries in it then loop through them and add them to a variable.

 

$countries = array("USA", "CANADA", "AUSTRALIA");
$countries_where = "";

$i = 0;
foreach($countries as $country){
if($i == count($countries)-1){
	$countries_where .= "OR `country`='{$country}'";	
	}else{
	$countries_where .= "OR `country`='{$country}' ";
}
}

$query = mysql_query("SELECT * FROM `table` WHERE `fld`='value' {$countries_where}");

 

not sure if thats what you mean.

yeah, you would have to loop through each country and add it to the WHERE clause i believe. if thats what your asking.

 

you could just have an array with the countries in it then loop through them and add them to a variable.

 

$countries = array("USA", "CANADA", "AUSTRALIA");
$countries_where = "";

$i = 0;
foreach($countries as $country){
if($i == count($countries)-1){
	$countries_where .= "OR `country`='{$country}'";	
	}else{
	$countries_where .= "OR `country`='{$country}' ";
}
}

$query = mysql_query("SELECT * FROM `table` WHERE `fld`='value' {$countries_where}");

 

not sure if thats what you mean.

 

 

I have an form where user can search for som user in my db if he choose name A and country='USA'

 

I have an select like this: $country='USA'

SELECT * FROM DB WHERE name LIKE '%a%' AND country='$country'

there I get all whos name is spelling with A in and and belong to USA

But if a user choose name A and don't choose country in my form I want to find all name wich is spelling with an A not depending on country.

yeah, you would have to loop through each country and add it to the WHERE clause i believe. if thats what your asking.

 

you could just have an array with the countries in it then loop through them and add them to a variable.

 

$countries = array("USA", "CANADA", "AUSTRALIA");
$countries_where = "";

$i = 0;
foreach($countries as $country){
if($i == count($countries)-1){
	$countries_where .= "OR `country`='{$country}'";	
	}else{
	$countries_where .= "OR `country`='{$country}' ";
}
}

$query = mysql_query("SELECT * FROM `table` WHERE `fld`='value' {$countries_where}");

 

not sure if thats what you mean.

 

It solves my problem if I type all countries in an array like this array('USA', 'Canada') etc. But I have my countries in a table named country, is it possible to get out an array like array(SELECT country FROM country) something like that, I searched for it but didn't find any usefull yet.

This is how I get an array from my table.

 

DB conn.....
$query = "SELECT DISTINCT country FROM `T_country`";
$countries = array();
while ($row = mysql_fetch_array ($result))
{
array_push($countries, "{$row['country']}");
}

 

Thank you ProjectFear, without u help I 'didn't get this far.

Archived

This topic is now archived and is closed to further replies.

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