0

I am trying to populate the drop-down list from mysql data and got the solution from Populating a Drop-Down list using DBI and How do I get selected value from drop down box in Perl CGI

I have following code:

#!/usr/bin/perl
use warnings;
use strict;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
use DBI;
use Data::Dumper;
print "Content-type: text/html\n\n";
my $dbh = DBI->connect();
my @tables=$dbh->selectcol_arrayref('select TABLE_NAME from 1009_table_list order by TABLE_NAME');
print Dumper@tables; #this dumper is giving results
print qq[
<!DOCTYPE html>
<html>
<head></head>
<body>
<form id="upload-form"><table>
<tr><td>Table Name:</td><td><select name="tbname">
];
print Dumper@tables; # this dumper is not printing anything
foreach my $table(@tables)
{
print qq "<option value=\"$table\">" . $table . "</option>";
}
print qq[
</select>
</td></tr>
</table></form></body>
</html>
];

At second comment in code I am not able to get the value of @tables for the drop-list. Why?

Community
  • 1
  • 1
norbdum
  • 2,361
  • 4
  • 19
  • 23
  • 1
    Could you show us the result of Dumper (or a part of it if it's too long)? – Toto Sep 17 '13 at 07:12
  • `ctrl+u` in browser to see second dumper? Besides, `print qq{};` – mpapec Sep 17 '13 at 07:16
  • @M42 the dumper is `$VAR1 = [ 'ABSTRACT-ACCOUNT-CURRENT', 'CHALLAN-INVOICE', 'CLEARANCE-DETAIL-FOR-INTER-UNIT-TRANSFERS', 'CLEARANCE-DETAILS', 'DETAILS-OF-CENVAT-CREDIT-TAKEN-UTILIZED', 'HEADER-DATA', 'OTHER-PAYMENTS', 'PAID', 'RECEIPT-DETAILS-OF-INTERMEDIATE-GOODS-RECEIVEDS', 'SELF-ASSESSMENT-MEMORANDUM' ]; ` – norbdum Sep 17 '13 at 07:28
  • @mpapec yes its showing the dumper but how to get it into drop down...its not working. The printqq corrected. – norbdum Sep 17 '13 at 07:30

2 Answers2

2

selectcol_arrayref returns array ref, so:

my $tables = $dbh->selectcol_arrayref('select TABLE_NAME from 1009_table_list order by TABLE_NAME');

foreach my $table (@$tables) {
  print qq{<option value="$table">$table</option>};
}
mpapec
  • 50,217
  • 8
  • 67
  • 127
2

my @tables=$dbh->selectcol_arrayref('select TABLE_NAME from 1009_table_list order by TABLE_NAME');

returns an array_ref, in order to use it, you have to dereference it:

my $tables=$dbh->selectcol_arrayref('select TABLE_NAME from 1009_table_list order by TABLE_NAME');
foreach my $table (@$tables) {
    print qq~<option value="$table">$table</option>~;
}
Toto
  • 89,455
  • 62
  • 89
  • 125
  • you could dereference `my @tables=@{$dbh->selectcol_arrayref}` so everything else stays in place. – mpapec Sep 17 '13 at 07:39