Gossamer Forum
Home : Products : DBMan : Installation :

Counting no. of entries for each category

(Page 1 of 2)
> >
Quote Reply
Counting no. of entries for each category
I'm trying to set up a recipe database
which the entries are divided in different
categories (i.e. meat, fish, veg, etc). What
I would like to do is to have a page that
display the number of entries for each category (i.e. meat: 10 entries, fish: 5 entries etc.), is there a way to do this?
Quote Reply
Re: Counting no. of entries for each category In reply to
The following is how I managed to sort and display by category. I'm sure it wouldn't take much to add a count routine that was reset every time there was a change in category. (records would have to be sorted by category for this to work). There is probably an easier way but this might give you some ideas.

First define:
sub tmp_category{
my (%rec) = @_;
($db_auto_generate and print &build_html_record(%rec) and return);
$tmp_category=$rec{'Category'};
print"<br><em>$tmp_category</em>";#whatever format you want the category title to look like
}
Call this sub from view_success:
&tmp_category (&array_to_hash($_, @hits));
just before this line --for (0 .. $numhits - 1) {
In the html_record insert the following:
if ($rec{'Category'} ne $tmp_category ){
&tmp_category;
}#if category has changed print new category header and redefine $tmp_category
prior to the format of your record display
Quote Reply
Re: Counting no. of entries for each category In reply to
Thanks alot, but I think this mod
doesn't work with the "short display"
mod... Thanks anyway Smile
Quote Reply
Re: Counting no. of entries for each category In reply to
Assuming your recipe category is called "Category" you can add this to the page where you want it printed out:

Code:
$in{$db_key} = "*";
my ($status,@hits)=&query();
for (0 .. $db_total_hits - 1) {
%tmp = &array_to_hash($_, @hits);
++$count{$tmp{'Category'}};
}

Then when you go to print it out, use
print "Number of fish entries: $count{'Fish'}";

Be sure that you keep the same case, though. If you have fish in your database, you'll need to use $count{'fish'}.

If you want to wade through some of the past messages here, there's a thread called "more on query" (I think. I know "query" is in the title) with some more discussion on this.


------------------
JPD
Quote Reply
Re: Counting no. of entries for each category In reply to
Thanks again JPDeni! I think you code really
helps some of us. Smile

I sorta get it working, however I have no
idea where to put the code. Currently I put
it under sub html_view_success, this is not
an appropriate place for the counter I think.
Frown

How can I create a spearate page to show
the stats? Ideally I would like to place a
link in the menu or footer to link to the page.

Thanks again Smile I'll spend some more time on
the code while waiting for help...
Quote Reply
Re: Counting no. of entries for each category In reply to
Unless you have a whole lot of other stuff on your html_home page, I would put it there. Sort of an introduction to the database.


------------------
JPD
Quote Reply
Re: Counting no. of entries for each category In reply to
I see. That's not a bad idea. Smile
Quote Reply
Re: Counting no. of entries for each category In reply to
   In the above example:
print "Number of fish entries: $count{'Fish'}";

Is there any way to have your code display the number of entries for each category (i.e. meat: 10 entries, fish: 5 entries etc.) automatically.

Maybe some kind of loop that would do it for you.
Quote Reply
Re: Counting no. of entries for each category In reply to
Sure:

Code:
foreach my $category (keys %count) {
print "$category: $count{$category}<br>\n";
}

Cheers,

Alex
Quote Reply
Re: Counting no. of entries for each category In reply to
Thanks Alex,

I tried it but it didn't quite work for me.
Really dunno why...

here is the code:

Code:
$in{$db_key} = "*";
my ($status,@hits)=&query();
for (0 .. $db_total_hits - 1) {
%cattmp = &array_to_hash($_, @hits);
++$catcount{$cattmp{'Category'}};
}

foreach my $Category (keys %catcount) {
print "$Category: $catcount{$Category}<br>\n";
}

And my URL:

http://home.hkstar.com/~kwl1/recipe/db.cgi?db=default&uid=default

Sorry my dbman is in Chinese... But I put
the code in the footer and I think you should
be able to see what is happening.

Cheers,

kwli
Quote Reply
Re: Counting no. of entries for each category In reply to
How is it not working? I went to your site and, although I couldn't read anything there, I did see a list of words at the bottom with numbers by them. What is it (or isn't it) doing?


------------------
JPD
Quote Reply
Re: Counting no. of entries for each category In reply to
The way it's not working is that there
are totally 100 records most of them are
categorized, however the code retures a
": 90" first and then display the list of
items and numbers.

And the sum of the counted items is 10, which
is 100 minus 90...

Really have no idea what is happening..
Quote Reply
Re: Counting no. of entries for each category In reply to
You say "most of them are categorized"? You mean not all of them are? I don't know if this is the problem, but it could be.

You might try

Code:
%cattmp = &array_to_hash($_,@hits);
if ($cattmp{'Category'}) { ++$catcount{$cattmp{'Category'}}; }



------------------
JPD
Quote Reply
Re: Counting no. of entries for each category In reply to
Thanks again JP,

The ":90" is now disappeared, and I know kinda know what is happening,
it's the short display mod which makes it only count 10 records per page...
When I move on to next 10 records, it now counts the categories for the entries on
the next page.

Hmm....

Quote Reply
Re: Counting no. of entries for each category In reply to
 
Quote:
it's the short display mod which makes it only count 10 records per page...
When I move on to next 10 records, it now counts the categories for the entries on
the next page.

This should not be connected with your short display mod if you are using it on your home page.



------------------
JPD
Quote Reply
Re: Counting no. of entries for each category In reply to
Oh sure, cheers alot, really appriciate it.
Here is the code from sub home_html until
html_view_success.

Code:
##########################################################
## Home Page ##
##########################################################

sub html_home {
# --------------------------------------------------------
# The database manager home page.

open(FILE, "< $db_file_name") or &cgierr("can't open $db_file_name: $!");
$count++ while <FILE>;
close FILE;

$date = (stat($db_file_name))[9];
($day, $month, $year) = (localtime($date))[3,4,5,];
$month++; $year = $year + 1900;


&html_print_headers;
print qq|
<html>
<head>
<title>$html_title: 主選單</title>
</head>

<body bgcolor="#DDDDDD">
<center>
<table border=1 bgcolor="#FFFFFF" cellpadding=5 cellspacing=3 width=500 align=center valign=top>
<tr><td colspan=2 bgcolor="navy">
<FONT FACE="MS Sans Serif, arial,helvetica" size=4 COLOR="#FFFFFF">
<b>$html_title: 主選單</b>
</td></tr>
<tr><td>
<p><center><$font_title><b>
主選單
</b></font></center><br>
<font face="verdana,arial,helvetica" size="2" Color=#003399><b>
權限: |;

print " 搜尋 " if ($per_view);
print " 新增 " if ($per_add);
print " 刪除 " if ($per_del);
print " 修改 " if ($per_mod);
print " 管理 " if ($per_admin);
print " None " if (!($per_view &#0124; &#0124; $per_add &#0124; &#0124; $per_del &#0124; &#0124; per_mod));
print qq|</b></font><p>

$main_text

最新資料輸入日期: $day-$month-$year

|; &html_footer;

##########################################################
# Stats mod
##########################################################
$in{$db_key} = "*";
my ($status,@hits)=&query();
for (0 .. $db_total_hits - 1) {
%cattmp = &array_to_hash($_,@hits);
if ($cattmp{'Category'}) { ++$catcount{$cattmp{'Category'}}; }}

foreach my $Category (keys %catcount) {
print "$Category: $catcount{$Category}<br>\n";
}
##########################################################
# End Stats mod
###########################################

print qq|


</td></tr>
</table>
</center>
</body>
</html>
|;
}

sub html_footer {
# --------------------------------------------------------
# Print the menu and the footer and logo. It would be nice if you left
# the logo in. Wink
#
# We only print options that the user have permissions for.
#

my $font = 'Font face="Verdana, Arial, Helvetica" Size=2';


print qq! <br><P align=center><$font>!;
print qq!<HR WIDTH="100%"><center>[ <A HREF="$db_script_link_url">主頁</A> !;
print qq!| <A HREF="$db_script_link_url&add_form=1">新增</A> ! if ($per_add);
print qq!| <A HREF="$db_script_link_url&view_search=1">搜尋</A> ! if ($per_view);
print qq!| <A HREF="$db_script_link_url&delete_search=1">刪除</A> ! if ($per_del);
print qq!| <A HREF="$db_script_link_url&modify_search=1">修改</A> ! if ($per_mod);
print qq!| <A HREF="$db_script_link_url&view_records=1&$db_key=*">觀看所有紀錄</A> ! if ($per_view);
print qq!| <A HREF="$db_script_link_url&admin_display=1">管理</A> ! if ($per_admin);
print qq!| <A HREF="$db_script_link_url&logoff=1">退出</A> ]</center><HR WIDTH="100%">!;
print qq!</font></p>!;

# Print the Footer.
print qq!


<table border=0 width=100%>
<tr><td align=left><$font>請到 <A HREF="http://home.hkstar.com/~kwl1/recipe/forum/">中文食譜搜尋器臨時討論區</A> 發表意見。<p>
[ <a href="$db_script_link_url&%A5%5B%A4J%A4%E9%B4%C1=%3E$Last&view_records=1">最近三天加入的紀錄</a> ] 現時資料庫已載有 $count 個食譜資料!<br>
</font></td></table><p>

!;



}

sub html_search_options {
# --------------------------------------------------------
# Search options to be displayed at the bottom of search forms.
#
print qq~
<P><strong>搜尋選項:</strong><br>
<INPUT TYPE="CHECKBOX" NAME="ma"> 任何字相同
<INPUT TYPE="CHECKBOX" NAME="cs"> 大小寫相同
<INPUT TYPE="CHECKBOX" NAME="ww"> 全字相同
<INPUT TYPE="CHECKBOX" NAME="re"> 規則詞句<BR>
<INPUT TYPE="TEXT" NAME="keyword" SIZE=15 MAXLENGTH=255> 關鍵字搜尋 <FONT SIZE=-1> (自動配對所有欄目)</FONT><BR>
<INPUT TYPE="TEXT" NAME="mh" VALUE="$db_max_hits" SIZE=3 MAXLENGTH=3> 最多顯示合選結果<BR>
排序選項:
<SELECT NAME="sb">
<OPTION>---
~; for (my $i =0; $i <= $#db_cols; $i++) { print qq~<OPTION VALUE="$i">$db_cols[$i]</OPTION>\n~ if ($db_form_len{$db_cols[$i]} >= 0); } print qq~
</SELECT>
排序方式:
<SELECT NAME="so">
<OPTION VALUE="ascend">遞增
<OPTION VALUE="descend">遞減
</SELECT><p>


<strong>
搜尋小貼士:</strong><p>

- 在欄內用 '*' 號來配對所有紀綠<BR>
- 在開首加 '>' 或 '<' 號 即可作範圍搜尋<BR>

<p><b> 列印格式:</b>
<SELECT NAME="format">
<OPTION>---
<OPTION VALUE="long">詳細列表
<OPTION VALUE="short">簡單列表

</SELECT><br>
~;
}

##########################################################
## Adding ##
##########################################################

sub html_add_form {
# --------------------------------------------------------
# The add form page where the user fills out all the details
# on the new record he would like to add. You should use
# &html_record_form to print out the form as it makes
# updating much easier. Feel free to edit &get_defaults
# to change the default values.

&html_print_headers;
print qq|
<html>
<head>
<title>$html_title: 新增資料</title>
</head>

<body bgcolor="#DDDDDD">
<form action="$db_script_url" method="POST">
<input type=hidden name="db" value="$db_setup">
<input type=hidden name="uid" value="$db_uid">
<center>
<table border=1 bgcolor="#FFFFFF" cellpadding=5 cellspacing=3 width=500 align=center valign=top>
<tr><td colspan=2 bgcolor="navy">
<FONT FACE="MS Sans Serif, arial,helvetica" size=4 COLOR="#FFFFFF">
<b>$html_title: 新增資料</b>
</td></tr>
<tr><td>
<p><center><$font_title><b>
加入新資料
</b></font></center><br>
<$font>
|; &html_record_form (&get_defaults); print qq|
</font></p>
<p><center> <INPUT TYPE="SUBMIT" NAME="add_record" VALUE="新增紀錄"> </center></p>
|; &html_footer; print qq|
</td></tr>
</table>
</center>
</form>
</body>
</html>
|;
}

sub html_add_success {
# --------------------------------------------------------
# The page that is returned upon a successful addition to
# the database. You should use &get_record and &html_record
# to verify that the record was inserted properly and to make
# updating easier.

&html_print_headers;
print qq|
<html>
<head>
<title>$html_title: 新增紀錄完成</title>
</head>

<body bgcolor="#DDDDDD">
<center>
<table border=1 bgcolor="#FFFFFF" cellpadding=5 cellspacing=3 width=500 align=center valign=top>
<tr><td colspan=2 bgcolor="navy">
<FONT FACE="MS Sans Serif, arial,helvetica" size=4 COLOR="#FFFFFF">
<b>$html_title: 新增紀錄完成</b>
</td></tr>
<tr><td>
<p><center><$font_title><b>
新增紀錄完成
</b></font></center><br>
<$font>
<P>新增以下紀錄:</FONT>
|; &html_record(&get_record($in{$db_key})); print qq|
</p>
|; &html_footer; print qq|
</td></tr>
</table>
</center>
</body>
</html>
|;
}

sub html_add_failure {
# --------------------------------------------------------
# The page that is returned if the addition failed. An error message
# is passed in explaining what happened in $message and the form is
# reprinted out saving the input (by passing in %in to html_record_form).

my ($message) = $_[0];

&html_print_headers;
print qq|
<html>
<head>
<title>$html_title: 系統錯誤:不能加入資料</title>
</head>

<body bgcolor="#DDDDDD">
<form action="$db_script_url" method="POST">
<input type=hidden name="db" value="$db_setup">
<input type=hidden name="uid" value="$db_uid">
<center>
<table border=1 bgcolor="#FFFFFF" cellpadding=5 cellspacing=3 width=500 align=center valign=top>
<tr><td colspan=2 bgcolor="navy">
<FONT FACE="MS Sans Serif, arial,helvetica" size=4 COLOR="#FFFFFF">
<b>$html_title: 系統錯誤:不能加入資料</b>
</td></tr>
<tr><td>
<p><center><$font_title><b>
錯誤: <font color=red> 不能加入紀錄</font>
</b></font></center><br>
<$font>
以下欄目出現問題: <FONT COLOR="red"><B>$message</B></FONT>
<P>請修正錯誤後再新增紀錄。</p></font>
|; &html_record_form (%in); print qq|
</font></p>
<p><center> <INPUT TYPE="SUBMIT" NAME="add_record" VALUE="新增紀錄"> </center></p>
|; &html_footer; print qq|
</td></tr>
</table>
</center>
</form>
</body>
</html>
|;
}
##########################################################
## Viewing ##
##########################################################

sub html_record_short {

my (%rec) = @_;
my $font_color = 'Font face="arial" size=-1 Color=#003399';
my $font = 'Font face="arial" size=-1 ';
print qq|
<a href="$db_script_link_url&ID=$rec{'ID'}&format=long&view_records=1&ww=on">$rec{'菜名'}</a>
</TR>
</TABLE>
|;
}


sub html_view_search {
# --------------------------------------------------------
# This page is displayed when a user requests to search the
# database for viewing.
# Note: all searches must use GET method.
#
&html_print_headers;
print qq|
<html>
<head>
<title>$html_title: 搜尋紀錄庫</title>
</head>

<body bgcolor="#DDDDDD">
<form action="$db_script_url" method="GET">
<input type=hidden name="db" value="$db_setup">
<input type=hidden name="uid" value="$db_uid">
<center>
<table border=1 bgcolor="#FFFFFF" cellpadding=5 cellspacing=3 width=500 align=center valign=top>
<tr><td colspan=2 bgcolor="navy">
<FONT FACE="MS Sans Serif, arial,helvetica" size=4 COLOR="#FFFFFF">
<b>$html_title: 搜尋紀錄庫</b>
</td></tr>
<tr><td>
<p><center><$font_title><b>
搜尋紀錄庫
</b></font></center><br>
<$font>
|; &html_record_form(); print qq|
|; &html_search_options; print qq|
</font></p>
<p><center> <INPUT TYPE="SUBMIT" NAME="view_records" VALUE="搜尋紀錄"> <INPUT TYPE="RESET" VALUE="重設表格"></center></p>
|; &html_footer; print qq|
</td></tr>
</table>
</center>
</form>
</body>
</html>
|;
}

sub html_view_success {
# --------------------------------------------------------
# This page displays the results of a successful search.
# You can use the following variables when displaying your
# results:
#
# $numhits - the number of hits in this batch of results.
# $maxhits - the max number of hits displayed.
# $db_total_hits - the total number of hits.
# $db_next_hits - html for displaying the next set of results.
#


my (@hits) = @_;
my ($numhits) = ($#hits+1) / ($#db_cols+1);
my ($maxhits); $in{'mh'} ? ($maxhits = $in{'mh'}) : ($maxhits = $db_max_hits);



&html_print_headers;
print qq|
<html>
<head>
<title>$html_title: 搜尋結果</title>
</head>

|;
if ($in{'format'} eq "long") {
print qq|

<body bgcolor="#DDDDDD">
<blockquote>
<table border=1 bgcolor="#FFFFFF" cellpadding=5 cellspacing=3 width=500 valign=top>
<tr><td colspan=2 bgcolor="navy"><FONT FACE="MS Sans Serif, arial,helvetica" size=1 COLOR="#FFFFFF">
<b>$html_title: 搜尋結果</b>
</font></td></tr>
</table>
<p><$font>
閣下是次搜尋合選紀錄為 <b>$db_total_hits</b> 項。</font>
|;

if ($db_next_hits) {

print "<br><$font>Number of test entries: $catcount{'one'}</font>";
print "<br><$font>頁數: $db_next_hits</font>";

}

# Go through each hit and convert the array to hash and send to
# html_record for printing.

for (0 .. $numhits - 1) {
print "<P><br><br>";
&html_record (&array_to_hash($_, @hits));
}
if ($db_next_hits) {
print "<br><$font>頁數: $db_next_hits</font>";
}

print qq|

<p>
<table border=0 bgcolor="#DDDDDD" cellpadding=5 cellspacing=3 width=500 valign=top>
<tr><td>|; &html_footer; print qq|</td></tr>
</table>
</blockquote>
</body>
</html>
|;
}
else {


print qq|

<body bgcolor="#DDDDDD">
<blockquote>
<table border=1 bgcolor="#FFFFFF" cellpadding=5 cellspacing=3 width=500 valign=top>
<tr><td colspan=2 bgcolor="navy"><FONT FACE="MS Sans Serif, arial,helvetica" size=1 COLOR="#FFFFFF">
<b>$html_title: 搜尋結果</b>
</font></td></tr>
</table>
<p><$font>
閣下是次搜尋合選紀錄為 <b>$db_total_hits</b> 項。</font>p
|;


if ($db_next_hits) {
print "<br><$font>頁數: $db_next_hits</font>";
}
print " <TABLE border=1 cellpadding=1> ";


# Go through each hit and convert the array to hash and send to
# html_record for printing.

for (0 .. $numhits - 1) {
print "<P>";
&html_record_short (&array_to_hash($_, @hits));
}
if ($db_next_hits) {
print "<br><p><$font>頁數: $db_next_hits</font></table>";
}

print qq|

<p>

<table border=0 bgcolor="#DDDDDD" cellpadding=5 cellspacing=3 width=500 valign=top>
<tr><td>|; &html_footer; print qq|</td></tr>
</table>
</blockquote>
</body>
</html>
|;
}
}
Quote Reply
Re: Counting no. of entries for each category In reply to
Tried it last nite already, I put it
in the main page... still the same..
Quote Reply
Re: Counting no. of entries for each category In reply to
The only thing I can suggest is that you make your html.pl file available for me to look at on the web -- or at least the html_home subroutine.


------------------
JPD
Quote Reply
Re: Counting no. of entries for each category In reply to
I still don't see it. What do you get if you print out $db_total_hits?


------------------
JPD
Quote Reply
Re: Counting no. of entries for each category In reply to
I added a line to print $db_total_hits and
it returns 100.

Code:
$in{$db_key} = "*";
my ($status,@hits)=&query();
for (0 .. $db_total_hits - 1) {
%cattmp = &array_to_hash($_,@hits);
if ($cattmp{'Category'}) { ++$catcount{$cattmp{'Category'}}; }}

foreach my $Category (keys %catcount) {
print "$Category: $catcount{$Category}<br>\n";
print "$db_total_hits";
}

Getting interesting...
Quote Reply
Re: Counting no. of entries for each category In reply to
Well, that tells us that it is getting all the results. Next bit of debugging:

Code:
$in{$db_key} = "*";
my ($status,@hits)=&query();
for (0 .. $db_total_hits - 1) {
%cattmp = &array_to_hash($_,@hits);
if ($cattmp{'Category'}) { ++$catcount{$cattmp{'Category'}};
++$count;
}}
foreach my $Category (keys %catcount) {
print "$Category: $catcount{$Category}<br>\n";
print "$count";
}

It should still give you 100. If not, we'll know that it's not going through the loop enough times. If it does give you 100, then I'll have to think of something else. Smile


------------------
JPD
Quote Reply
Re: Counting no. of entries for each category In reply to
This time it says 110! oh...

like this:

"主菜": 1
110"其他": 1
110"粥品": 1
110"飯": 2
110"小食": 3
110"湯": 1
110"頭盤": 1
110
Quote Reply
Re: Counting no. of entries for each category In reply to
Code:
++$count{$tmp{'Category'}};

How would you initialize this counter to 0 so that you don't get any empty variables? I tried all sorts of ways and I still keep on getting an empty value.
Quote Reply
Re: Counting no. of entries for each category In reply to
David--

Did you include the

if ($tmp{'Category'}) {

condition? That should take care of your problem. Either that, or make sure that every record has an entry in that field.

kwli--

The reason the "110" came up on every row is that I told you to put the $count variable in the wrong place. Smile



------------------
JPD

All my advice is offered on the basis of kindness-ware. If I've helped you to solve a problem, go out of your way to be kind to someone today.

Quote Reply
Re: Counting no. of entries for each category In reply to
JPDeni,

To my suprise it didn't return a long list,
rather, it prints out the list of %db_select_fields for 'Category' first, otherwise it's
the same as last time...

Code:

"頭盤" "粥品" "其他" "湯" "主菜" "小食" "飯" "飯" "小食" "小食"
"主菜": 1
110"其他": 1
110"粥品": 1
110"飯": 2
110"小食": 3
110"湯": 1
110"頭盤": 1
110

The characters in double-quotes are the
select fields, really no idea how to hack this one. Well, thanks again JPD..
> >