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:
Need to perform a break on subcategory

 



allanonline
Novice

Apr 9, 2004, 3:59 PM

Post #1 of 10 (815 views)
Need to perform a break on subcategory Can't Post

I am using a script that returns my results already sorted.

I need my results to display a title before each category not after each record

The reults I am looking for are
Our Soup Menu
Soup of the Day

A fresh pot made daily.
Cup - 3.50
Bowl - 4.50

French Onion Soup
A rich, beefy broth with saueed onions, croutons and melted cheese.
6.95

Our Salads
Caesar Salad
Crisp romaine lettuce with croutons, grated parmesan, bacon bits, and our house caesar dressing
5.75

Green Salad
A bed of mesculin greens topped with crisp garden vegetables and diced tomato.
4.95

Orange Salad
Mesculin greens topped with raisins, almond and mandarin orange segments. Served with our honey basil vinaigrette.
6.95

Chowders
Seafood Chowder
A hearty choder made with an abundance of seafood. Served with homemade biscuits.
Bowl - 6.95
Cup - 4.95

My results presently look like this http://www.seabreezeconsulting.com/cgi-bin/soupmenu.cgi
Thanks in advance
Allan
Unsure
Attachments: soupmenu.cgi (2.23 KB)


davorg
Thaumaturge / Moderator

Apr 10, 2004, 12:43 AM

Post #2 of 10 (813 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:


Code
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.


Code
#!/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


allanonline
Novice

Apr 10, 2004, 5:52 AM

Post #3 of 10 (810 views)
Re: [davorg] Need to perform a break on subcategory [In reply to] Can't Post

Hi Dave,

I really appreciate the help with this, but I'm still not getting the results I am looking for. I tried the code that you wrote and get an internal server error.

Tried writing the pseudo-code and get the $categorytitle[$i] to work on the first run, seems that I can't get a value into $prev_cat

I think I'm missing something in the translation.

I have attached a copy of the data file if that helps.

Thank You

Allan
Unsure

(This post was edited by allanonline on Apr 10, 2004, 5:57 AM)
Attachments: menu.dat (0.73 KB)


allanonline
Novice

Apr 10, 2004, 6:04 AM

Post #4 of 10 (806 views)
Re: [allanonline] Need to perform a break on subcategory [In reply to] Can't Post

The datafile is only partial

Full data file is attached here
Unsure
Attachments: menu.dat (5.41 KB)


davorg
Thaumaturge / Moderator

Apr 10, 2004, 6:52 AM

Post #5 of 10 (797 views)
Re: [allanonline] Need to perform a break on subcategory [In reply to] Can't Post

I'll bet that that the errors you're getting are because you have cut and pasted the code from the forum and it has broken the end of the heredoc markers.

For example, if you look at the code


Code
print <<SOF; 
<html>
<head>
<title>Soup Menu</title>
</head>
<body>
<table width="550" align="center">
SOF


You need to ensure that there is no extra whitespace after the closing "SOF". The same applies to all of the heredocs in the program.

And now I have your data I've found a couple of bugs in the code. I missed a column out of the definition of @cols. That should be:

Code
my @cols = qw(menuid image category subcategory title description italics price);

(adding "image" to the list)

Also I changed the name of a hash key accidently. The line that prints the category title needs to be

Code
print qq(<tr><td colspan="2">$_->{category_title}</td></tr>);

(changing "categorytitle" to "category_title")

Then running the program I can see one small problem. The output I get (omitting the details) is:

Code
Our Soup Menu 
French Onion Soup

Our Salads
Orange Salad
Green Salad
Caesar Salad

Chowders
Seafood Chowder

Our Soup Menu
Soup of the Day


You can see that you get the "Our Soup Menu" heading twice as the items are just produced in the order that they appear in the input file.

You can fix that by making sure that the input file is always in the right order, or you can perhaps sort @db_searchresults in some way before you start to display the contents.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


allanonline
Novice

Apr 10, 2004, 7:46 AM

Post #6 of 10 (792 views)
Re: [davorg] Need to perform a break on subcategory [In reply to] Can't Post

Hi Dave,

Sorry to be a pain but I am still getting a server error.

I'm pretty sure if I done everything you had suggested, would it be possible for you to upload a copy of the script?

Thanks

Allan
Unsure


allanonline
Novice

Apr 10, 2004, 1:37 PM

Post #7 of 10 (780 views)
Re: [allanonline] Need to perform a break on subcategory [In reply to] Can't Post

Hello Again

I was able to get it working using the pseudo-code, I had the $prev_cat = "$db_subcategory[$i]"; outside of the if statement.

Thanks for all your help, if you wouldn't mind, I would still like to have a copy of the code that you had written, justto see where I was making my mistake(s). This might help from making similar ones later.

Thanks Again

Allan
Unsure


davorg
Thaumaturge / Moderator

Apr 10, 2004, 11:00 PM

Post #8 of 10 (778 views)
Re: [allanonline] Need to perform a break on subcategory [In reply to] Can't Post


In Reply To
Sorry to be a pain but I am still getting a server error.


What sort of server error are you getting? What is in the web server error log?

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


allanonline
Novice

Apr 11, 2004, 3:03 AM

Post #9 of 10 (775 views)
Re: [davorg] Need to perform a break on subcategory [In reply to] Can't Post

Internal Server ErrorThe server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, webmaster@seabreezeconsulting.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.



Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
Apache/1.3.29 Server at www.seabreezeconsulting.com Port 80
Unsure


davorg
Thaumaturge / Moderator

Apr 11, 2004, 6:54 AM

Post #10 of 10 (772 views)
Re: [allanonline] Need to perform a break on subcategory [In reply to] Can't Post

That looks like the error that you get in the browser, not the one from the server error log.

Web servers deliberately give vague errors like that to the browser while putting the real error message into the error log.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks

 
 


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

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