SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2000
    Location
    Norcross, GA
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have a good handle on making a dynamic drop-down box, but I'm not sure how to go about taking it one step further...

    For example. A variable is passed to a form -- $fooid

    In the form there is a php enabled <select> routine:

    while ($myrowver = mysql_fetch_array($verresult)) {
    $fooid = $myrowver[fooid];
    $fooname = $myrowver[fooname];
    echo "<option value=\"$fooid\">$fooname</option>";

    How would I make sure that the value of $fooid has a 'selected' attribute?
    - Mike
    http://www.georgiaoffroad.com

  2. #2
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, a cude, messy way would be something like this:

    PHP Code:
    while ($myrowver mysql_fetch_array($verresult)) { 
      
    $fooid $myrowver[fooid]; 
      
    $fooname $myrowver[fooname]; 
      if (
    $fooid == $myrowver[fooid]) {
        echo 
    "<option selected value=\"$fooid\">$fooname</option>"
      } else {
        echo 
    "<option value=\"$fooid\">$fooname</option>"
      } 
    There's likely a better way to do it, but that should work just fine.

  3. #3
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I was just thinking of a better way of using mysql_fetch_array() consider this approach
    PHP Code:
    while ($myrowver mysql_fetch_array($verresult)) { 
      
    extract($myrowver);
      if (
    $fooid == $myrowver[fooid]) {
        echo 
    "<option selected value="$fooid">$fooname</option>"
      } else {
        echo 
    "<option value="$fooid">$fooname</option>"
      } 
    Notice the extract() function, this should take care of the assigning of field values to vars like $fooid = $myrowver["fooid"]

    no $fooid is already set for you.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  4. #4
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good point, although actually, I now realize that neither of ours will work...he'll need something like this:

    PHP Code:
    while ($myrowver mysql_fetch_array($verresult)) { 
    if (
    $fooid == $myrowver[fooid]) {
        echo 
    "<option selected value=\"$fooid\">$fooname</option>"
      } else {
        echo 
    "<option value=\"$myrowver[fooid]\">$fooname</option>"
      } 
    As he mentioned before, he wants to pass the $fooid variable to that page - therefore, if we give it a new value (with $fooid = $row, etc, OR with the extract() function), the ($fooid == $myrowver[fooid]) condition will ALWAYS evaluate to TRUE.

    I realized this in my first post, but forget to take out the two offending lines (lines 2 and 3).

  5. #5
    SitePoint Enthusiast
    Join Date
    Jan 2001
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I use this javascript function alot when I need to do this type of thing. might be an option for you.

    function load_select(f_name,select_name,val)
    {

    var the_one = eval('document.'+f_name+'.'+select_name);

    for(x=0;x < the_one.length;x++)
    {
    if (the_one.options[x].value == val)
    {
    the_one.options.selectedIndex = x;
    break;
    }

    }
    the_one = "";
    }

    simply call this by either

    <body onload="load_select('form_name','select_box_name','<?echo $value_to_be_selected ?>'>

    or inline at bottom of page like

    <script>load_select('form_name','select_box_name','<?echo $value_to_be_selected ?>');</script>

    hope that helps..

    billy


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
  •