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

Top 10 by Hits at every Category

Quote Reply
Top 10 by Hits at every Category
Hello all,

How can i use the mod for Links 2.0 as below at Links SQL ? Please help.

Top 5 Hits in each category Mod for Links 2.0

1) In nph-build.cgi, before the ending 1; add the following:


Code

sub build_top_x {
my ($category) = @_;
my (@values, $top_cat, @topx, $topxhtml, $count);
$original = $db_sort_links;
$db_sort_links = $db_hits;
@topx = &build_sorthit (@{$links{$category}});
$db_sort_links = $original;
$topxhtml = qq~<table cellpadding="0" cellspacing="0" border="0">\n~;
my $display = 5;
(($#topx+1) / ($#db_cols+1) >= 5) or $display = ($#topx+1)/($#db_cols+1);
for ($i = 0; $i < $display; $i++) {
%tmp = &array_to_hash ($i, @topx);
$count = $i+1;
$topxhtml .= qq~<tr><td valign="top"><font size="1">$count.</font></td><td valign="top">~;
if ($tmp{'isDetailed'} eq "Yes") { $topxhtml .= qq~<a href="$build_detail_url/$tmp{$db_key}$build_extension"><font size="1">$tmp{'Title'}</font></a>~; }
else { $topxhtml .= qq~<a href="$build_jump_url?$db_key=$tmp{$db_key}" target="_blank"><font size="1">$tmp{'Title'}</font></a>~; }
$topxhtml .= qq~<font size="1">($tmp{'Hits'})</font></td></tr>\n~;
}
$topxhtml .= qq~</table>~;
return $topxhtml;
}



2) Still in nph-build.cgi, in the sub build_category_pages Routine, add:


Code

$title_linked = &build_linked_title ($cat);
$title = &build_unlinked_title ($cat);
$total = ($#{$links{$cat}} + 1) / ($#db_cols + 1);
$category_name = $cat;
$category_name_escaped = &urlencode ($cat);
$category_clean = &build_clean ($cat);
$top = &build_top_x ($cat);



3) then, add the following in the sub build_staggered Routine in nph-build.cgi:


Code

$t1 = time();
print "** Creating Top Sites in Categories. . .\n";
&build_top_x;
print "** Done (", time - $t1, " s)!\n\n";


Right after that:


Code

$t1 = time();
print "** Creating What's Cool Page. . .\n";
&build_rate_page;
print "** Done (", time - $t1, " s)!\n\n";



4) And also add this in the sub build_all Routine in nph-build.cgi:


Code

# Create Top Sites in Categories
print "Building Top Sites in Categories . . .\n";
&build_top_x;
print "Done\n\n";



Right after this:


Code

# Create What's Cool Page
$use_html ?
print "Building <A HREF=\"$build_ratings_url/$build_index\">What's Cool</A> Page . . .\n" :
print "Building Top Rated . . .\n";
&build_rate_page;
print "Done\n\n";



5) In the sub site_html_category Routine in site_html_templates.pl, add:


Code

meta_keywords => $meta_keywords,
top => $top,



6) Add this in category.html for printing:


Code

<%top%>



7) Finally, replace the sub build_sorthit Routine in db_utils.pl with this (or change it as needed):


Code

sub build_sorthit {
# --------------------------------------------------------
# This function sorts a list of links. It has been modified to sort
# new links first, then cool links, then the rest alphabetically. By modifying
# the sort function below, you can sort the links however you like (by date,
# or random, etc.).

my (@unsorted) = @_;
my ($num) = ($#unsorted+1) / ($#db_cols+1);
my (%sortby, %isnew, %iscool, $hit, $i, @sorted, $column, $type);

foreach $column (@db_cols) {
if ($db_sort_links == $db_def{$column}[0]) {
$type = $db_def{$column}[1];
last;
}
}
for ($i = 0; $i < $num; $i++) {
$sortby{$i} = $unsorted[$db_sort_links + ($i * ($#db_cols+1))];
($unsorted[$db_isnew + ($i * ($#db_cols+1))] eq "Yes") and ($isnew{$i} = 1);
($unsorted[$db_ratings + ($i * ($#db_cols+1))] eq "Yes") and ($iscool{$i} = 1);
}
if ($type eq "date") {
foreach $hit (sort {

&date_to_unix ($sortby{$b}) <=> &date_to_unix ($sortby{$a});
} (keys %sortby)) {
$first = ($hit * $#db_cols) + $hit;
$last = ($hit * $#db_cols) + $#db_cols + $hit;
push (@sorted, @unsorted[$first .. $last]);
}
}
elsif ($type eq "numer") {
foreach $hit (sort {
$sortby{$b} <=> $sortby{$a};
} (keys %sortby)) {
$first = ($hit * $#db_cols) + $hit;
$last = ($hit * $#db_cols) + $#db_cols + $hit;
push (@sorted, @unsorted[$first .. $last]);
}
}
else {
foreach $hit (sort {
($isnew{$b} and !$isnew{$a}) and return 1;
($isnew{$a} and !$isnew{$b}) and return -1;
($iscool{$b} and !$iscool{$a}) and return 1;
($iscool{$a} and !$iscool{$b}) and return -1;
($isnew{$a} and $isnew{$b}) and return lc($sortby{$a}) cmp lc($sortby{$b});
($iscool{$a} and $iscool{$b}) and return lc($sortby{$a}) cmp lc($sortby{$b});
return lc($sortby{$a}) cmp lc($sortby{$b});
} (keys %sortby)) {
$first = ($hit * $#db_cols) + $hit;
$last = ($hit * $#db_cols) + $#db_cols + $hit;
push (@sorted, @unsorted[$first .. $last]);
}
}
return @sorted;
}