
davorg
Thaumaturge
/ Moderator
Apr 10, 2004, 12:43 AM
Post #2 of 10
(545 views)
|
|
Re: [allanonline] Need to perform a break on subcategory
[In reply to]
|
Can't Post
|
|
Create a variable (called something like $prev_cat and initialise it to an empty string. Each time round your main loop, only print out the category header if the category in the current record is different to the value in $prev_cat. Also, at the end of each loop interation, copy the current category into $prev_cat. So, in pseudo-code, it would look something like this:
my $prev_cat = ''; while ($count) { if ($prev_cat ne $categorytitle[$i]) { # print catergory title } # print main record. --$countl $prev_cat = $categorytitle[$i]; } But having taken a closer look at your whole program I think that it could benefit from a major rewrite. Here's how I would write it.
#!/usr/bin/perl ################################################################ # Allan Clark # aclark@allstream.net ################################################################ use strict; use warnings; use CGI ':cgi'; print header; print <<SOF; <html> <head> <title>Soup Menu</title> </head> <body> <table width="550" align="center"> SOF ####### open(DATABASE, "odpmenu/menu.dat") || die "Can't open input: $!"; my @cols = qw(menuid category subcategory title description italics price); my @db_searchresults; #################### search query while (<DATABASE>) { my %rec; @rec{@cols} = split /:/; my $cat = "soups"; next unless $rec{category} eq $cat; push @db_searchresults, \%rec; } unless (@db_searchresults) { print <<END_OF_ERROR; <BR><div align=center> <b><font face='Verdana' color=Navy size="-1">Sorry your search returned 0 results</font></b><br> <FORM><INPUT TYPE = "BUTTON" VALUE = " Return to Search Page " onClick = "window.history.go(-1);"></FORM></div> END_OF_ERROR exit; } my %sub_cats = ('a-soup' => "<b>Our Soup Menu</b>\n", 'b-salad' => "<b>Our Salads</b>\n", 'c-chowder' => "<b>Chowders</b>\n"); my $prev_cat = ''; foreach (@db_searchresults) { $_->{category_title} = $sub_cats{$_->{subcategory}}; print '<TR valign="top">'; print '<TD><table width="100%">'; if ($_->{category_title} ne $prev_cat) { print qq(<tr><td colspan="2">$_->{categorytitle}</td></tr>); } print <<END_OF_REC; <tr valign="top"><td width="30"> </td><td><b>$_->{title}</b></td></tr> <tr valign="top"><td width="30"> </td><td>$_->{description}</td></tr> <tr valign="top"><td width="30"> </td><td>$_->{price} </td></tr></table><br> </TD> END_OF_REC $prev_cat = $_->{category_title}; } print <<EOF; </tr> </table> </body> </html> EOF Unfortunately, as I don't have access to your data file, I can't test my version. -- Dave Cross, Perl Hacker, Trainer and Writer http://www.dave.org.uk/ Get more help at Perl Monks
|