in reply to mysql values in select box

Like most CGI stuff, you just interpoliate the variables into a string that you print.

You might look at ovid's very nice CGI course There are probably nicer ways to do this but this is what you asked for...

use strict; use warnings; use diagnostics; # you got this array from your Database... my @foo=qw(red blue green purple grey pink black); print "<SELECT NAME=test> \n"; foreach (@foo){ print "<OPTION VALUE=\"$_\"> $_\n"; }; print "</SELECT>";


email: mandog

Replies are listed 'Best First'.
Re: Re: mysql values in select box
by Kanji (Parson) on Oct 08, 2001 at 05:34 UTC

    That could get ugly if anything in @foo contains characters that should be escaped such as > or " ... something CGI.pm's popup_menu will handle for you automatically.

    #!/usr/bin/perl -wT use strict; use CGI qw/:standard/; my %colours = ( '#008000' => 'Green', '#808000' => '> Olive', '#00FF00' => '> Lime', '#008080' => '> Teal', ); print header, popup_menu( -name => 'colour', -labels => \%colours, -values => [ sort keys %colours ], );

    Also, if you're using DBI.pm and don't plan on labeling your <SELECT> values, then you can just pass in the output from DBI.pm's selectall_arrayref method.

    print popup_menu( -name => 'colours' -values => $dbh->selectall_arrayref('SELECT name FROM colour'), );

        --k.