CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Print to a Table

 



six
stranger

Apr 29, 2001, 6:04 AM

Post #1 of 3 (311 views)
Print to a Table Can't Post

Hi All,

A while back, rGeoffrey helped me with a sort and a print from my database and i would like to see if I could take it one step further.

As you can see, he gave me the code to take all the rows in my database and give me the link to each 'Category' even tho there would be multiple listings under each one.


Code
  sub do_cat_update 
{
open(ARRAY, "../Data_files/shopping.data") || &eg_error(array);
@data = <ARRAY>;
close(ARRAY);
my %hash;
foreach (@data) {
my $category = (split ('\|', $_))[1];
$hash{$category} = 1 unless (exists ($hash{$category}));
}
my @links = map { '<a href="%%sc_link%%&product=' . "$_\">$_</a>\n" } sort keys %hash;

open(CONFIG, ">../Library/cat.cfg") || &eg_error(cat_cfg);
print CONFIG "@links</font>\n";
close(CONFIG);

}

What I would like to do now is print them to a table. I have the basic code down to do this but my question is let's say I have 9 categories, how could I print that to three rows of three categories.

Code
<table>  
<tr><td>category</td><td>category</td><td>category</td></tr>
<tr><td>category</td><td>category</td><td>category</td></tr>
<tr><td>category</td><td>category</td><td>category</td></tr>
</table>



I have tried numerous combinations but can't get close and this board is always a good source when you are stumped...

Brad



rGeoffrey
User / Moderator

Apr 30, 2001, 10:02 PM

Post #2 of 3 (296 views)
Re: Print to a Table [In reply to] Can't Post

After you have your array of things to print, you can use this...


Code
my @array = qw (apple banana pear kiwi orange tangerine grapefruit 
watermelon strawberry blueberry blackberry);

&make_table (3, 'Fruits', @array);

sub make_table
{
my ($width, $message, @images) = @_;
my $columns = (@images > $width) ? $width : scalar (@images);

print "<table><tr><th colspan=\"$columns\">$message</th></tr>\n";

while (@images) {
print ("<tr>",
(map { "<td>$_</td>" } splice (@images, 0, $columns)),
"</tr>\n"
);
}

print "</table>\n";
}

The splice is used to grab the parts in $width chunks for each row. And in the last row it places the leftovers. As an added bonus if you set width higher than the number of elements in the array, then it will reduce the colspan in the header row to the number of columns you actually need.

--
Sun Sep 9, 2001 - 1:46:40 GMT, a very special second in the epoch. How will you celebrate?


six
stranger

May 2, 2001, 1:11 AM

Post #3 of 3 (285 views)
Re: Print to a Table [In reply to] Can't Post

Many thanks again rGeoffrey,

As usual, it worked like a charm.

I didn't need the <th> thingy, but after getting the snippet working, I've made that another working variable in the script...

Again, Thanks for your time

Brad


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives