in reply to Re: No results from script
in thread No results from script

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.

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

    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 (); }

        cool.. In get_customer() your customer id is being put into $dbh. As you assign directly to $dbh you don't need it in that first line, so..

        change: sub get_customer { my ($dbh, $customer_id) = @_; $dbh = WebDB::connect (); ... to: sub get_customer { my ($customer_id) = @_; my $dbh = WebDB::connect ();

        Now you really want to keep the subs separate, so instead of calling get_customer from print_results we get_customer, then send the customer details to print results..

        if ($choice eq "submit") # customer submitted order number { get_customer (); print_results (); } else ... becomes: if ($choice eq "submit") # customer submitted order number { my $cutomer = get_customer($customer_id); print_results($customer); } else ...

        Then we change print_results to accept the customer details as an argument

        sub print_results { my $customer = get_customer($customer_id); ... becomes: sub print_results { my $customer = @_;

        cheers,

        J