Results 1 to 6 of 6

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

  1. #1
    oceanwap's Avatar
    oceanwap is offline x10Hosting Member
    Join Date
    Nov 2009
    Location
    India
    Posts
    24

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

    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 Code:
    <html><head><title>select country</title></head><body>
    <?php
    mysqli_select_db
    ($connection,"$db");
    $sql "SELECT country_name FROM ip2c GROUP BY country_name";
    $resultmysqli_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 Code:
    <option>Afganistaan</option>
    <option selected="selected">Canada</option>
    <option>Egypt</option>
    I want to know by which method I can achieve this.

  2. #2
    oceanwap's Avatar
    oceanwap is offline x10Hosting Member
    Join Date
    Nov 2009
    Location
    India
    Posts
    24

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

    Sorry for this silly question I asked above, I found the solution immidaetly after posting this.Here it is:-
    PHP Code:
    <?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 Code:
    <?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 by oceanwap; 04-13-2010 at 01:46 AM.

  3. #3
    misson is offline x10 Spammer
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,573

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

    Quote Originally Posted by oceanwap View Post
    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.

    Quote Originally Posted by oceanwap View Post
    Here it is:-
    PHP Code:
    <?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 Code:
    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>";

    Quote Originally Posted by oceanwap View Post
    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 Code:
    <?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 Code:
    $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.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Jon Skeet's and Eric Raymond's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  4. #4
    oceanwap's Avatar
    oceanwap is offline x10Hosting Member
    Join Date
    Nov 2009
    Location
    India
    Posts
    24

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

    Quote Originally Posted by misson View Post
    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 Code:
    <?php
    $test 
    "success";
    $primary "test";
    $ID = ${$primary};
    echo 
    $ID;
    ?>
    So that outputs
    HTML Code:
    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 by oceanwap; 04-13-2010 at 01:16 AM. Reason: spelling mistakes

  5. #5
    xav0989's Avatar
    xav0989 is offline Community Public Relation
    Join Date
    Jul 2008
    Location
    ifk
    Posts
    4,468

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

    Quote Originally Posted by oceanwap View Post
    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.
    Xavier L | Community Public Relations Manager (Free Hosting Support)
    █ Yes, my position is too cool to even exist!
    How am I helping? Rate this post by clicking the icon below! (this is even better than "liking" a post)
    Terms of Service | Acceptable Use Policy | x10Hosting Wiki

  6. #6
    hernandez9039 is offline x10Hosting Member
    Join Date
    Feb 2010
    Posts
    19

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

    Thanks for your info guys, and it is a great experience to visit at http://x10hosting.com. Thank you and good luck to you
    _________________________________________
    Web Design London
    Last edited by hernandez9039; 04-15-2010 at 05:25 AM.

Similar Threads

  1. Cant Select Database
    By mrkini in forum Free Hosting
    Replies: 6
    Last Post: 04-08-2010, 02:10 AM
  2. cannot select database
    By slyrzmb in forum Free Hosting
    Replies: 3
    Last Post: 10-28-2009, 07:45 AM
  3. Help Select Suitable CMS
    By shivk in forum Scripts, 3rd Party Apps, and Programming
    Replies: 12
    Last Post: 08-04-2009, 12:09 PM
  4. I can't select the database!
    By offres in forum Free Hosting
    Replies: 1
    Last Post: 04-02-2008, 07:50 PM
  5. I can't select the database!
    By offres in forum Free Hosting
    Replies: 1
    Last Post: 04-02-2008, 02:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
dedicated servers