SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Feb 2006
    Posts
    299
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help Me On How To Display Intial Value In Combo Box When Using Php And Mysql

    Hello people,
    I have a problem with a combo box. I have a combo box that is selecting values from Mysql database table, this combo is in PHP. These values are being used on a form for editing records. Now I want this combo box to display INITIAL values from database. For example if the record has Canada as Country value when the record is opened in edit mode it should display combo box for countries but the initial value should be Canada. Until a user selects a different country. How can I achieve this?? PHP code I am using is below.
    Your help will be highly appreciated.


    PHP Code:
    include'connections.php';

    $result mysql_query("SELECT DISTINCT fromb from route");


                        <
    select name="From" id="From">
                       
             while (
    $myrow mysql_fetch_array($result))
             {
               
            echo 
    "<option>$myrow[fromb]</option>";

             } 

  2. #2
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Code php:
    include'connections.php';
     
    $result = mysql_query("SELECT DISTINCT fromb from route");
     
     
                        <select name="From" id="From">
     
             while ($myrow = mysql_fetch_array($result))
             {
                $selected = $country==$myrow[fromb] ? 'selected="selected"' : '';
                echo "<option value=\"$myrow[fromb]\" $selected>$myrow[fromb]</option>";
     
             }

    $country is the Country field selected somewhere above this loop.
    Saul

  3. #3
    SitePoint Wizard Hammer65's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln Nebraska
    Posts
    1,161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is a very common question, which means that it is a very common thing to want to do in PHP. Given that, it would be best to use a solution that is more re-usable and less procedural, unless you like writing the same thing over and over and over.

    Below is the function that I use

    PHP Code:
        function selectMenu($name,$op,$input='',$attributes=null)
        {
            if(!empty(
    $name))
            {
                
                
    $attr $attributes;
                if(
    is_array($attributes))
                {
                    
    $attr '';
                    foreach(
    $attributes as $key=>$aValue)
                    {
                        
    $attr .= $key=\"$aValue\"";
                    }
                }
                
    $multivalue false;
                if(
    is_array($input) && isset($attributes['multiple']))
                {
                    
    $multivalue true;
                }
                
    $tag "<select name=\"$name\"$attr>\n[options]</select>\n";
                
    $options '';
                foreach(
    $op as $key=>$value)
                {
                    
    $selected '';
                    if(
    is_numeric($key))
                    {
                        
    $key $value;
                    }
                    if(
    $multivalue)
                    {
                        if(
    in_array($value,$input))
                        {
                            
    $selected ' selected="selected"';
                        }
                    }
                    else
                    {
                        if(
    $value == $input)
                        {
                            
    $selected ' selected="selected"';
                        }
                    }
                    
    $options[] = "<option value=\"$value\"$selected>$key</option>";
                }
                if(!empty(
    $options))
                {
                    
    $tag str_replace('[options]',"\t".implode("\n\t",$options)."\n",$tag);
                }
                else
                {
                    
    $tag str_replace('[options]',"\n",$tag);
                }
                return 
    $tag;
            }
            else
            {
                return 
    '';
            }
        } 
    To use you might do this.

    PHP Code:
    $result mysql_query("SELECT DISTINCT fromb from route");
    $op = array();
    if(
    $result)
    {
      while (
    $myrow mysql_fetch_array($result))
       {
             
    $op[$myrow['fromb']] = $myrow['fromb']      
       }
    }
    // pre-selected value could come from anywhere
    $preselectedcountry 'US';
    // Do the combo box
    $combo selectMenu('country',$op,$preselectedcountry,array('id'=>'country'));

    // later when you need it 
    echo($combo); 
    More code, but re-usable, and it allows you to separate logic from presentation a little more.
    Visit my blog
    PHP && Life
    for technology articles and musings.


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
  •