agge Posted March 20, 2007 Share Posted March 20, 2007 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? ??? ??? Link to comment https://forums.phpfreaks.com/topic/43456-solved-if-i-have-an-empty-value-in-a-form-dropdown/ Share on other sites More sharing options...
JasonLewis Posted March 20, 2007 Share Posted March 20, 2007 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. Link to comment https://forums.phpfreaks.com/topic/43456-solved-if-i-have-an-empty-value-in-a-form-dropdown/#findComment-211046 Share on other sites More sharing options...
agge Posted March 20, 2007 Author Share Posted March 20, 2007 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. Link to comment https://forums.phpfreaks.com/topic/43456-solved-if-i-have-an-empty-value-in-a-form-dropdown/#findComment-211081 Share on other sites More sharing options...
agge Posted March 20, 2007 Author Share Posted March 20, 2007 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. Link to comment https://forums.phpfreaks.com/topic/43456-solved-if-i-have-an-empty-value-in-a-form-dropdown/#findComment-211147 Share on other sites More sharing options...
agge Posted March 21, 2007 Author Share Posted March 21, 2007 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. Link to comment https://forums.phpfreaks.com/topic/43456-solved-if-i-have-an-empty-value-in-a-form-dropdown/#findComment-211905 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.