SitePoint Sponsor

User Tag List

Results 1 to 15 of 15

Hybrid View

  1. #1
    SitePoint Zealot m0h's Avatar
    Join Date
    Sep 2001
    Location
    Cairo, Egypt
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Set an item to selected in a drop down list based on the DB

    I have a very long drop down list that the user select from it his country then it is stored in the database (mysql), when the user edit his info I want his country to be selected, the country list is not stored in my DB, when the user select the country its value (a nummber) is added to his file.
    Can this be done without adding the country list to my database?

  2. #2
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi m0h, btw what happen with your gif script, did it work?
    Could you please include some code? What do you want to do, include the country into the user's profile (inside the DB)?

  3. #3
    SitePoint Zealot m0h's Avatar
    Join Date
    Sep 2001
    Location
    Cairo, Egypt
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello Paul Yes it worked after I converted the out to upper case! it toke me 4 days and all what i needed was strtoupper($Img); .

    The country is in the user's profile, I want when the user edit his profile the country he selected be4 be the selected one in the drop down list.

    The join part
    <option value='' selected>-=- Country -=-</option>
    <option value='1'>Andorra</option>
    <option value='2'>Australia</option>
    <option value='3'>Austria</option>
    ...

    The edit part
    <option value='Num' selected>His Country</option>
    <option value='1'>Andorra</option>
    <option value='2'>Australia</option>
    <option value='3'>Austria</option>

    the country list is not in the DB only the value is added

    I think i'll have to add the country list to the DB but i'm asking here first if there is any other way coz its not an eazy job!

  4. #4
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah use php to take the country list out of the html form and put it into an array, then it will be super easy to do, if you want send me a file with the dropdown list or a link to it, and I will write a script to get the country names into an array for you and show you how to use it.

    freddy@bereminded.com
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  5. #5
    SitePoint Zealot m0h's Avatar
    Join Date
    Sep 2001
    Location
    Cairo, Egypt
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, I have send u the file
    Thanx alot

  6. #6
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is your array.

    PHP Code:
    <?
    $countries 
    = array(
    '376' => 'Andorra'
    '61' => 'Australia'
    '43' => 'Austria'
    '994' => 'Azerbaijan'
    '973' => 'Bahrain'
    '32' => 'Belgium'
    '387' => 'Bosnia'
    '55' => 'Brasil'
    '55' => 'Brasil'
    '359' => 'Bulgaria'
    '855' => 'Cambodia'
    '1' => 'Canada'
    '56' => 'Chile'
    '86' => 'China'
    '385' => 'Croatia'
    '357' => 'Cyprus'
    '420' => 'Czech Repl.'
    '45' => 'Denmark'
    '20' => 'Egypt'
    '372' => 'Estonia'
    '298' => 'Faroe Islands'
    '358' => 'Finland'
    '33' => 'France'
    '49' => 'Germany'
    '233' => 'Ghana'
    '350' => 'Gibraltar'
    '30' => 'Greece'
    '852' => 'Hong Kong'
    '36' => 'Hungary'
    '354' => 'Iceland'
    '91' => 'India'
    '62' => 'Indonesia'
    '353' => 'Ireland'
    '972' => 'Israel'
    '39' => 'Italy'
    '229' => 'Ivory Coast'
    '81' => 'Japan'
    '962' => 'Jordan'
    '294' => 'Kenya'
    '965' => 'Kuwait'
    '371' => 'Latvia'
    '961' => 'Lebanon'
    '266' => 'Lesotho'
    '370' => 'Lithuania'
    '352' => 'Luxemburg'
    '853' => 'Macau'
    '389' => 'Macedonia'
    '261' => 'Madagascar'
    '60' => 'Malaysia'
    '356' => 'Malta'
    '230' => 'Mauritius'
    '212' => 'Morocco'
    '264' => 'Namibia'
    '31' => 'Netherlands'
    '64' => 'New Zealand'
    '47' => 'Norway'
    '968' => 'Oman'
    '968' => 'Oman'
    '51' => 'Peru'
    '63' => 'Philippines'
    '48' => 'Poland'
    '351' => 'Portugal'
    '974' => 'Qatar'
    '262' => 'Reunion'
    '40' => 'Romania'
    '7' => 'Russia'
    '966' => 'Saudi Arabia'
    '221' => 'Senegal'
    '65' => 'Singapore'
    '421' => 'Slovakia'
    '386' => 'Slovenia'
    '27' => 'South Africa'
    '34' => 'Spain'
    '94' => 'Sri Lanka'
    '46' => 'Sweden'
    '41' => 'Switzerland'
    '886' => 'Taiwan'
    '295' => 'Tanzania'
    '66' => 'Thailand'
    '216' => 'Tunesia'
    '90' => 'Turkey'
    '971' => 'U.A.E.'
    '380' => 'Ukraine'
    '44' => 'United Kingdom'
    '1' => 'U.S.A./Canada'
    '58' => 'Venezuela'
    '381' => 'Yugoslavia'
    '260' => 'Zambia'
    '263' => 'Zimbabwe'
    );
    ?>

    Here is how you would setup for dynamic selection


    PHP Code:

    //For testing I set $country = 90;
    //In your case I assume the number for the country 
    //will be living in the database so $country will get 
    //its value from the database.
    $country = 90;
    ?>
    <form>
    <select name="country">
            <option value=""<?=(!$country || $country == '') ? ' selected' ''?>>-=- Country -=-</option>

    <?
        
    foreach($countries as $key => $val) {
            
    ?>
            <option value="<?=$key?>"<?=($country == $key) ? ' selected' ''?>><?=$val?></option>
            <?
            
    }
    ?>
    </select>
    </form>


    And in case you are interested here is the code I used to read in that html and write to another file a php array.

    PHP Code:
    <?
    $data 
    file("Country.txt");
    $out fopen("Country2.txt""w");
    $str "<?\n\$countries = array(";
    foreach(
    $data as $key => $country) {
        
    ereg("<option value='([^']+)'(.+)?>([^<]+)</option>"$country$matches);
        if(
    $matches[3] != '') {
            
    $str .= "'$matches[1]' => '$matches[3]', \n";
            }
        else {
            continue;
            }
        }
    $str .= substr($str, -2);
    $str .= ");\n?>";
    fputs($out$str."\n");
    fclose($out);
    ?>
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  7. #7
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I guess Louie is going to write something similar,
    PHP Code:
    <?php
     $countrylist 
    = array('Andorra,','Australia',...);

     foreach (
    $countrylist as $a => $b) {
       
    $selected = ($userprofile['country'] == $a) ? ' selected' '';
       
    $echo "<option value='$a'$selected>$b</option>";
     }
    ?>
    Note I'm assuming the country code is the same as the index of the array, ie, Andorra = 0, Australia = 1, etc. Also $userprofile is the result from mysql_fetch_array() and country is your column in the table (DB).

  8. #8
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by freddydoesphp
    Here is your array.

    PHP Code:
    <?
    $countries 
    = array(
    '376' => 'Andorra'
    '61' => 'Australia'
    '43' => 'Austria'
    '994' => 'Azerbaijan'
    '973' => 'Bahrain'
    '32' => 'Belgium'
    '387' => 'Bosnia'
    '55' => 'Brasil'
    '55' => 'Brasil'
    '359' => 'Bulgaria'
    '855' => 'Cambodia'
    '1' => 'Canada'
    '56' => 'Chile'
    '86' => 'China'
    '385' => 'Croatia'
    '357' => 'Cyprus'
    '420' => 'Czech Repl.'
    '45' => 'Denmark'
    '20' => 'Egypt'
    '372' => 'Estonia'
    '298' => 'Faroe Islands'
    '358' => 'Finland'
    '33' => 'France'
    '49' => 'Germany'
    '233' => 'Ghana'
    '350' => 'Gibraltar'
    '30' => 'Greece'
    '852' => 'Hong Kong'
    '36' => 'Hungary'
    '354' => 'Iceland'
    '91' => 'India'
    '62' => 'Indonesia'
    '353' => 'Ireland'
    '972' => 'Israel'
    '39' => 'Italy'
    '229' => 'Ivory Coast'
    '81' => 'Japan'
    '962' => 'Jordan'
    '294' => 'Kenya'
    '965' => 'Kuwait'
    '371' => 'Latvia'
    '961' => 'Lebanon'
    '266' => 'Lesotho'
    '370' => 'Lithuania'
    '352' => 'Luxemburg'
    '853' => 'Macau'
    '389' => 'Macedonia'
    '261' => 'Madagascar'
    '60' => 'Malaysia'
    '356' => 'Malta'
    '230' => 'Mauritius'
    '212' => 'Morocco'
    '264' => 'Namibia'
    '31' => 'Netherlands'
    '64' => 'New Zealand'
    '47' => 'Norway'
    '968' => 'Oman'
    '968' => 'Oman'
    '51' => 'Peru'
    '63' => 'Philippines'
    '48' => 'Poland'
    '351' => 'Portugal'
    '974' => 'Qatar'
    '262' => 'Reunion'
    '40' => 'Romania'
    '7' => 'Russia'
    '966' => 'Saudi Arabia'
    '221' => 'Senegal'
    '65' => 'Singapore'
    '421' => 'Slovakia'
    '386' => 'Slovenia'
    '27' => 'South Africa'
    '34' => 'Spain'
    '94' => 'Sri Lanka'
    '46' => 'Sweden'
    '41' => 'Switzerland'
    '886' => 'Taiwan'
    '295' => 'Tanzania'
    '66' => 'Thailand'
    '216' => 'Tunesia'
    '90' => 'Turkey'
    '971' => 'U.A.E.'
    '380' => 'Ukraine'
    '44' => 'United Kingdom'
    '1' => 'U.S.A./Canada'
    '58' => 'Venezuela'
    '381' => 'Yugoslavia'
    '260' => 'Zambia'
    '263' => 'Zimbabwe'
    );
    ?>
    grrrr.... my country is not there.

  9. #9
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Where is that? Also I used the list from m0h so I can't be held repsonsible nor can my cat
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  10. #10
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by freddydoesphp
    Where is that? Also I used the list from m0h so I can't be held repsonsible nor can my cat
    Come on, it's under Location field (does the cat know how to read a map? )

  11. #11
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    He is a cat not cartographer. Anyways I forgot to pack my map this morning
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  12. #12
    SitePoint Zealot m0h's Avatar
    Join Date
    Sep 2001
    Location
    Cairo, Egypt
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanx freddy
    Paul I was going to add it but i'm not sure if this location is on earth or not

  13. #13
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  14. #14
    SitePoint Zealot m0h's Avatar
    Join Date
    Sep 2001
    Location
    Cairo, Egypt
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    your avatar makes me sure that u r not from earth

  15. #15
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    LOL


Bookmarks

Posting Permissions

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