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

OK, guys here is whats up:

BACKGROUND
I am setting up a drop-down menu (scrolling list) and filling it like so:
###setup my dropdown my @employee; while (@row = $sth->fetchrow_array) { # Used with arrays # handle null and undef's $last_name = (defined $row[2]) ? $row[2] : ''; push @employee, $last_name; }

And using it like so:
foreach (@employee) { print "<option value=$_>$_\n"; }

All of this is working just fine.

PROBLEM
Here is the problem. As you can see i am pulling in JUST the last name, which is not unique. I have several people with the same last name.

What i need is somtheing like this

<option value=SS_NUMBER>FIRST_NAME LAST_NAME

This requres that i pull out two more fields. Which i think can be done as above also:
Somthing like so:
my @employee; while (@row = $sth->fetchrow_array) { # Used with arrays # handle null and undef's $SS_NUMERB = (defined $row[0]) ? $row[0] : ''; $first_name = (defined $row[1]) ? $row[1] : ''; $last_name = (defined $row[2]) ? $row[2] : ''; push @SS_NUMBER, $SS_NUMBER; push @employee_firstname, $first_name; push @employee_lastname, $last_name; }

How can i set this up to display like this, and fill my scrolling list:
<option value=$SS_NUMBER>$employee_FIRSTNAME $employee_LAST_NAME

One note however, i will still need a SS_number variable alone for further processing other than this.

thanks,

;-}

Replies are listed 'Best First'.
Re: using DBI oracle to make scrolling lists(drop down menus)
by thunders (Priest) on Jan 22, 2002 at 06:23 UTC
    the CGI module provides a method called scrolling_list that makes this sort of thing simple. You pass the function an array of values and a hash of labels for those values. I usually do this by reference to keep it clean. I would do something like this
    #!/usr/bin/perl -w use DBI; use CGI qw(:standard); use strict; #You connect to Oracle in a similar way I'm sure... my $driver = "mysql"; my $database = "blah"; my $hostname = "blah"; my $dsn = "DBI:$driver:database=$database;host=$hostname"; my $dbh = DBI->connect($dsn,undef,undef); print header,start_html,"\n"; my $people_query = $dbh->prepare("SELECT SS_NUMBER, FIRST_NAME +, LAST_NAME FROM people"); $people_query->execute; my %people_hash; #creat a hash of "fname lname" keyed by SSN while (my @people_array = $people_query->fetchrow_array) { $people_hash{"$people_array[0]"} = "$people_array[1] $ +people_array[2]"; } #create a list of the SSNs maybe sort it? my @people_keys = sort{$a <=> $b} keys %people_hash; print table( Tr( td( ['Some people:', scrolling_list( -name =>'people', -values =>\@people_keys, -labels =>\%people_hash, -size=>1) ]), ), ); print end_html;
      Personally I prefer to do the sorting in Oracle:
      $sql = "SELECT ss_number, last_name || ' ' || first_name NAME FROM people ORDER BY 2";
      and using
      $people_hash{$people_array[0]} = $people_array[1]; push @people_keys,$people_array[0];
      to populate the local storage structures.
      Just a matter of style, really. I haven't done any benchmarking, but Oracle Corp spends a lot of money writing code to make sorting effecient :)

      rdfield

Re: using DBI oracle to make scrolling lists(drop down menus)
by jonjacobmoon (Pilgrim) on Jan 22, 2002 at 04:51 UTC
    Look into fetchrow_hashref and Template Toolkit. Those two alone should help you control your code better.

    Sorry, I don't have the time to go into detail but just wanted to send you in the right direction : ) hope it helps.


    I admit it, I am Paco.
Re: using DBI oracle to make scrolling lists(drop down menus)
by wardk (Deacon) on Jan 22, 2002 at 05:23 UTC
    I agree that looking into hashref is a great idea, especially if you have to create the select's option list later (i.e. you don't want to just create the option list while in the fetch loop).

    my input to this would be to possibly find some other key than SSN, if you create this dropdown with SSN, a simple View Source will allow all users to capture all the others SSN. this can never be a good thing (unless perhaps you work for SSA and this is an app for internal use only :-)

      the Social Security number is just an example for this post. I am actually using a unique refernce number. But definately a good point to remember.
Re: using DBI oracle to make scrolling lists(drop down menus)
by data67 (Monk) on Jan 22, 2002 at 05:23 UTC
    Is there a good way out there to do this same thing with different tables. I mean if you have a form with several list box's like this what would be a good way to fetch data from more than one table in the same database database.

    thanks

      yes, if your tables are related you would do it in the same statement.
      #this part might be different depending on how your db does SQL my $people_query = $dbh->prepare("SELECT ss_number, f_name, l_name, st +ate FROM people, states WHERE people.stID = states.stID");

      If your tables are not related you would use multiple statement handles like the one above. As long as they have different names you can manipulate them indepedently.
Re: using DBI oracle to make scrolling lists(drop down menus)
by nandeya (Monk) on Jan 23, 2002 at 00:47 UTC
    Here is a quick partial example (not printing out all of the html page stuff) along with sorting within the SQL as mentioned within. Think could have made more efficient if I made use of a complex data structure off of a array as opposed to the way I went about in the hash below, but nonetheless, here is excerpt of...

    my $sth = $dbh->prepare("SELECT SSN, FIRST_NAME, LAST_NAME, FROM ZZ_INFO ORDER BY LAST_NAME"); unless ($sth->execute()) { print "$DBI::errstr \n"; }; my %hrcd; my $i = 0; while (my @row = $sth->fetchrow_array) { my $ssn = (defined $row[0]) ? $row[0] : ''; my $fn = (defined $row[1]) ? $row[1] : ''; my $ln = (defined $row[2]) ? $row[2] : ''; $hrcd{$i}= [$ssn, $ln, $fn]; $i++; } print "<table><tr><th>Name</th></tr><tr><td><select name=\"SSN\" size= +\"1\">\n"; for my $j (sort keys %hrcd) { print "<option value=\"$hrcd{$j}->[0]\">$hrcd{$j}->[1], $hrcd{$j}->[ +2]</option> \n"; } print "</select></td></tr></table>\n";

    nandeya