Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Printing Tables...

by powerhouse (Friar)
on Mar 06, 2003 at 22:09 UTC ( #241008=perlquestion: print w/replies, xml ) Need Help??

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

Hello, Fellow Monks! I have been racking my brain, trying to do something, but cannot get it figured out(in my head). Can you please offer me some advise...

header cell1 header cell2
content cell1 content cell2


I am pulling data out of a MySQL database, and in a while statement I'm putting the information in a table.

The problem is that, as you know, in the WHILE statment, I only have 1 "database row" to work with at a time and the format of a table requires 2 cells side by side: <td></td><td></td>Before the new row. Of course you already know that.

How can I get that information to "content cell1", without knowing the 'header' for "header cell2"?

In other words, if I am on record 1, and I need the header(name) in "header cell1" and the content for "content cell1" but before I can get to "content cell1", I have to create "header cell2". Do you see what I'm trying to do? I often cannot explain things the way I mean them, I'm told by my wife ;o)

If you don't understand, please reply back, and I'll try to explain it better.

thx,
Richard

Replies are listed 'Best First'.
Re: Printing Tables...
by robartes (Priest) on Mar 06, 2003 at 22:18 UTC
    I think you'll have to prefetch the information for cell 2. For example, use a AoH such as:
    @AoH = [ { ('header' => "Header cell 1", 'content' => "Content cell 1", ) } , { ('header' => "Header cell 2", 'content' => "Content cell 2", ) } , ] ;
    The actual information in the AoH comes from the database, of course. Then, when you print your table, just use the information in the AoH. You'll find that it has many advantages to seperate your data and presentation layers this way.

    CU
    Robartes-

Re: Printing Tables...
by cchampion (Curate) on Mar 07, 2003 at 01:45 UTC

    Check this module. DBIx::HTML_Table written by jeffa.

    Perhaps you could solve the problem without so much effort.

    cchampion

    update Corrected module name. Thanks to PodMaster

Re: Printing Tables...
by Mr. Muskrat (Canon) on Mar 06, 2003 at 22:25 UTC
    I think that this code will help. (Provided I read your post correctly.)
    my $sth = $dbh->prepare($sql); if (!$sth) { die "sth error:" . $dbh->errstr . "\n"; } if (!$sth->execute) { die "sth execute error:" . $sth->errstr . "\n"; } my $names = $sth->{'NAME'}; # the info you seek foreach my $name (@{$names}) { # do something with the name }
Re: Printing Tables...
by Enlil (Parson) on Mar 06, 2003 at 22:40 UTC
    Here is some pseudocode to work from:
    push header from row into hash element @{$hash{HEADER}}
    push content from row into hash element @{$hash{CONTENT}}
    to print table then you can just do something like the following (untested):
    print "<TABLE><TR><TD>", join "</TD><TD>",@{$hash{HEADER}}; print "</TD></TR><TR><TD>, join "</TD><TD>",@{$hash{CONTENT}}; print "</TD></TR></TABLE>";

    -enlil

Re: Printing Tables...
by ehdonhon (Curate) on Mar 06, 2003 at 23:13 UTC

    Are you saying that each row in your HTML table actually represents one column from your SQL table? If so, you should take a look at the selectcol_arrayref or fetchall_arrayref methods of DBI.

Re: Printing Tables...
by powerhouse (Friar) on Mar 06, 2003 at 23:35 UTC
    Thank you, one and all! What I'm doing is building a shopping cart for my wifes site that I'm building for her.

    The information coming out of the database is for each product. I'm doing a search for each product that contains both the correct Category ID and that she has turned "on" in the database using the interface I built for her. Then it is using a while($row = $sth->fetchrow_hashref()) { to create the content of the database. There are two cells for each product in the Header is the name. In the bottom cell right below the header is a picture of the item, the description, retail price and our price. Plus, it says if the product is in stock, if not then says the ETA, and has a link where they can add it to a "watch list" to be notified by email when the product is back in stock. PLUS, if the visitor is logged in AND they are a wholesaler it shows their price too. AND a add to cart link.

    The problem I'm having is that since the shopping cart is showing 2 products side by side (4 cells, 2 headers 2 content cells), I'm having difficulty getting the second cell for the product on the left, since I have to create the header of the 1st cell.

    I had been thinking of a simple way to do it and I come up with this...(I cannot get it to work so I'm going to contemplate your various ideas)

    I've created a counter. If the counter is 0 then it creates the header1 the way it is supposed to. Then for header 2 it adds this: {{headercell2}} Then it creates the content cell 1 with all the data, then it prints the content cell 2 with this {{contentcell2}}
    Then finishes. and adds 1 to the counter.

    If the counter is 1, then instead of doing the above it just does a switch:
    $string_w_data =~ s/{{headercell2}}/$header_information/;
    It does that for both the header and content cell, then resets the counter to start over.

    I feel like that is a sloppy way to do it, but it SHOULD work, but currently does not.

    I'm going to give it one more hack at it, then If I cannot get it to work, I'm going to try each of your examples, to the best of my ability. I'm by no means a Perl Guru, so Some of them are over my head, so I'll have to learn more ;o)

    Thank you, again,
    Richard

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://241008]
Approved by ehdonhon
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2022-06-25 19:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (83 votes). Check out past polls.

    Notices?