in reply to No results from script

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

Replies are listed 'Best First'.
Re: Re: No results from script
by b310 (Scribe) on Jun 04, 2003 at 13:55 UTC
    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

        Hi Edoc,

        I tried your suggestion. It appears to me that I need to have two subroutines: one for the get_customer and one for printing the results.

        I modified my code and I'm still not having luck. Can you take a look?

        Thanks.
        my $customer_id; $customer_id = param("customer_id"); print header(), start_html (-title => "Order Tracking System", -bgcolo +r => "white"); my $choice = lc (param ("choice")); if ($choice eq "submit") # customer submitted order number { get_customer (); print_results (); } else { print p (escapeHTML ("Logic error, unknown choice: $choice")); } print end_html(); sub get_customer { my ($dbh, $customer_id) = @_; $dbh = WebDB::connect (); my $sth = $dbh->prepare ("SELECT * FROM Shipments WHERE customer_id = +?"); $sth->execute($customer_id); while (my $track_ref = $sth->fetchrow_hashref ()){ push(@customer, $track_ref); } return(\@customer); } sub print_results { my $customer = get_customer($customer_id); 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")) + )); foreach my $row (@$customer) { print table ({-border => 0}, Tr ({-valign=>"center", -style=>"font-family: verdana; font-size: +10pt;"}, td ($row->{customer_id}), td ($row->{carrier_id}), 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 (); }