Ask a Question related to PERL Miscellaneous, Design and Development.

  1. #1

    Default DBI Output Help

    Hello.

    I am learning Perl and have decided to create a web page that has
    dropdowns populated by a postgreSQL DB. I've been able to do this and
    the page looks fine but the code is awful. A chunk of the code is
    posted below; can anyone suggest a way to clean this up and make it
    more efficient? Namely, I am trying to consolidate the actual
    population of the <SELECT> into one loop, and figure out a way to get
    rid of the ugly "$i < scalar(@array)" line.

    This all works, but I'm still learning Perl and haven't figured out
    how to make it work good!

    Thanks,
    Tom

    CODE BELOW:
    ===========


    #!/opt/perl/bin/perl -wT
    use DBI;
    use CGI;
    use CGI::Carp qw(fatalsToBrowser);

    my $q = new CGI;

    my $dbh = DBI->connect( "dbi:Pg:dbname=web" ) || die;
    my @a = ('groupdescription','groups');
    my @b = ('role','servers_role');
    my $sth1 = $dbh->prepare("Select $a[0] from $a[1] order by $a[0]");
    my $sth2 = $dbh->prepare("Select $b[0] from $b[1] order by $b[0]");

    $sth1->execute;
    $sth2->execute;

    print $q->header,
    $q->start_html,
    $q->startform,
    "<table><Tr><td>$a[0]</td>",
    "<td><Select name=\"$a[0]\">";

    while( @row1 = $sth1->fetchrow ) {
    for( $i = 0; $i < scalar(@row1); $i++ ) {
    print "<option value=\"$row1[$i]\">$row1[$i]";
    }
    }
    print "</Select></td></Tr>",
    "<Tr><td>$b[0]</td>",
    "<td><Select name=\"$b[0]\">";

    while( @row2 = $sth2->fetchrow ) {
    for( $i = 0; $i < scalar(@row2); $i++ ) {
    print "<option value=\"$row2[$i]\">$row2[$i]";
    }
    }
    print "</Select></td></Tr></table></body></html>";
    Tom Guest

  2. Similar Questions and Discussions

    1. how to output 19.50 instead 19.5 ???
      I'm making a shop for a shirtcompany. At some point i want to outout the price of the order, but if the order's for instance 19.50 $ flash outputs...
    2. PDF output how-to ...
      Is there any example on how to format my photo(and some text description such as title, description, location, and date/time) to a PDF file. I'd...
    3. Carriage returns/output not displayed in output.asp
      PLEASE DON'T MULTIPOST. PLEASE DON'T POST ATTACHMENTS. PLEASE DON'T DOUBLE-POST. Ray at work
    4. #25152 [Opn->Bgs]: output buffering functions don't catch "virtual" output
      ID: 25152 Updated by: iliaa@php.net Reported By: msarsale at buenosaires dot gov dot ar -Status: Open +Status:...
    5. output at 16:9
      I have an application which I want to utput to a plasma screen. the screen works better at 16:9. Reading the graphics card instructions thet say...
  3. #2

    Default Re: DBI Output Help

    In article <f4144fab.0309061156.77e83ec1@posting.google.com >,
    [email]ompers@hotmail.com[/email] (Tom) wrote:
    > Hello.
    >
    > I am learning Perl and have decided to create a web page that has
    > dropdowns populated by a postgreSQL DB. I've been able to do this and
    > the page looks fine but the code is awful. A chunk of the code is
    > posted below; can anyone suggest a way to clean this up and make it
    > more efficient? Namely, I am trying to consolidate the actual
    > population of the <SELECT> into one loop, and figure out a way to get
    > rid of the ugly "$i < scalar(@array)" line.
    >
    > This all works, but I'm still learning Perl and haven't figured out
    > how to make it work good!
    >
    > Thanks,
    > Tom
    >
    > CODE BELOW:
    > ===========
    >
    >
    > #!/opt/perl/bin/perl -wT
    use strict;
    > use DBI;
    > use CGI;
    > use CGI::Carp qw(fatalsToBrowser);
    >
    > my $q = new CGI;
    >
    ..
    ..
    ..
    > while( @row1 = $sth1->fetchrow ) {
    > for( $i = 0; $i < scalar(@row1); $i++ ) {
    > print "<option value=\"$row1[$i]\">$row1[$i]";
    > }
    > }
    while( my @row1 = $sth1->fetchrow_array ) {
    print qq|<option value="$row1[0]">$row1[0]|;
    }

    ..
    ..
    ..


    hth-

    --
    Michael Budash
    Michael Budash Guest

  4. #3

    Default Re: DBI Output Help

    [email]ompers@hotmail.com[/email] (Tom) wrote in message news:<f4144fab.0309061156.77e83ec1@posting.google. com>...
    > Hello.
    >
    > I am learning Perl and have decided to create a web page that has
    > dropdowns populated by a postgreSQL DB. I've been able to do this and
    > the page looks fine but the code is awful. A chunk of the code is
    > posted below; can anyone suggest a way to clean this up and make it
    > more efficient? Namely, I am trying to consolidate the actual
    > population of the <SELECT> into one loop, and figure out a way to get
    > rid of the ugly "$i < scalar(@array)" line.
    >
    > This all works, but I'm still learning Perl and haven't figured out
    > how to make it work good!
    >
    > Thanks,
    > Tom
    >
    > CODE BELOW:
    > ===========
    >
    >
    > #!/opt/perl/bin/perl -wT
    > use DBI;
    > use CGI;
    > use CGI::Carp qw(fatalsToBrowser);
    >
    > my $q = new CGI;
    >
    > my $dbh = DBI->connect( "dbi:Pg:dbname=web" ) || die;
    > my @a = ('groupdescription','groups');
    > my @b = ('role','servers_role');
    > my $sth1 = $dbh->prepare("Select $a[0] from $a[1] order by $a[0]");
    > my $sth2 = $dbh->prepare("Select $b[0] from $b[1] order by $b[0]");
    >
    > $sth1->execute;
    > $sth2->execute;
    >
    > print $q->header,
    > $q->start_html,
    > $q->startform,
    > "<table><Tr><td>$a[0]</td>",
    > "<td><Select name=\"$a[0]\">";
    >
    > while( @row1 = $sth1->fetchrow ) {
    > for( $i = 0; $i < scalar(@row1); $i++ ) {
    > print "<option value=\"$row1[$i]\">$row1[$i]";
    > }
    > }
    > print "</Select></td></Tr>",
    > "<Tr><td>$b[0]</td>",
    > "<td><Select name=\"$b[0]\">";
    >
    > while( @row2 = $sth2->fetchrow ) {
    > for( $i = 0; $i < scalar(@row2); $i++ ) {
    > print "<option value=\"$row2[$i]\">$row2[$i]";
    > }
    > }
    > print "</Select></td></Tr></table></body></html>";
    Try this...

    my ($selectrow1,$selectrow2) =
    ("<table><Tr><td>$a[0]</td><td><select name='$a[0]'>",
    "<tr><td>$b[0]</td><td><select name='$b[0]'>");

    while( (@row1 = $sth1->fetchrow ) or (@row2 = $sth2->fetchrow) )
    {
    for (@row1) { $selectrow1 .= "<option value='$_\'>$_</option>\n" }
    for (@row2) { $selectrow2 .= "<option value='$_\'>$_</option>\n" }
    }
    print "$selectrow1</Select></td></Tr>\n$selectrow2</Select></td></Tr></table></body></html>";


    Tom
    ztml.com
    Tom Guest

  5. #4

    Default Re: DBI Output Help

    [email]tom@ztml.com[/email] (Tom) wrote in message news:<59b4279a.0309070513.344d894e@posting.google. com>...
    > [email]ompers@hotmail.com[/email] (Tom) wrote in message news:<f4144fab.0309061156.77e83ec1@posting.google. com>...
    > > Hello.
    > >
    > > I am learning Perl and have decided to create a web page that has
    > > dropdowns populated by a postgreSQL DB. I've been able to do this and
    > > the page looks fine but the code is awful. A chunk of the code is
    > > posted below; can anyone suggest a way to clean this up and make it
    > > more efficient? Namely, I am trying to consolidate the actual
    > > population of the <SELECT> into one loop, and figure out a way to get
    > > rid of the ugly "$i < scalar(@array)" line.
    > >
    > > This all works, but I'm still learning Perl and haven't figured out
    > > how to make it work good!
    > >
    > > Thanks,
    > > Tom
    > >
    > > CODE BELOW:
    > > ===========
    > >
    > >
    > > #!/opt/perl/bin/perl -wT
    > > use DBI;
    > > use CGI;
    > > use CGI::Carp qw(fatalsToBrowser);
    > >
    > > my $q = new CGI;
    > >
    > > my $dbh = DBI->connect( "dbi:Pg:dbname=web" ) || die;
    > > my @a = ('groupdescription','groups');
    > > my @b = ('role','servers_role');
    > > my $sth1 = $dbh->prepare("Select $a[0] from $a[1] order by $a[0]");
    > > my $sth2 = $dbh->prepare("Select $b[0] from $b[1] order by $b[0]");
    > >
    > > $sth1->execute;
    > > $sth2->execute;
    > >
    > > print $q->header,
    > > $q->start_html,
    > > $q->startform,
    > > "<table><Tr><td>$a[0]</td>",
    > > "<td><Select name=\"$a[0]\">";
    > >
    > > while( @row1 = $sth1->fetchrow ) {
    > > for( $i = 0; $i < scalar(@row1); $i++ ) {
    > > print "<option value=\"$row1[$i]\">$row1[$i]";
    > > }
    > > }
    > > print "</Select></td></Tr>",
    > > "<Tr><td>$b[0]</td>",
    > > "<td><Select name=\"$b[0]\">";
    > >
    > > while( @row2 = $sth2->fetchrow ) {
    > > for( $i = 0; $i < scalar(@row2); $i++ ) {
    > > print "<option value=\"$row2[$i]\">$row2[$i]";
    > > }
    > > }
    > > print "</Select></td></Tr></table></body></html>";
    >
    > Try this...
    >
    > my ($selectrow1,$selectrow2) =
    > ("<table><Tr><td>$a[0]</td><td><select name='$a[0]'>",
    > "<tr><td>$b[0]</td><td><select name='$b[0]'>");
    >
    > while( (@row1 = $sth1->fetchrow ) or (@row2 = $sth2->fetchrow) )
    > {
    > for (@row1) { $selectrow1 .= "<option value='$_\'>$_</option>\n" }
    > for (@row2) { $selectrow2 .= "<option value='$_\'>$_</option>\n" }
    > }
    > print "$selectrow1</Select></td></Tr>\n$selectrow2</Select></td></Tr></table></body></html>";
    >
    >
    > Tom
    > ztml.com
    Thanks a lot for the help guys! I ended up with a solution very
    similar to your suggestions:

    while( my @row = $sth1->fetchrow ) {
    for(@row) { print "<option value='$_'>$_</option>" }
    }

    Works great. I also am using 'fetchrow_array' to retrieve single
    results; and yes, I put use strict; back into place ;-).

    Thanks again,
    Tom
    Tom Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139