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

Hello Monks,

I'm trying to print a table with results from a query. When I run the script, I get a blank page. Luckily, no error messages.

Can someone take a look at my code and help me out with why I'm receiving a blank page.

Thank you.
sub get_customer { my ($dbh, $track_ref, @track_ref) = @_; $dbh = WebDB::connect (); my $sth = $dbh->prepare ("SELECT * FROM Shipments WHERE customer_id = +?"); $sth->execute($customer_id); while (my @track_ref = $sth->fetchrow_array ()){ push (@track_ref, Tr ({-align => "CENTER", -valign=>"TOP", -BGCOLOR= +>"silver", -style=>"font-family: verdana; font-size: 10pt;"}, th ({-width=>"50"},("Customer ID")), th ({-width=>"50"},("Carrier")), th ({-width=>"90"},("Tracking Number")) + )); push (@track_ref, Tr ({-valign=>"center", -style=>"font-family: verdan +a; font-size: 10pt;"}, td ($track_ref[7]), td ($track_ref[3]), td ($track_ref[4]) )); } return (table ({-align => "CENTER", -border => 0}, @track_ref)); print start_form (-action => "http://wwwapps.ups.com/tracking/tracking +.cgi", -method => "POST"), table ( Tr ( td ("UPS Tracking Number:"), td (textfield (-name => "tracknum")) ), ), submit (-name => "choice", -value => "Track"), end_form (); }

Replies are listed 'Best First'.
Re: No results from script
by naChoZ (Curate) on Jun 04, 2003 at 13:15 UTC

    push (@track_ref, Tr ({-align => "CENTER", -valign=>"TOP", -BGCOLOR= +>"silver", -style=>"font-family: verdana; font-size: 10pt;"},

    You can't push this to an array like that... You're trying to push:

    Tr ({-align => "CENTER"
    -valign=>>"TOP"
    -BGCOLOR=>"silver"
    -style=>"font-family: verdana; font-size: 10pt;"

    as elements to an array.

    Ideally, push the value to the array and then do the CGI stuff when you print it.

    ~~
    naChoZ

      Hello.

      I made a modification to the code to eliminate the push. The modification that I did make does bring me results but I'm not completely there.

      Right now, my results appear as: if I have a customer with three tracking numbers, my table appears with a row of table headings and the first tracking number, then a second row with table headings and the second tracking number, and finally a third row of table headings and the third tracking number.

      Is there a way to re-write my code to show the table headings once with the three tracking numbers underneath the headings?

      Here's the modified code....
      sub get_customer { my ($dbh, $track_ref, @track_ref) = @_; $dbh = WebDB::connect (); my $sth = $dbh->prepare ("SELECT * FROM Shipments WHERE customer_id = +?"); $sth->execute($customer_id); while (my @track_ref = $sth->fetchrow_array ()){ print table ({-border => 0}, Tr ({-align => "CENTER", -valign=>"TOP", -BGCOLOR=>"silver", -st +yle=>"font-family: verdana; font-size: 10pt;"}, th ({-width=>"50"},("Customer ID")), th ({-width=>"50"},("Carrier")), th ({-width=>"90"},("Tracking Number")) + )); print table ({-border => 0}, Tr ({-valign=>"center", -style=>"font-family: verdana; font-size: +10pt;"}, td ($track_ref[7]), td ($track_ref[3]), td ($track_ref[4]) )); } print start_form (-action => "http://wwwapps.ups.com/tracking/tracking +.cgi", -method => "POST"), table ( Tr ( td ("UPS Tracking Number:"), td (textfield (-name => "tracknum")) ), ), submit (-name => "choice", -value => "Track"), end_form (); }

      Thanks for your help.
        Here, this is chopped up from something I did a long time ago.

        my $sql_statement = "SELECT * from foo where bar = \"$baz\" "; my $sth = $dbh->prepare($sql_statement); $sth->execute() or die "Can't execute SQL statement : $dbh->errstr"; $sth->bind_columns(undef, \$FOO,\$BAR,\$BAZ,\$ONE,\$FOR,\$EACH,\$COLUM +N); my $row; while ($row = $sth->fetchrow_arrayref) { my @fields = ( $row->[9],$row->[0],$row->[1],$row->[2],$row->[3], $row->[4],$row->[5],$row->[6],$row->[7],$row->[8] ); #i fooled with the order to make it print in the order i wanted &showrow(@fields); } $sth->finish; # this is where you tailor your output however you want sub showrow { print "<tr>\n"; # start your table row foreach $value (@_) { print $value; # this is where you do table data } print "</tr>\n"; # stop your table row }

        ~~
        naChoZ

Re: No results from script
by Thelonius (Priest) on Jun 04, 2003 at 13:17 UTC
    The print start_form ... comes after
    return (table ({-align => "CENTER", -border => 0}, @track_ref));
    and so will never be executed.
Re: No results from script
by edoc (Chaplain) on Jun 04, 2003 at 13:16 UTC

    Is this sub meant to print to the browser or return some content for something else to print?

    You appear to be filling @track_ref with the data from the database, then pushing a heap of html onto the end of the array. Then you 'return' some more html + the array.

    After that you have a print statement that will never be reached because of the return before it.

    cheers,

    J

      Hi.

      What I'm trying to do is print a table to the browser with the results from the table.

      I want to show three columns: customer number, carrier, tracking number.

      I'm using an array because I want to show all the tracking numbers for a specific customer number.

      If I have a customer with 5 tracking numbers, I would like to show a table with each tracking number as a seperate row.

      Can you help me figure out a way to achieve this?
      Thanks.

        Ok, well basically, I think you just wanted to move the print that prints the header out of the loop.. I did that and then got a bit carried away..

        Now, I have to add, I've never used CGI.pm to create HTML so whether those print statements are correct or not is for you to decifer. It looks to me as if you may actually be starting a table for the headers and then starting a new table for each of the rows but the rest should be ok. **This code is totally untested..

        You will need to change the names of the fields to the field names in you database table in the 2 parts of the script that refer to them.

        # we've split the data from presentation as much as we can # so we start by getting the customer data which is returned # by 'get_customer' as an array of hash references. my $customer = get_customer($customer_id); # print the table header print table ({-border => 0}, Tr ({-align => "CENTER", -valign=>"TOP", -BGCOLOR=>"silver" +, -style=>"font-family: verdana; font-size: 10pt;"}, th ({-width=>"50"},("Customer ID")), th ({-width=>"50"},("Carrier")), th ({-width=>"90"},("Tracking Number")) )); # print the rows of customer data # $customer is a reference to an array, so we dereference that # with '@' so we can loop through it's values, assigning them # to $row, one at a time. $row is now a hash reference so we # can derefence each key/value using the 'arrow' "$row->{key}". foreach my $row (@$customer){ print table ({-border => 0}, Tr ({-valign=>"center", -style=>"font-family: verdana; font +-size: 10pt;"}, # **** these 'keys' will need to be changed to the names of your datab +ase fields. **** td ($row->{customer_number}), td ($row->{carrier}), td ($row->{tracking_number}) )); } print start_form (-action => "http://wwwapps.ups.com/tracking/tracking +.cgi", -method => "POST"), table ( Tr ( td ("UPS Tracking Number:"), td (textfield (-name => "tracknum")) ), ), submit (-name => "choice", -value => "Track"), end_form (); sub get_customer{ my ($customer_id) = @_; my $dbh = WebDB::connect(); # **** change "customer_number, carrier, tracking_number" to the na +mes of your database fields. **** my $sth = $dbh->prepare ("SELECT customer_number, carrier, tracking_ +number FROM Shipments WHERE customer_id = ?"); $sth->execute($customer_id); # push each result row into @customer while (my $track_ref = $sth->fetchrow_hashref()){ push(@customer, $track_ref); } # return @customer as an array reference return(\@customer); }

        cheers,

        J