How to Auto select User's country in registration form's select menu using PHP

oceanwap

New Member
Messages
24
Reaction score
0
Points
0
I am creating a user registration form.In this form there is a option to select the country.I am fetching the country name from ip2country database.That also means I can get the country of user by his IP address.
I want the user's country to be automatically selected according to his IP address so it will save his time to select the country manually.Also I am going to use this in mobile site.So typing the C for canada won't work for most of the mobile devices.This is my php code:-
PHP:
<html><head><title>select country</title></head><body>
<?php
mysqli_select_db($connection,"$db");
$sql = "SELECT country_name FROM ip2c GROUP BY country_name";
$result= mysqli_query($connection,$sql);
?>
<form>
<select>
<?php
while($row = mysqli_fetch_array($result))
{
    echo "<option>{$row['country_name']}</option>";
}
?>
</select>
</form>
</body>
</html>
<?php
mysqli_close($connection);
?>
I want this while loop to generate this for appropriate country according to its IP address.
HTML:
<option>Afganistaan</option>
<option selected="selected">Canada</option>
<option>Egypt</option>
I want to know by which method I can achieve this.
 

oceanwap

New Member
Messages
24
Reaction score
0
Points
0
Sorry for this silly question I asked above, I found the solution immidaetly after posting this.Here it is:-
PHP:
<?php
while($row_country = mysqli_fetch_array($result_country))
{
  //I get the $country_detected from IP2Country Database. 
  if($country_detected == $row_country['country_name'])
   {
    $select="selected=\"selected\"";
    echo "<option {$select}>{$row_country['country_name']}</option>";
   }
   else
  {
   echo "<option>{$row_country['country_name']}</option>";
  }
}
?>
Actually I thought it will be similar to creating a select list for months where you want to select the current month automatically.There I used php's date() function.And a too much if else statements.But here databases made my life easy.Here is the php code I wrote for auto selecting month.
PHP:
<?php
date_default_timezone_set('Asia/Kolkata');
$timestamp=date(r);
$month=substr($timestamp,8,3);
if ($month==Jan)
{
$select1 = 'selected="selected"';
}
if ($month==Feb)
{
$select2 = 'selected="selected"';
}if ($month==Mar)
{
$select3 = 'selected="selected"';
}
if ($month==Apr)
{
$select4 = 'selected="selected"';
}
if ($month==May)
{
$select5 = 'selected="selected"';
}
if ($month==Jun)
{
$select6 = 'selected="selected"';
}
if ($month==Jul)
{
$select7 = 'selected="selected"';
}
if ($month==Aug)
{
$select8 = 'selected="selected"';
}
if ($month==Sep)
{
$select9 = 'selected="selected"';
}
if ($month==Oct)
{
$select10 = 'selected="selected"';
}
if ($month==Nov)
{
$select11 = 'selected="selected"';
}
if ($month==Dec)
{
$select12 = 'selected="selected"';
}
?>
I don't think this is the smart way to do this, please suggest a better solution.
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Sorry for this silly question I asked above, I found the solution immidaetly after posting this.
No worries about asking silly questions. Kudos for finding the answer, and thanks for posting it so that others might learn. However, I'm almost certain this question has been asked before on these very forums, and undoubtably been asked elsewhere.

Here it is:-
PHP:
<?php
while($row_country = mysqli_fetch_array($result_country))
{
  //I get the $country_detected from IP2Country Database. 
  if($country_detected == $row['country_name'])
   {
    $select="selected=\"selected\"";
    echo "<option {$select}>{$row['country_name']}</option>";
   }
   else
  {
   echo "<option>{$row['country_name']}</option>";
  }
}
?>
That's almost as good as you can get it when dealing with DB query results. However, there's no need for two echo statements.
PHP:
while($row_country = mysqli_fetch_array($result_country))
{
  //I get the $country_detected from IP2Country Database. 
  if($country_detected == $row['country_name']) {
    $attr=' selected="selected"';
  } else {
    $attr = '';
  }
  echo "<option$attr>$row[country_name]</option>";
}

Actually I thought it will be similar to creating a select list for months where you want to select the current month automatically.There I used php's date() function.And a too much if else statements.But here databases made my life easy.Here is the php code I wrote for auto selecting month.
PHP:
<?php
date_default_timezone_set('Asia/Kolkata');
$timestamp=date(r);
$month=substr($timestamp,8,3);
if ($month==Jan) {
    $select1 = 'selected="selected"';
}
...
?>
Anytime you find yourself creating numerous variable names with the same prefix, you should probably be using an array. Anytime you find yourself writing a large sequence of if statements, you should probably be using an array or a switch statement (depending in part on how similar the branches are). If the if blocks are exclusive of each other (i.e. at most one test will be true), you should make the later statements elseif statements.

PHP:
$months = array_combine(array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 
                              'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'), 
                        array_fill(0, 12, ''));
$months[date('M')] = ' selected="selected"';

?><select><?php
  foreach ($months as $mo => $attr) {
    echo "<option$attr>$mo</option>";
  }
?></select><?php

Also, strings should always be quoted; you never know what constants may be defined later on or keywords added to the language. You quoted neither the date format string nor the short month names.
 

oceanwap

New Member
Messages
24
Reaction score
0
Points
0
No worries about asking silly questions. Kudos for finding the answer, and thanks for posting it so that others might learn.
Thanks I learnt what you wrote, I searched web using google beore asking this question.Web results confused me, I read the concept of dynamic variables.
example:-
PHP:
<?php
$test = "success";
$primary = "test";
$ID = ${$primary};
echo $ID;
?>
So that outputs
HTML:
success
I thought that if I can genarate the variable names and their value dynamically.
like
$select1="selected";
$select2="selected";

And then if I can echo them in while loop using the test condition ($country_name==$row['country_name']) it would be a great idea.
But it was totally unnecessary in the case of country selection.I wasted my 3 hours on this.And searched the web for this topic.But if I
would have asked this in forum earlier, my time would have been saved.
Again thanks for your suggestions.
 
Last edited:

xav0989

Community Public Relation
Community Support
Messages
4,467
Reaction score
95
Points
0
I thought that if I can genarate the variable names and their value dynamically.
like
$select1="selected";
$select2="selected";

And then if I can echo them in while loop using the test condition ($country_name==$row['country_name']) it would be a great idea.
But it was totally unnecessary in the case of country selection.I wasted my 3 hours on this.And searched the web for this topic.But if I
would have asked this in forum earlier, my time would have been saved.
Again thanks for your suggestions.

Time spent to search online for things related to programming is never waisted. You never know when you'll use it, in the future.
 
Top