Thanks to Gerald, Werner, Gerlinde and Josh for help with the SELECT 
form element.  Gerald's was the simplest, most pragmatic solution, 
but I ended up figuring out something that was a bit more like 
Gerlinde and Josh suggested, but saving on a real 'if-then-else' loop 
and doing it all in one line:

   while ($row = mysql_fetch_object ($list)) {
      printf("<OPTION VALUE=%s %s>%s</OPTION>\n", $row->id, $row->id ==
         $formValues["listid"] ? "SELECTED" : "", $row->shortname);
   }

So using the ternary operator I do the 'if-then' for each row - if 
the id for that row = the id stored in $formValues[listid], then the 
word 'SELECTED' gets printed on the line.  If not then nothing is 
printed in that place - so if the 'listid' were '2', we'd get 
something like:

    <OPTION VALUE=1 >red</OPTION>
    <OPTION VALUE=2 SELECTED>white</OPTION>
    <OPTION VALUE=3 >blue</OPTION>

I can see that this technique could be applied to check boxes and 
radio buttons as well.  I'm liking that ternary operator!

Nelson


>Check out the tutorial at the following link:
>http://www.thickbook.com/extra/php_loopoption.phtml
>
>  >
>>  For creating a new record, no problem - but if the form is displaying
>>  data from an existing record, I want the pulldown list to indicate the
>>  existing value for 'type'.  I've done a brute-force 'if-then' version of
>>  this for a two value radio button, but doing a list like that could get
>>  ugly.  I'm hoping that someone has a more elegant solution.
>>
>>  Here's the code I'm using - the pulldown displays nicely.
>>
>  >   $list = mysql_query("SELECT id, type FROM contact_type, $link);
>>
>>    printf("<TR><TD CLASS=\"FormItem\">Contact Type: </TD>");
>>
>>    printf("<TD CLASS=\"FormInput\"><SELECT NAME=toolnum>");
>  >   while ($row = mysql_fetch_object ($list)) {
>  >     printf("<OPTION VALUE=%s>%s</OPTION>", $row->id, $row->type);
>>    }
>  >   printf("</SELECT>%s</TD></TR>\n", ($formValues) ? $formValues["id"] :
>>  "");
>  >
>>

-- 

---------------------------------------------------------------------
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/           (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php

Reply via email to