Gossamer Forum
Home : Products : DBMan : Customization :

How to get the count

(Page 1 of 2)
> >
Quote Reply
How to get the count
Hello
How do i get the count from the default.db to
display at my main dbman.
Quote Reply
Re: How to get the count In reply to
You want to display the current number of records on the DBMan home page?

In sub html_home (in the html.pl file), before

&html_print_headers;

add

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

Then, where you want to print out the number of records use

There are currently $count records in the database.

(Or whatever wording you want.)

Just be sure to put this within a "print qq|" statement. If you don't understand what that means, ask and I'll explain it.



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





Quote Reply
Re: How to get the count In reply to
That's a little trickier.

I think I'm going to have to think about it a bit. I'll get back to you. I promise. Smile

You're very welcome!



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





Quote Reply
Re: How to get the count In reply to
Think you JPDeni the code work very nice
and Jp can you help me again, if i want to get each category count from default.db. For
eg. Announce (1), Computer (0).. look like
the Link script.

Thank
Quote Reply
Re: How to get the count In reply to
I'm still working on it. I had a solution to it before, but some folks are having trouble with it, so I have to try a different approach.


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





Quote Reply
Re: How to get the count In reply to
I think I have a solution to this that will work.

I'm assuming you want to count the values in one field -- that is, that one field can have one of several different options and you want to count how many of each option have been selected.

You'll need to know the number of the field that you're using -- not the name -- for this one. For this example, I'm going to count the entries in field 6. The possible entries are "Computer,Freebies, and Tutorials."

Code:
open (DB, "<$db_file_name") or
&cgierr(" unable to open db file: $db_file_name.\nReason: $!");
if ($db_use_flock) { flock(DB, 1); }
@lines = <DB>;
close DB;
$count_total = scalar(@lines);
@options = ('Computer','Freebies','Tutorials');
foreach $line (@lines) {
@data = &split_decode($line);
++$count{$data[6]};
}

When you want to print out the information, you would use

Code:
print "There are a total of $count_total entries in the database:<BR>";
foreach $option (@options) {
unless ($count{$option}) {
$count{$option} = "0";
}
print "$option ($count{$option})<BR>";
}

Naturally, you'll change everything that is in bold print above to match your database.

This is untested, but I think I at least found all my syntax errors. Please let me know what happens.


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





Quote Reply
Re: How to get the count In reply to
JP

Thank you very much, the code work fine, i forgot to put the print thing and the |; what a small stupid thing i make.



Wong

[This message has been edited by wong (edited May 11, 1999).]
Quote Reply
Re: How to get the count In reply to
Not a problem. Smile I think every one of us has made either that same mistake or one like it. I'm glad you got it to work for you.


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





Quote Reply
Re: How to get the count In reply to
Can't get it work, the code did not print out the category entries,

[This message has been edited by wong (edited May 11, 1999).]
Quote Reply
Re: How to get the count In reply to
You have the second part of the code in the wrong place. It's hard to tell you exactly where to put it because I don't know what you have in your html_home page.

Let's say you just have the default wording on your html_home page. It would look something like this:

Code:
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>
<em>Enjoy!</em> and let me <a href="mailto:alex\@gossamer-threads.com">know</a>
if you have any comments!</font></p>
|;

print "There are a total of $count_total entries in the database:<BR>";

foreach $option (@options) {
unless ($count{$option}) {
$count{$option} = "0";
}
print "$option ($count{$option})<BR>";
}

&html_footer;

print qq|</td></tr>
</table>
</center></body></html>
|;

Does this help?

If you still can't get it to work, copy your html.pl file into a directory where I can see it -- one that you would put web pages in -- and rename it so it has a .txt extension. Something like "html_pl.txt". Then let me know where I can see it and I'll let you know where your problem is.

I know the code itself works, because someone else took it from here and it worked fine for him. He said it counted over 600 records in about a second.


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







[This message has been edited by JPDeni (edited May 11, 1999).]
Quote Reply
Re: How to get the count In reply to
Hi,

This MOD is working great for me too.

I just have a question, do you think it's possible to build a list, opposed to typing it manually.

Here is the problem. I want to count all the companies. I have a about 50 companies and instead of typing them all manually, I want it to build the list from the .cfg file. This is also good if I decide to add a company, I won't have to add them in two places.

------------------
JFrost





Quote Reply
Re: How to get the count In reply to
I suppose you could.

Assuming your list is defined as a select field, you could use

@options = split (/\,/, $db_select_fields{'Fieldname'});

instead of

@options = ('Computer','Freebies','Tutorials');

Another thing you could do is just take it from the %count array, except this will not print out any options that are not currently in the database. That is, you'll get no OptionA: (0).

Delete the
@options =
line in the first part of the code.

In the second part of the code, use

Code:
foreach $key (keys %count) {
print "$key: ($count{$key})<BR>";
}

instead of

Code:
foreach $option (@options) {
unless ($count{$option}) {
$count{$option} = "0";
}
print "$option ($count{$option})<BR>";
}


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





Quote Reply
Re: How to get the count In reply to
Hi

If posible to get the email from passwd at the add record field when a user add record.


[This message has been edited by wong (edited May 13, 1999).]
Quote Reply
Re: How to get the count In reply to
Unless you are using my password lookup mod, the user's email address is not in the .pass file.

If you are using my password lookup mod, I would add the following to html_add_record, before

&html_print_headers;

Code:
open (PASS, "<$auth_pw_file") or &cgierr("unable to open: $auth_pw_file.\nReason: $!");
if ($db_use_flock) { flock(PASS, 1); }
@lines = <PASS>;
close PASS;
foreach $line (@lines) {
if ($line =~ /^$db_userid:/) {
($userid, $pw, $view, $add, $del, $mod, $admin, $mailaddress) = split (/:/, $line);
last;
}
}
%rec = &get_defaults;
$rec{'Email'} = $mailaddress

Then change

&html_record_form (&get_defaults);

to

&html_record_form (%rec);



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





Quote Reply
Re: How to get the count In reply to
I don't know's what to said you know everything in Dbman.



Edwin
www.jwtech.net
Quote Reply
Re: How to get the count In reply to
Well, not *every*thing!! Smile Just give me a few days and I'll make some really stupid mistake in response to someone and you'll know how much I *don't* know!!



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





Quote Reply
Re: How to get the count In reply to
When i try it the first time the code work.
But now got a problem when i add the data.
Here the err:
http://www.jwtech.net/...37653&add_form=1 Frown

Quote Reply
Re: How to get the count In reply to
I told you! Smile

I was unable to connect to your server. Possibly a momentary glitch somewhere. But it would be better if you would just tell me what the error is.

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





Quote Reply
Re: How to get the count In reply to
Code work perfectly.
Thank you for your time. Smile
Quote Reply
Re: How to get the count In reply to
Whew! That's a relief!! Smile



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





Quote Reply
Re: How to get the count In reply to
Here the problem, the error was when i try to add data the server ask for "you have chosen to download a file from this location". "using IE". If i remove the code the add data work.
Maybe the code got some problem getting the default.pass for email. Smile
Code:
open (PASS, "<$auth_pw_file")
or &cgierr("unable to open: $auth_pw_file.\nReason: $!");
if ($db_use_flock)
{ flock(PASS, 1); }
@lines = <PASS>;close PASS;
foreach $line (@lines) {
if ($line =~ /^$db_userid:/) {
($userid, $pw, $view, $add, $del,
$mod, $admin, $mailaddress) = split
(/:/, $line); last; }}%rec = &get_defaults;$rec{'Email'} =
$mailaddress


[This message has been edited by wong (edited May 13, 1999).]

[This message has been edited by wong (edited May 13, 1999).]
Quote Reply
Re: How to get the count In reply to
Well, I see that I made a mistake. I forgot to put a ; at the end of a line.

$rec{'Email'} = $mailaddress;

However, that should have made it so the script wouldn't work at all, instead of the problem you're having. (I did log onto your site and try it myself.)

I'm stumped. If adding the ; doesn't do it for you, I have no more ideas. Sorry.


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





Quote Reply
Re: How to get the count In reply to
Hi there...The codes listed by JPDeni are working fine...However, I do have a small challenge to pose to you guys. I have two fields set-up to show number of records for both "Employee Type" and "Status" (meaning Open and Closed Positions).

Codes:
---------------------------------------------
open (DB, "<$db_file_name") or
&cgierr(" unable to open db file: $db_file_name.\nReason: $!");
if ($db_use_flock) { flock(DB, 1); }
@lines = <DB>;
close DB;
$count_total = scalar(@lines);
@options = ('Open');
foreach $line (@lines) {
@data = &split_decode($line);
++$count{$data[3]};
}

open (DB, "<$db_file_name") or
&cgierr(" unable to open db file: $db_file_name.\nReason: $!");
if ($db_use_flock) { flock(DB, 1); }
@lines = <DB>;
close DB;
$count_total = scalar(@lines);
@options2 = ('Administrator', 'Classified_Staff', 'Faculty', 'Professional_Staff', 'Temporary_Staff');
foreach $line (@lines) {
@data = &split_decode($line);
++$count{$data[4]};
}
---------------------------------------------

The problem is that I would like to show the accurate number of OPEN Positions in the Employee Type. See, when I CLOSE a position, the number in the Employee Type shows the TOTAL NUMBER of positions, NOT the total number of OPEN positions.

This is the ideal output that I am looking for:
--------------------------------------------
Administrator: (1)
Classified Staff: (0)
Faculty: (1)
Professional Staff: (0)
Temporary Staff: (0)

Total Number of Available Jobs in our Database: (2)
--------------------------------------------

[ Note: We currently have a total of 14 jobs in our database. Twelve of them are closed and two of them are open. ]

Is there a way to fix the codes so that only the number of OPEN positions shows next to the Employee Type.

Also, is there a way to put the variables in the fields into tables rather than lines. Currently, I have manually coded a bulleted list for the variables. But I would like the variables to be presented in a table. Basically, I would like to create a table of contents similarly to what is done in the Category listing in LINKS2.

Any ideas would be most appreciated.

Thanks.

------------------
Eliot Lee
Founder and Editor
Anthro TECH, L.L.C
http://www.anthrotech.com/
info@anthrotech.com
==========================
Coconino Community College
http://www.coco.cc.az.us/
Web Technology
Coordinator
elee@coco.cc.az.us

[This message has been edited by Eliot (edited May 18, 1999).]
Quote Reply
Re: How to get the count In reply to
Code:
open (DB, "<$db_file_name") or
&cgierr(" unable to open db file: $db_file_name.\nReason: $!");
if ($db_use_flock) { flock(DB, 1); }
@lines = <DB>;
close DB;
@options2 = ('Administrator', 'Classified_Staff', 'Faculty', 'Professional_Staff', 'Temporary_Staff');
foreach $line (@lines) {
++$count_total;
@data = &split_decode($line);
++$count{data[7];
if ($data[7] eq "Open") {
++$count{$data[4]};
}
}
foreach $option (@options2) {
unless ($count{$option}) {
$count{$option} = "0";
}
}

Change the 7 to the field number of your Status field.

You can print it out in any format you want. Use the following variables:

$count{'Administrator'} -- number of open Administrator positions
$count{'Classified_Staff'} -- number of open Classified Staff positions
$count{'Faculty'} -- number of open Faculty positions
$count{'Professional_Staff'} -- number of open Professional Staff positions
$count{'Temporary_Staff'} -- number of open Temporary Staff positions

$count_total -- total number of positions

$count{'Open'} -- total number of open positions
$count{'Closed'} -- total number of closed positions



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





Quote Reply
Re: How to get the count In reply to
JPDeni,

Thanks for the help. However, the codes are not working. I keep getting script errors.

Here are the codes that I replaced in the "Main Menu" sub-routine:

Codes:
--------------------------------------------
open (DB, "<$db_file_name") or
&cgierr(" unable to open db file: $db_file_name.\nReason: $!");
if ($db_use_flock) { flock(DB, 1); }
@lines = <DB>;
close DB;
@options2 = ('Administrator', 'Classified_Staff', 'Faculty', 'Professional_Staff', 'Temporary_Staff');
foreach $line (@lines) {
++$count_total;
@data = &split_decode($line);
++$count{data[3];
if ($data[3] eq "Open") {
++$count{$data[4]};
}
}
--------------------------------------------

Here is the script error messages I receive:

Error Message:
--------------------------------------------
Error Message : Error loading required libraries.
Check that they exist, permissions are set correctly and that they compile.
Reason: Can't use subscript on constant item at C:/InetPub/wwwroot/Intranet/cgi-bin/jobline/html.pl line 647, near "3]"
syntax error at C:/InetPub/wwwroot/Intranet/cgi-bin/jobline/html.pl line 648, near "if"
syntax error at C:/InetPub/wwwroot/Intranet/cgi-bin/jobline/html.pl line 650, near "}"
---------------------------------------------
I tried a variety of things, including:

1) Putting a right bracket after the first
data[3].

2) Moving the first bottom right bracket after the ++$count{data[3]; to close that loop.

3) Switching the number of the field sequence from 3 - 3 - 4 to 3 - 4 - 3.

4) Changed "Open" to 'Open'.

None of these edits worked.

Do you have any idea about how to clean up the codes?

Thanks.

------------------
Eliot Lee
Founder and Editor
Anthro TECH, L.L.C
http://www.anthrotech.com/
info@anthrotech.com
==========================
Coconino Community College
http://www.coco.cc.az.us/
Web Technology
Coordinator
elee@coco.cc.az.us
> >