Gossamer Forum
Home : Products : Links 2.0 : Customization :

Global Variable Help

Quote Reply
Global Variable Help
I am trying to add a global variable in my links setup. I have a field in my database that shows the number of images in each content set (each link). I want to add a global tag (<%imagecount%>) that shows the total number of images in the sets. I figured the best place to have the count totalled would be in nph-build.cgi in sub build_stats, so I made the changes below in red and added the tag to site_html_templates.pl in the globals section. It doesn't work, nothing is shown. I don't get the Unknown Tag error, just empty, as if the variable is empty. If I take the open DB portion below, and add it to the sub build_home, it works perfectly, but not as a global variable, just on the home page, but this adds overhead to nph-build.cgi, because it is opening and going through the database again, after sub build_stats has already done it.

I am not a Perl wiz, but I have managed to figure my way through some modifications of my own, but this has got me stumped. Any ideas?

Thanks in advance,

Charlie

Quote:


THE CHANGES I MADE ARE IN RED:

sub build_stats {
# --------------------------------------------------------
# This routine does a lot of the messy work. It builds globally accessible
# arrays of new_links and cool_links. It finds out how many links are in each
# category, and whether a category contains a new/modified link.

my (@values, $category, $cat, @alt_categories, @categorylist, $depth, $i, $cat, %unique_categories, $imagetally, $images);
my $staggered_mode = shift || undef;

open (DB, "<$db_file_name") or &cgierr("unable to open database: $db_file_name. Reason: $!");
LINE: while (<DB>) {
/^#/ and next LINE; # Skip comment Lines.
/^\s*$/ and next LINE; # Skip blank lines.
chomp;
@values = &split_decode ($_);
$category = $values[$db_category];
$images = $values[$db_count]; #(Note: $db_count is defined in links.def)
$imagetally = $imagetally + $images;


# Add the link to the list of links.
push (@{$links{$category}}, @values) if (!$staggered_mode);
$grand_total++;

# Add the link to the alternate categories as well.
if (defined $db_alt) {
@alt_categories = split(/\Q$db_delim\E/, $values[$db_alt]);
foreach (@alt_categories) {
push (@{$links{$_}}, @values);
}
}

# Add the link to the list of new links if it is new.
push (@{$new_links{$category}}, @values) if ($values[$db_isnew] eq "Yes");

# This adds one to the total of each category above the current category.
# We have to caluclate the affect of the link on each alt category as well as the main.
%unique_categories = "";
foreach $cat ($category, @alt_categories) {

# Calculate the stats: the number of links and the newest link.
@categorylist = split (/\//, $cat);
$depth = $#categorylist;

# This adds one to the total of each category above the current category,
# and also marks any above categories new, if this link is new.
for $i (0 .. $depth) {
if (! defined ($unique_categories{$cat})) {
$unique_categories{$cat} += 1;
$stats{$cat}[0]++;
}

if ((!$stats{$cat}[1]) || &compare_dates($values[$db_modified], $stats{$cat}[1])) {
$stats{$cat}[1] = $values[$db_modified];
}
pop (@categorylist);
$cat = join("/", @categorylist);
}
}
}
close DB;

# Now we have to sort the links and categories..
if (!$staggered_mode) {
foreach $link ( keys %links ) {
@{$links{$link}} = &build_sorthit (@{$links{$link}});
}
foreach $cat ( keys %subcategories ) {
@{$subcategories{$cat}} = sort @{$subcategories{$cat}};
}
}
$grand_total ||= 0;
$imagecount = $imagetally;
}


Quote:


site_html_templates.pl, %globals section added:

imagecount => $imagecount
Quote Reply
Re: [cvance] Global Variable Help In reply to
In doing more trial and error, I find that the tag will work if I put the "imagecount => $imagecount," in each variable array in each individual sub (site_html_home, site_html_category, etc) in site_html_templates.pl. If I put it in the global array, and take it out of each sub, it doesn't work anywhere. I notice that "grand_total => $grand_total" is in each sub too, and not in globals. I may be wrong (I probably am) but if it works in the individual subs, shouldn't it work in the global array instead of each sub?

If anyone has any ideas, please let me know, for my own future reference.

Charlie
Quote Reply
Re: [cvance] Global Variable Help In reply to
well, I am no expert but perl has local and global variables. when you created the variable, you created them local to the sub build_stats routine by using the MY function

Code:
my (@values, $category, $cat, @alt_categories, @categorylist, $depth, $i, $cat,
%unique_categories, $imagetally, $images);


Gene
"The older I get, the more I admire competence, just simple competence in any field from adultery to zoology."