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

In reply to Sorting data returned from DBI ... by chinamox

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.