Gossamer Forum
Home : Products : Links 2.0 : Customization :

Sorting Popular Links, after modification

Quote Reply
Sorting Popular Links, after modification
Hi,
I have slightly changed the nph_build.cgi sub build_rate_page to include the top 10 links on that page aswell, in tables like the other two top tens.

http://www.tobia.com/skins/ to see what I mean with that.

If you take a look at the Top 10 Skins (by Downloads) you will notice that they aren't in the right order, and I don't know how to sort them into decending order.

The following is my sub build_rate_page

Code:
sub build_rate_page {
# --------------------------------------------------------
# Creates a Top 10 ratings page.

my (@values, $id, $votes, $rate, @top_votes, %top_votes, @top_rate, %top_rate, %link_output, $category_clean);
local ($top_rated, $top_votes, $total, $percent, $link_results, $title_linked);

if ($build_ratings_path =~ m,^$build_root_path/(.*)$,) {
&build_dir ($1);
}

$total = 0;

open (LINKS, $db_links_name) or &cgierr ("unable to open links database: $db_links_name. Reason: $!");
LINE: while (<LINKS> ) {
/^#/ and next LINE; # Skip comment Lines.
/^\s*$/ and next LINE; # Skip blank lines.
chomp;
@values = &split_decode ($_);
$id = $values[$db_key_pos];
$votes = $values[$db_votes];
$rate = $values[$db_rating];
next if ($votes < 8);
if (($#top_votes < 8) or ($votes > $top_votes[$#top_votes])) {
push (@{$top_votes{$votes}}, @values);
if ($#top_votes <= 8) {
push (@top_votes, $votes);
@top_votes = sort { $b <=> $a } @top_votes;
}
else {
splice (@{$top_votes{$#top_votes}}, 0, $#db_cols);
$#{$top_votes{$#top_votes}} or delete $top_votes{$#top_votes};
delete $top_votes{$top_votes[$#top_votes]-$id};
$top_votes[$#top_votes] = $votes;
@top_votes = sort { $b <=> $a } @top_votes;
}
}
if (($#top_rate < 8) or ($rate > $top_rate[$#top_rate])) {
push (@{$top_rate{$rate}}, @values);
if ($#top_rate <= 8) {
push (@top_rate, $rate);
@top_rate = sort { $b <=> $a } @top_rate;
}
else {
splice (@{$top_rate{$#top_rate}}, 0, $#db_cols);
$#{$top_rate{$#top_rate}} or delete $top_rate{$#top_rate};
delete $top_rate{$top_rate[$#top_rate]-$id};
$top_rate[$#top_rate] = $rate;
@top_rate = sort { $b <=> $a } @top_rate;
}
}
}
close LINKS;

$total = 0;
CATEGORY: foreach $category (sort keys %cool_links) {
LINK: for ($i = 0; $i < ($#{$cool_links{$category}}+1) / ($#db_cols + 1); $i++) {
$total++;
%tmp = &array_to_hash ($i, @{$cool_links{$category}}, $total);
$link_output{$category} .= &site_popular_html_link (%tmp) . "\n";
}
}
foreach $category (sort keys %cool_links) {
$category_clean = &build_clean ($category);
$link_results .= $link_output{$category};
}

$top_rated = ''; $top_votes = ''; $countRate='0';

foreach (sort { $b <=> $a } @top_votes) {
$countRate++;
$seen{$_}++;
%link = &array_to_hash ($seen{$_} - 1, @{$top_votes{$_}});
$top_votes .= qq~<tr><td>$countRate</td><td align=center width="15%">$link{'Rating'}</td><td align=center width="15%">$link{'Votes'}</td><td width="35%"><a href="$build_jump_url?$db_key=$link{$db_key}" title="Click here to download $link{'Title'}.">$link{'Title'}</a></td><td width="35%"><a href="$build_search_url?query=$link{'Title'}">View a Screen-Shot</a></td></tr>\n~;
}
$countRate='0';
foreach (sort { $b <=> $a } @top_rate) {
$countRate++;
$seen{$_}++;
%link = &array_to_hash ($seen{$_} - 1, @{$top_rate{$_}});
$top_rated .= qq~<tr><td>$countRate</td><td align=center width="15%">$link{'Rating'}</td><td align=center width="15%">$link{'Votes'}</td><td width="35%"><a href="$build_jump_url?$db_key=$link{$db_key}" title="Click here to download $link{'Title'}.">$link{'Title'}</a></td><td width="35%"><a href="$build_search_url?query=$link{'Title'}">View a Screen-Shot</a></td></tr>\n~;
}
$title_linked = &build_linked_title ("Cool");
open (RATE, ">$build_ratings_path/$build_index") or &cgierr ("unable to open top rated page: $build_ratings_path/$build_index. Reason: $!");
print "\tVote Range: $top_votes[0] .. $top_votes[$#top_votes]\n";
print "\tRate Range: $top_rate[0] .. $top_rate[$#top_rate]\n";
print RATE &site_html_ratings;
close RATE;
}

The following is the site_popular_html_link sub referenced in the above one.

Code:
sub site_popular_html_link {
my (%rec) = @_;
$output = qq~<tr><td>$total</td><td width="15%" align="center">$rec{'Hits'}</td><td width="50%"><a href="$build_jump_url?$db_key=$rec{$db_key}" title="Click here to download $rec{'Title'}.">$rec{'Title'}</a></td><td width="35%"><a href="$build_search_url?query=$rec{'Title'}">View a Screen-Shot</a></td></tr>\n\n~;

return $output;
}

Basically, how can I sort into order the Top 10 download that are churned out by this?