On Nov 8, 2:22 pm, [EMAIL PROTECTED] (Neil) wrote: > Hi, > > I've a perl script that I'm working on to get a list of options from a > database presented to a user in a form. I can do the db query fine > and store the variables in an array, but I can't workout how to get > the array printed as a list of items in a menu. > > The script is.... > > #!/usr/bin/perl -w > use strict; > use CGI; > use CGI::Apache; > use DBI; > > ### Initialise a new CGI object > my $q = new CGI; > > ## Define the database connectin > my $user = "xxxx"; > my $dbh = DBI -> > connect('DBI:mysql:localhost:database=gdraw;host=localhost;port=3306', > $user,'xxxxx') || exit; > > ## Grab the datasets that are available by querying the > ## 'studies' table > my $statement = 'SELECT study FROM studies'; > my $query = $dbh -> prepare($statement); > $query -> execute(); > my @studies = $query -> fetchrow_array; > > ## Print the web-page > print $q -> header, > $q -> start_html(-title => "gDRAW - Genotype Database, R & > Analysis Web-interface", -style => {-src => "/css/gdraw1.css"}), "\n", > $q -> h1("Existing Data"), "\n", > $q -> p("You can select cohorts, markers and phenotypes to load > from below"), "\n", > > ## Start the form > $q -> start_form(-action => "", -method => "post", - > enctype=>"multipart/form-data"), > > ## Have a pull-down box for selecting the studies that the > ## user can access > $q -> popup_menu(-name => 'study_select', -values => > [EMAIL PROTECTED]), > > ## End the form > $q -> endform, > $q -> end_html; > > exit; > > The section that I'm having trouble with is... > > ## Have a pull-down box for selecting the studies that the > ## user can access > $q -> popup_menu(-name => 'study_select', -values => > [EMAIL PROTECTED]), > > @studies is of length 2,
You are making that assumption with absolutely no reason to believe it is true. You are assuming you retrieved all of the values from the database. What makes you believe you did that correctly? You never checked the contents of @studies, never checked the size of @studies. > but only one of these gets listed in the menu That's because @studies only contains one element. > that is generated. This line: > my @studies = $query -> fetchrow_array; retrieves ONE row from the database, and stores it in @studies. If you want to retrieve all rows from the query, you have to do so in a loop, or via one of the fetchall_* methods: my @studies; while (my ($study) = $query->fetchrow_array) { push @studies, $study; } Paul Lalli -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/