How to echo selected country?

Discussion in 'PHP' started by Om ji Kesharwani, Sep 4, 2010.

  1. #1
    How to echo selected country from the table in a combobox. the combobox is having list of many country.

    eg.

    <select>
    <option>country1</option>
    <option>country2</option>
    <option>country3</option>
    </select>


    if in table, country2 is stored . Then how to echo country2 as default selected?
     
    Om ji Kesharwani, Sep 4, 2010 IP
  2. wwfc_barmy_army

    wwfc_barmy_army Well-Known Member

    Messages:
    122
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    110
    #2
    <select>
    <option>country1</option>
    <option selected="selected">country2</option>
    <option>country3</option>
    <option>country4</option>
    </select>

    Hope this helps.
     
    wwfc_barmy_army, Sep 4, 2010 IP
  3. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #3
    Heres a little example I whipped to demonstrate how to do this.

    <select name="country">
    <?php
    echo ($row['country'] == 'england') ? '<option value="england" selected="selected">England</option>' : '<option value="england">England</option>';
    ?>
    <option value="ireland">Ireland</option>
    </select>
    PHP:
    You can either do so using ternary (like the above) or using if statement/s.
     
    danx10, Sep 4, 2010 IP
  4. Om ji Kesharwani

    Om ji Kesharwani Peon

    Messages:
    211
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4


    I can do it by:

    <select>
    <option <? if($row[country]=="country1" echo "selected"; ?>>country1</option>
    <option <? if($row[country]=="country2" echo "selected"; ?>>country2</option>
    <option <? if($row[country]=="country3" echo "selected"; ?>>country3</option>
    </select>

    Is there any other shortcut. Coz if there is 500 country i have to echo it for 500 times. I think this is not good tecknique
     
    Om ji Kesharwani, Sep 6, 2010 IP
  5. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #5
    You can store all the country names in a database or an array and then simply loop through all of them. That way you only have to write the code once.
     
    stephan2307, Sep 6, 2010 IP
  6. Om ji Kesharwani

    Om ji Kesharwani Peon

    Messages:
    211
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Ya database can be good solution.
    Thank u
     
    Om ji Kesharwani, Sep 9, 2010 IP
  7. HungryMinds

    HungryMinds Active Member

    Messages:
    216
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    63
    #7
    Hi! Try This Method. Script Not Tested.

    <?php

    $countries = '
    <select>
    <option>country1</option>
    <option>country2</option>
    <option>country3</option>
    <option>country4</option>
    </select>
    ';

    $row = $row["country"];

    print str_replace(">".$row, " selected='selected'>".$row, $countries);

    ?>
     
    HungryMinds, Sep 10, 2010 IP