Gossamer Forum
Home : Products : DBMan : Customization :

Counter for different TYPE

Quote Reply
Counter for different TYPE
From the default database of DBman, there's a field name TYPE (web,newsgroup,mailing list,ftp, gopher)

I want to display the counter for each TYPE.EG
WEB - 20
NEWSGROUP -4
MAILING LIST -18
FTP - 3
GOPHER - 39

Quote Reply
Re: Counter for different TYPE In reply to
I gotta make up a mod for this! Unfortunately, UBB won't search for things other than letters and numbers, so I can't find the previous times that I've given this code.

I'm assuming you want the categories to list on the db home page. If so, in sub html_home, at the beginning, add:

Code:
open (DB, "<$db_file_name") or &cgierr("error unable to open db file: $db_file_name.\nReason: $!");
if ($db_use_flock) { flock(DB, 1); }
@lines = <DB>; # Slurp the database into @lines..
close DB;
 
foreach $line (@lines) {
@data = split (/\Q$db_delim\E/o, $line);
if ($data[6]) {
++$count{$data[6]};
++$total_count;
}
}

Be sure to change the 6 above (both places) to the number of the field that holds the data you want to count.

In the place where you want to print out the information, use

Code:
foreach $type (%count) {
print qq|$type - $count{$type}<BR>|;
}
print qq|Total - $total_count|;


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








[This message has been edited by JPDeni (edited August 09, 1999).]
Quote Reply
Re: Counter for different TYPE In reply to
I only get this

Total -

Quote Reply
Re: Counter for different TYPE In reply to
Is all the code in the same subroutine?


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





Quote Reply
Re: Counter for different TYPE In reply to
Yap, its all in the same sub routine.
Quote Reply
Re: Counter for different TYPE In reply to
The only other reasons I can think of that it wouldn't work is that you don't have any records with anything in the field $data[#].

You might try this:

after
@data = split (/\Q$db_delim\E/o, $line);

add

$output .= "data[0] ";

And then after

print qq|Total - $total_count|;

add

print "<BR>$output";

You should get a list of the values of the first field in all of your records.



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





Quote Reply
Re: Counter for different TYPE In reply to
++total_count;

does it have a $? $total_count

and, do I have to add total_count, count, to default.cfg?

Quote Reply
Re: Counter for different TYPE In reply to
Yes. I'm sorry. I forgot to put in the $.

It should be

++$total_count;

(I'll change it in the original so folks don't get confused.)

No, you don't need to add anything to the default.cfg file. These are not fields.

When you said

Quote:
I only get this

Total -

Did you mean that you didn't get the listing of the types or just that there was no total printed?




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





Quote Reply
Re: Counter for different TYPE In reply to
-
Total -

this is what i get

--------------------------------------
if ($db_use_flock) { flock(DB, 1); }
@lines = <DB>; # Slurp the database into @lines..
close DB;

foreach $line (@lines) {
@data = split (/\Q$db_delim\E/o, $line);
$output .= "data[0] ";
if ($data[3]) {
++$count{$data[3]};
++$total_count;
}
}
-----------------------------------------

I have data in 3(I am using the default db/cfg..) which is the TYPE
Quote Reply
Re: Counter for different TYPE In reply to
Okay. That means the split isn't working.

Try this. Change

@data = split (/\Q$db_delim\E/o, $line);

to

@data = split (/\|/, $line);

See what happens then.

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





Quote Reply
Re: Counter for different TYPE In reply to
still the same after changing to @data = split (/\|/, $line);
Quote Reply
Re: Counter for different TYPE In reply to
Okay. We'll back up further.

After

close DB;

add

$output = $line[0];

Be sure that you still have the line

print "<BR>$output";

after printing out the total (which doesn't print out, I know. Smile )


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


I just noticed something on your earlier message where you posted the code you used. You didn't include the line:

Code:
open (DB, "<$db_file_name") or &cgierr("error unable to open db file: $db_file_name.\nReason: $!");

Is that line in your script?




[This message has been edited by JPDeni (edited August 08, 1999).]
Quote Reply
Re: Counter for different TYPE In reply to
Ok..not sure if its correct.

--------------------------------------------
sub html_home {
# --------------------------------------------------------
# The database manager home page.
if ($db_use_flock) { flock(DB, 1); }
@lines = <DB>; # Slurp the database into @lines..
close DB;
$output = $line[0];
foreach $line (@lines) {
@data = split (/\|/, $line);
if ($data[3]) {
$output .= "data[0] ";
++$count{$data[3]};
++$total_count;
}
}
$page_title = "Main Menu";
&html_page_top;


$days = 31; # set this one day more than you want records to be considered "new."
# If you want to include records from the previous 7 days, set the number
# to 8.

$new = &get_date(time() - ($days * 86400));

# < -- Start page text -- >
print qq|
<$face size="1"><b>
Permissions:
|;
print " View " if ($per_view);
print " Add " if ($per_add);
print " Delete " if ($per_del);
print " Modify " if ($per_mod);
print " Admin " 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><$font>
This database has been set up so any user can view any other users information, but you can
only modify and delete your own records. If you have admin access, you can of course do anything
you like.<br><br>
|;
# print qq! <a href="$db_script_link_url&Date-gt=$new&view_records=1">What's New</a> ! if ($per_view);


# < -- End page text -->

# $in{'Date-gt'} = $new;
# my ($status, @hits) = &query("view");
# if ($status eq "ok") {
# my ($numhits) = ($#hits+1) / ($#db_cols+1);
# my ($maxhits); $in{'mh'} ? ($maxhits = $in{'mh'}) : ($maxhits = $db_max_hits);
# print "These are the newest members:<P>";
# for (0 .. $numhits - 1) {
# %rec = &array_to_hash($_, @hits);
# print qq|
# <a href="$db_script_link_url&$db_key=$rec{$db_key}&ww=1&view_records=1">$rec{'Userid'}</a><br>|;
# }
# }




foreach $Type ($count) {
print qq| $Type - $count{$Type}<BR>|;
}
print qq| Total - $total_count|;
print "<BR>$output";



&html_footer;
&html_page_bottom;
}

--------------------------------------------
after adding the $output = $line[0];

nothing appear also
Quote Reply
Re: Counter for different TYPE In reply to
Sorry, I know where to put the line already.

anyway, here's the result I got
-
Total - 11

which is, not excatly what I want.
As you can see,
Type => 'Web,Newsgroup,Mailing List,FTP,Gopher'

What I want is extract from the TYPE data,
Web = 10
Newsgroup =33
etc etc

[This message has been edited by nels (edited August 08, 1999).]
Quote Reply
Re: Counter for different TYPE In reply to
After

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

If you don't open the file, nothing can be read or counted.


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







[This message has been edited by JPDeni (edited August 08, 1999).]
Quote Reply
Re: Counter for different TYPE In reply to
I need to see your html.pl file. Please copy it to a web-accessible directory -- one where you would place html files -- and come back here and tell me where it is and I'll go look at it.


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





Quote Reply
Re: Counter for different TYPE In reply to
Its still not showing.

Quote Reply
Re: Counter for different TYPE In reply to
I know. I realized it as soon as I woke up this morning. (I really need to make up a mod for this so I don't make stupid mistakes!)

It should be

foreach $type (%count) {



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





Quote Reply
Re: Counter for different TYPE In reply to
http://216.221.184.157/html.txt
Quote Reply
Re: Counter for different TYPE In reply to
Okay. Now I see my mistake.

Instead of

foreach $type ($count) {

it should be

foreach $type (@count) {

Sorry 'bout that.


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





Quote Reply
Re: Counter for different TYPE In reply to
This is what i got.

Newsgroup - 2
2 -
FTP - 1
1 -
Gopher - 1
1 -
Mailing List - 5
5 -
Web - 2
2 -
Total - 11

how come got this
2 -
etc etc


Quote Reply
Re: Counter for different TYPE In reply to
I don't know. It doesn't make any sense to me. Someone else said he had the same thing happen.

Okay. I think I've got it. (I'm sorry about this. We're all learning here.)

Instead of

foreach $type (%count) {

try

foreach $type (keys %count) {

If this doesn't do it, I don't know what will.

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





Quote Reply
Re: Counter for different TYPE In reply to
a thousand thanks to u JPDeni.

It works! anyway, isit possible to have more than one ? eg besides the Type, I want to have Validated shown also.
Quote Reply
Re: Counter for different TYPE In reply to
Sure. But some of the things would have to change.

Code:
open (DB, "<$db_file_name") or &cgierr("error unable to open db file: $db_file_name.\nReason: $!");
if ($db_use_flock) { flock(DB, 1); }
@lines = <DB>; # Slurp the database into @lines..
close DB;

$total_count=scalar(@lines);

foreach $line (@lines) {
@data = split (/\Q$db_delim\E/o, $line);
if ($data[6]) {
++$count6{$data[6]};
}
if ($data[7]) {
++$count7{$data[7]};
}
}

You can count the values in every field like that. As long as your $count value is different for each one, you can keep them straight.

Then, when you print them out, use what I ended up giving you for each one:

Code:
foreach $type (keys %count6) {
print qq|$type - $count6{$type}<BR>|;
}
foreach $type (keys %count7) {
print qq|$type - $count7{$type}<BR>|;
}
print qq|Total - $total_count|;

and so on.

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





Quote Reply
Re: Counter for different TYPE In reply to
Thanks alot.