drivle has asked for the wisdom of the Perl Monks concerning the following question:

Hello- don't know if it's appropriate to ask total newbie questions here - but nothing ventured nothing gained, eh? I've finally worked out how to get values out of a mysql database and print them, but I can't work out how to get them into a html select box. ie if I retrieve 10 values - I then want to be able to have them available to a user in dropdown form so that they can select the one they want to move on with the next part of the procedure. I'd be very grateful for any advice, thanks.

Replies are listed 'Best First'.
Re: mysql values in select box
by mandog (Curate) on Oct 08, 2001 at 02:05 UTC
    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

      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.


Re: mysql values in select box
by kwoff (Friar) on Oct 08, 2001 at 04:54 UTC

    Tip: carefully read `perldoc CGI` and `perldoc DBI` if you're going to do much CGI programming in Perl. (I realize that is begging for someone to point out in the following something that contradicts one of those documents. :)

    I tested this, so hopefully pasting didn't mess anything up. (Change the $table and $sql in the following.)

    #!/usr/local/bin/perl -w # Demonstrate a scrolling list with DBI use strict; use CGI; use DBI; use vars qw($q $table); $q = new CGI; $table = 'makes'; MAIN: { my ($makes, $html); $makes = get_makes(); if (ref $makes) { print makes_page($makes); } else { print error_page('Blah!'); } } sub get_makes { my ($dbh, $sql, $sth, @makes); $dbh = get_dbh(); $sql = qq{SELECT make FROM $table}; $sth = $dbh->prepare($sql); $sth->execute(); while (my $make = $sth->fetchrow_array()) { push @makes, $make; } $sth->finish(); $dbh->disconnect(); return \@makes; } sub get_dbh { my ($dsn, $dbh); $dsn = "DBI:mysql:database=cartalk"; # better: # $dsn = q{DBI:mysql:mysql_read_default_file=/home/you/.my.cnf +}; $dbh = DBI->connect($dsn) || die $DBI::errstr; return $dbh; } sub makes_page { my $makes = shift; my ($make, $html); $make = (defined $q->param('make') && $q->param('make') =~ /^\ +w+$/) ? $q->param('make') : ''; $html = $q->header() . $q->start_html('Your title') . $q->star +t_form(); $html .= $q->scrolling_list(-name => 'make', '-values' => $makes, -size => 6); $html .= $q->submit(-name => 'submit', -value => 'Submit!'); $html .= "<P>Submitted: $make</P>\n"; $html .= $q->end_form() . $q->end_html(); return $html; } sub error_page { my $msg = shift; my ($html); $html = $q->header(). $q->start_html('Error!'); $html .= $msg; $html .= $q->end_html(); return $html; }
Re: mysql values in select box
by blackjudas (Pilgrim) on Oct 09, 2001 at 01:02 UTC
    Just a small suggestion, if you are going to work with many select boxes in your script, I certainly suggest working with the primary keys that the database call returns. This will make it easier for you if you need to determine actions, or previous selections a user has made based on those numbers. Here's an example:
    { my $sth = $dbh->prepare("SELECT id, value FROM database"); my $result = $sth->fethall_arrayref(); print "<select name=\"selectboxname\"><option></option>"; for (@$result) { print "<option value=\"$_->[0]\">$_->[1]</option>"; } print "</select>"; }
    The above example runs through each result (for loop) and takes the current value of each entry (..row ie. id->1, value=>Selection) and prints the option tag. You can easily check for previously selected options by adding a conditional in the for loop above and test against a previously declared value and adding the "selected" string into your option tags.

    You will now have a variable in your session called 'selectboxname' with a number value equivalent to what the user chose from the select.

    BlackJudas