chinamox has asked for the wisdom of the Perl Monks concerning the following question:
Hello monks,
I am working on a project for an online class involving using a DBI to access a mySOL database and print out the results. Actually I have done all of this but now I am trying to refine how the resulting data is displayed in an html table.
My current Program:
#!/opt/bin/perl -w # use strict; use DBI; use CGI; my $cgi = CGI->new(); my $paygroup_num = $cgi->param('paygroup_num'); if ($paygroup_num) { print $cgi->header(), $cgi->start_html("sample cgi page 1"); print "<table>"; display_paygroup_page($cgi, $paygroup_num); print "</table>"; $cgi->end_html(); } else { show_form($cgi); }; ######## # SUBS BELOW ######## sub display_paygroup_page { my ($cgi, $paygroup_num) = @_; my $match = pop(@_); print "<tr><td> City Name </td><td> Last Name </td><td> First Name </ +td></tr>\n"; ## Connect to database my $dbh = DBI->connect('dbi:mysql:adatabase', 'undef', 'undef') || die "couldn't connect to database"; ## Get the data my $sql ='SELECT city, last_name, first_name FROM employees ORDER BY c +ity'; my $sth = $dbh->prepare($sql); $sth->execute(); ## worked through data my($last_name, $first_name, $city, $paygroup_num1); $sth->bind_columns(\($city, $last_name, $first_name, $paygroup_num1)); while($sth->fetch()){ if($match =~ $paygroup_num1){ print"<tr><td> $city </td><td> $last_name </td><td> $first_name </ +td></tr>\n"; } } $sth->finish(); $dbh->disconnect(); }; sub show_form { my $cgi = shift; print $cgi->header(), $cgi->start_html("Please specify an employee number"), $cgi->start_form(), "Enter an employee pay level number: ", $cgi->textfield(-name => 'paygroup_num'), $cgi->submit(), $cgi->br(), "(for example: S-32 or H-22)", $cgi->end_form(), $cgi->end_html(); };
For example if the proceeding CGI is run, it renders the results as an HTML table. Similar to this one:
__DATA__ City Name Last Name First Name Boston Ma Rebecca Boston Kent Orin Boston Austin James Newark Smith Jessica Newark Clark Benjamin Seattle Jones Matt Seattle Jones George __END__
Now I want to sort the table so that it is ordered by city, last name, first name instead of just by city.
__DATA__ City NameLast NameFirst Name Boston Austin James Boston Kent Orin Boston Ma Rebecca Newark Clark Benjamin Newark Smith Jessica Seattle Jones George Seattle Jones Matt __END__
I can’t really figure out how best to do this sorting and so I am asking if anyone knows of a useful tool inside of DBI that could help me, or failing that, another clever way of sorting the displayed data first by city, then last name and finally first name.
As always, thank you for your time and efforts.
Best,
-mox
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Sorting data returned from DBI ...
by davido (Cardinal) on Dec 10, 2006 at 06:51 UTC | |
by chinamox (Scribe) on Dec 10, 2006 at 07:09 UTC | |
by davido (Cardinal) on Dec 10, 2006 at 16:48 UTC | |
|
Re: Sorting data returned from DBI ...
by McDarren (Abbot) on Dec 10, 2006 at 06:54 UTC | |
by chinamox (Scribe) on Dec 10, 2006 at 07:13 UTC | |
by Devanchya (Beadle) on Dec 10, 2006 at 14:31 UTC |