Gossamer Forum
Home : Products : Gossamer Links : Development, Plugins and Globals :

Totals Globals - A collection

Quote Reply
Totals Globals - A collection
Here are a few Totals globals that I have gathered together from these forums. May be useful all in one place.

Code:
sub { # show total number of hits (without commas - 123456)
my ($total) = $DB->table('Links')->select(['SUM(Hits)'])->fetchrow_array;
return $total;
}

------------------------------------------

sub { # show total number of hits (with commas - 123,456)
local $_ = $DB->table('Links')->select(['SUM(Hits)'])->fetchrow_array;
return 0 unless defined $_;
1 while s/^([-+]?\d+)(\d{3})/$1,$2/;
return $_;
}

------------------------------------------

sub { # show total number of votes (without commas - 123456)
my ($total) = $DB->table('Links')->select(['SUM(Votes)'])->fetchrow_array;
return $total;
}

------------------------------------------

sub { # show total number of votes (with commas - 123,456)
local $_ = $DB->table('Links')->select(['SUM(Votes)'])->fetchrow_array;
return 0 unless defined $_;
1 while s/^([-+]?\d+)(\d{3})/$1,$2/;
return $_;
}

------------------------------------------

sub { # show grand total (with commas - 123,456)
my $tags = shift;
local $_ = $tags->{grand_total};
return 0 unless defined $_;
1 while s/^([-+]?\d+)(\d{3})/$1,$2/;
return $_;
}

TAG: Replace <%grand_total%> with <%grand_total_formatted%>

------------------------------------------

sub { # show total number of categories (without commas - 123456)
my $cat_db = $DB->table ('Category');
my $count = $cat_db->total;
return $count;
}

------------------------------------------

sub { # show total number of reviews (without commas - 123456)
my $cat_db = $DB->table ('Reviews');
my $count = $cat_db->total;
return $count;
}

------------------------------------------

sub { # show total number of new links (without commas - 123456)
$DB->table('Links')->count({isNew => 'Yes'})
}

------------------------------------------

sub { # show total number of updated links (without commas - 123456)
$DB->table('Links')->count({isChanged => 'Yes'})
}

------------------------------------------

If you have any more please add them.

Last edited by:

MJB: Mar 12, 2006, 5:16 AM
Quote Reply
Re: [MJB] Totals Globals - A collection In reply to
Hi,

Nice work :)

I'm working on the UltraWidgets package, again, and once the main program is released, I'll have a collection of these widgets released in a series of modules that can be called from the plugins via functions, rather than globals.

I have a fairly huge "Totals" routine, that depending on what you call it with, will return pretty much almost any sort of total available from the database.

If you go through and find any other routines, or cool globals, post them in messages like this, and I'll try to make sure they, or similar functionality is available in a standard function format.

There are two versions (at least) of this program, the first is the free release of the basic access tools, like the totals, new, etc.

The "pro" version will have more advanced functionality, and access functions, and will be part of our standard release, but will all fit together to provide some standard calling conventions, and maintennance/bug fixes.


PUGDOG� Enterprises, Inc.

The best way to contact me is to NOT use Email.
Please leave a PM here.
Quote Reply
Re: [MJB] Totals Globals - A collection In reply to
Code:
sub { # show total number of registered users (without commas - 123456)
$DB->table('Users')->count({Status => 'Registered'})
}
Quote Reply
Re: [MJB] Totals Globals - A collection In reply to
This global will show the LinkOwner total:

Code:
sub {
my ($total) = $DB->table('Links')->select(['COUNT(DISTINCT(LinkOwner))'])->fetchrow_array;
return $total;
}

*Maybe there is a clean way using count instead select... but this is working!

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Blondies can have brains (sometimes...)
Quote Reply
Re: [SaraBem] Totals Globals - A collection In reply to
Hi,

Ah, I see you worked out the prob in the post I just replied to <G> The way you have it is (AFAIK) the only real way to do the count. Just made a couple of changes (well, more just pasted what I just posted in your other thread =))

Code:
sub {
return $DB->table('Links')->select ( ['COUNT(DISTINCT(LinkOwner))'] )->fetchrow || 0;
}

I'm gonna have a quick scout of some of the globals I've written a while back, could prob add some here =)

Hope that helps.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Totals Globals - A collection In reply to
This global lets you grab the content of an external page:

my $strip_newlines = 1; - set this to "0" if you don't want to strip out the newlines.

Code:
sub {

my $strip_newlines = 1;

my $url = $_[0];
use LWP::Simple;
my $got = get($url);

if ($strip_newlines) { $got =~ s/\n/ /sig; }

$got ? return \$got : return qq|$url not found online!|;

}

Simply call with: <%global_name("http://www.site.com/page.html")%>

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Totals Globals - A collection In reply to
This global lets you grab the "name" of the domain, and also the TLD (extension)

Code:
sub {
my $url = $_[0];
my $return_full = $_[1];
my ($tld,$domain_name);
if ($url=~ m/www\.(.*?)(\.com|\.co\.uk|\.net|\.org?)/i){
$domain_name = $1;
$tld = $2;
}
$return_full ? return $domain_name . $tld : return $domain_name;
}

Call with: <%global_name($URL,1)%>

This would process <%URL%> - If you have another field, just use $Field_Name

Set the second paramater to "0" to just return the name, or 1 to return the domain and tld.

Enjoy :)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [MJB] Totals Globals - A collection In reply to
Code:
sub { # show total number of new links (without commas - 123456)
$DB->table('Links')->count({isNew => 'Yes'})
}

May be safer to add in isValidated into these conditions?

Code:
sub { # show total number of new links (without commas - 123456)
$DB->table('Links')->count({isNew => 'Yes', isValidated => 'Yes' });
}

Just in case there are some odd listings that may throw the count off :)

Just another random idea, which may add some more flexability to those globals - something like:

$field
$DB->table($_[0])->count({$field => 'Yes',isValidated => 'Yes'})

Hope that helps

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Totals Globals - A collection In reply to
This is a newer version:

<%global_name('table to look at','field to count listings','format or not')%>
<%global_name('Links|Reviews|Category','isNew|isChanged|isPopular','1|0')%>

Example:
Code:
<%global_name('Links','isNew',1)%>

Code:
sub {
my $table = $_[0] || return qq|Error, no table defined|;
my $field = $_[1] || return qq|Error, no field defined - use either isNew, isChanged or isPopular|;
my $do_commify = $_[2]; # pass in either 1 or 0 (true or false)
my $cnt = $DB->table($table)->count( { $field => 'Yes', isValidated => 'Yes' });
if ($do_commify) {
local $_ = $cnt;
return 0 unless defined $_;
1 while s/^([-+]?\d+)(\d{3})/$1,$2/;
return $_ || 0;
} else {
return $cnt || 0;
}
}

All seems to work fine in my tests, and stops the need for multiple globals for the same kind of tasks :)


Also, I can't see the "commify" global (although you are using the code :)) - something like this should be fine:

..global name - "commify"

Code:
sub {
# show grand total (with commas - 123,456)
local $_ = $_[0];
return 0 unless defined $_;
1 while s/^([-+]?\d+)(\d{3})/$1,$2/;
return $_;
}

..call with: <%commify($field_to_process)%>

..example usage:

Code:
<%global_name($grand_total)%>
<%global_name($Hits)%>
<%global_name($Votes)%>

...or any other numerical field that you want to format :)

Hope that helps.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Totals Globals - A collection In reply to
This global seems to work well for grabbing the URL/path to an image that has been uploaded using the FILE type;

<%global_name($ID,'FieldName','TableName')%>

Code:
sub {
# -------------------------------------------------------------------
# Call with <%global_name($ID,'FieldName','TableName')%>

my ($ID,$field,$table) = @_;

$table ||= 'Links';

# make sure a fieldname and ID are provided...
if (!$ID || !$field) { return "You need to define the ID and fieldname."; }

# get the actual path to where the file is/will be saved...
my $schema = $DB->table($table)->cols;
my $path = $schema->{$field}->{'file_save_url'};
$path =~ s,/$,,; # get rid of trailing / at end of $path

my $image_details = $DB->table($table."_Files")->select( { ForeignColName => $field, ForeignColKey => $ID } )->fetchrow_hashref;

my $id = $image_details->{ID};
my $filename = $image_details->{File_Name};
my $file_url = $image_details->{File_URL}; # this is only the URL/folder

my @cut = split //, $id;
my $folderid = $cut[$#cut];

my $url = "$file_url/$folderid/$id-$filename";


$url =~ s|([^\/]+)$|GT::CGI->escape( $1 )|e;

return $url;

}

:)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [MJB] In reply to
Oops - just realised that this thread was just for "totals" (count) globals - sorry =) I'll still leave my posts here though, just in case they are of any use to anyone :)

Newline --> <br/> conversion global.

<%global_name($value)%>
Code:
sub {
my $tmp = $_[0];
$tmp =~ s|\n|<br/>|sg;
return $tmp;
}


Format figure (into price);

<%global_name($value,"NUMBER_OF_DECIMAL_PLACES")%>
Code:
sub {
my $tmp = sprintf("%.$_[1]f", $_[0]);
return $tmp;
}

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] In reply to
I've started replacing globals like <%grand_total%> with your totals from UltraGlobals because the standard global doesn't work on all pages (my totals are in the left sidebar).

I could commify the basic globals quite easily but what I'd like to know is, is it possible to commify something like:

<%Plugins::ULTRAGlobals::Get_Totals('Links','all')%>
<%Plugins::ULTRAGlobals::Get_Totals('Gallery_Files','all')%>
<%Plugins::ULTRAGlobals::Get_Totals('Category','all')%>
etc.

?
Quote Reply
Re: [MJB] In reply to
Hi,

Pretty simple :)

Code:
<%set total_links_all = Plugins::ULTRAGlobals::Get_Totals('Links','all')%>
<%Plugins::ULTRAGlobals::Commify($total_links_all)%>

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] In reply to
Unbelievable!

I wish I could get to grips with how this thing ticks then I wouldn't feel so bad about leaching your advice. I hope GT appreciates what you do around here because I sure as hell know that we do. Wink
Quote Reply
Re: [MJB] In reply to
NP ;)

Hint hint GT - another free server would be nice LOL Laugh

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!