Gossamer Forum
Home : Products : Links 2.0 : Customization :

Toplist at category pages ???

Quote Reply
Toplist at category pages ???
I want to put a top 3 links list at the top of mu category pages, sorted by hits. The links in the category must be sorted by aplha.

I followed all the instructions in:

http://gossamer-threads.com/perl/forum/showflat.pl?Cat=&Board=L2Cust&Number=39692&page=&view=&sb=&part=1&vc=1

I managed to put the top 3 in my category pages, however I cannot get the sort order right.

As karl said in the forum topic above:

" i use your mod and everything works fine, but the "ranking" is in wrong order.
There is no System in it, like the one with the most hits on top.

Also most of the entrys in the category are not shown.

As example:

1. Some Link (9 Hits)
2. Some Link (320 Hits)
3. Some Link (1 Hits)
4. Some Link (1 Hits)
5. Some Link (1 Hits)

But there are also entrys with 123 or 212 Hits, but there are not in the toplist. "

Eliot replied with:

"Yea...In Jerry's earlier post, he mentioned that you need to edit the sub build_sorthit routine in the db_utils.pl file.

Go to the following URL for information on what you need to edit the sub build sorthit routine:

http://www.widgetz.com/review/instructions.cgi?4"


Well the link above is dead!!!! And I cannot read a sololution elsewhere in this forum.

An example of what I want is shown at:
http://www.fussballsuche.de/de/Global/EM_und_WM/


Please Help me, because I spend too many hours already ...


Axel









Quote Reply
Re: Toplist at category pages ??? In reply to
Ok you don't need to edit sub build_sorthit at all as that will messup the rest of your sorting.

Try:

Code:
my (@hits,$links);
Code:
open(DB,"$db_links_name") || &cgierr("Couldn't open $db_links_name : $!");
LINE: while(<DB>) {
(/^\s*$/) and next LINE;
(/^#/) and next LINE;
@values = &split_decode($_);
push @hits, "$values[$db_hits]-$values[$db_id]";
}
close(DB);
Code:
@hits = sort { $b <=> $a } @hits;
if ($#hits > -1) {
foreach (@hits) {
split /-/;
my (%rec) = &get_record($_[1]);
$links .= &site_html_link(%rec);
$hits[2] and last;
}
}
Then create a new tag list => $links, and put <%list%> in your template.

That is a really pants way of doing it but it should work. If not let me know and I'll go and test it out.


Installs:http://wiredon.net/gt
FAQ:http://www.perlmad.com

Quote Reply
Re: Toplist at category pages ??? In reply to
 
This is the routine I followed:

1) Add the following sub-routine in your nph-build.cgi file. This can be placed at the end of the script:


code:
--------------------------------------------------------------------------------

sub build_top_x {my ($category) = @_;my (@values, $top_cat, @topx, $topxhtml, $count); $db_sort_links = "14"; @topx = &build_sorthit (@{$links{$category}}); $db_sort_links = "1"; $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><b>$count.</b></font> </td><td valign="top"><$font>~; if ($tmp{'isDetailed'} eq "Yes") { $topxhtml .= qq~<a href="$build_detail_url/$tmp{$db_key}$build_extension"><b>$tmp{'Title'}</b></a>~; } else { $topxhtml .= qq~<a href="$build_jump_url?$db_key=$tmp{$db_key}"><b>$tmp{'Title'}</b></a>~; } $topxhtml .= qq~</font>
<$font_small>($tmp{'Hits'} Downloads)</font></td></tr>\n~; } $topxhtml .= qq~</table>~; return $topxhtml;}

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

2) Then add the following codes:


code:
--------------------------------------------------------------------------------

$top = &build_top_x ($cat);

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

AFTER the following codes in the sub build_category_pages routine in the nph-build.cgi file:


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);

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

3) Add the following codes:


code:
--------------------------------------------------------------------------------

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

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

AFTER the following codes:


code:
--------------------------------------------------------------------------------

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

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

in the sub build_staggered routine in the nph-build.cgi file.

4) Add the following codes:


code:
--------------------------------------------------------------------------------

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

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

AFTER the following codes in the sub build_all routine in nph-build.cgi:


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) Add the following codes to the sub site_html_category routine in the %load_template area:


code:
--------------------------------------------------------------------------------

top => $top,

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

6) Then in your category.html template file, add the following tag to the spot in the page where you want the Top Sites located:


code:
--------------------------------------------------------------------------------

<%top%>

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

Hope this makes it clear.



Tell me WHERE I should put the code you gave me:

my (@hits,$links);

open(DB,"$db_links_name") || &cgierr("Couldn't open $db_links_name : $!");LINE: while(<DB>) { (/^\s*$/) and next LINE; (/^#/) and next LINE; @values = &split_decode($_); push @hits, "$values[$db_hits]-$values[$db_id]"; } close(DB);

@hits = sort { $b <=> $a } @hits; if ($#hits > -1) { foreach (@hits) { split /-/; my (%rec) = &get_record($_[1]); $links .= &site_html_link(%rec); $hits[2] and last; } }


And must I add the <%list%> to the <%top%>


Help me, I am not a veteran with links 2???


This is how my nph-build now looks like after adjusting some things:

sub build_top_x {
my ($category) = @_;
my (@values, $top_cat, @topx, $topxhtml, $count);
$db_sort_links = "14";
@topx = &build_sorthit (@{$links{$category}});
$db_sort_links = "1";
$topxhtml = qq~<center><table width="93%" cellpadding="0" cellspacing="0" border="0">\n~;
my $display = 3;
(($#topx+1) / ($#db_cols+1) >= 3) 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 nowrap width="3%"><small>$count.</small> </td><td nowrap width="87%"><small>~;
if ($tmp{'isDetailed'} eq "Yes") { $topxhtml .= qq~<a href="$build_detail_url/$tmp{$db_key}$build_extension"><b>$tmp{'Title'}</b></a>~; }
else { $topxhtml .= qq~<a href="$build_jump_url?$db_key=$tmp{$db_key}"><b>$tmp{'Title'}</b></a>~; }
$topxhtml .= qq~</small></td><td nowrap width="10%" align="right"><small> ($tmp{'Hits'} Hits)</small></td></tr>\n~;
}
$topxhtml .= qq~</table></center>~;
return $topxhtml;
}



Quote Reply
Re: Toplist at category pages ??? In reply to
Hmm forget my code - I misunderstood. The code I provided will show the top 3 links in the entire database, not per category.

I'd need to think this over before posting the code. But there is already code available that you can use so I don't really need to write any more.

Installs:http://wiredon.net/gt
FAQ:http://www.perlmad.com

Quote Reply
Re: Toplist at category pages ??? In reply to
Well if there is a code available tell me where I can find it? Again, I do not have a problem with creating a Top 3 list. I have problems with sorting this top 3 list by the number of hits. The category links itself I want to sort in alphabetical order.

I should find things in:

http://www.widgetz.com/review/instructions.cgi?4"

But this link is dead.

I cannot be more specific. Understand I am a rookie in this matter. So if there is a way, lead me to it ...

Axel

Quote Reply
Re: Toplist at category pages ??? In reply to
To sort the main category links then yes you need to edit sub build_sorthit but you said you wanted to sort the top 3 by hits - in that case you'd just use sort {}

Installs:http://wiredon.net/gt
FAQ:http://www.perlmad.com

Quote Reply
Re: Toplist at category pages ??? In reply to
Paul,

Please can you be more specific in how I should edit the db_util.pl:

You said:

"...in that case you'd just use sort {}"

My sub build_sorthit looks like this:

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);

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_ispop + ($i * ($#db_cols+1))] eq "Yes") and ($iscool{$i} = 1);
}
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;
}


Maybe you can change the above code in what it must be?

Axel

Quote Reply
Re: Toplist at category pages ??? In reply to
Can't Anyone help me ...

Quote Reply
Re: Toplist at category pages ??? In reply to
Alex (2),
in the German forum someone postet a solution and rewrite Jerry's sorting code...
http://www.nicky.net/...2&highlight=top5
Hope that helps, but it's only in German.
If you want to get an english translation, mail me.
Or use the following: http://www.topsuche.net/top5.htm - only German too.

DelPierro
http://www.simball.de/links_mods/
Quote Reply
Re: Toplist at category pages ??? In reply to
Thanks a lot. I will try it soon, but if you could translate it into english it would be a big big help!

I like your site and used the 5 star mod earlier. Thats when I also saw the site 'fussbalsuche' which gave me a lot of new idea's, like the top 3 on category pages.

I hope this mod will work for me now.

Quote Reply
Re: Toplist at category pages ??? In reply to
still it does not sort right???

1. De Nederlandse Orde van Belastingadviseurs (1 Hits)
2. Belastingplan 2001 (12 Hits)
3. Tax Forum (2 Hits)
4. Belastingdienst (7 Hits)

looks like it is sorting by the first numbers 1, 12, 2, 7

what do we do?

Quote Reply
Re: Toplist at category pages ??? In reply to
Hello Alex (2),
I will try to translate it tomorrow, cause it's late here in germany. You're from the netherlands and cannot read the german installation guide?! Wink
I hope that the english translation has been finished tomorrow.
The users in the German forum use the mod without any problems, I think.

P.S. You know the english section of "Fussballsuche"? Try this: http://www.fussballsuche.com

DelPierro
http://www.simball.de/links_mods/
Quote Reply
Re: Toplist at category pages ??? In reply to
I managed to get things right now!!! Thanks a lot for your help DelPiero, the german forum and your top5.htm page got me through it.

Of course I can read German, but when I read your post I thought the codes would be different too (in german). Not thinking it is an universal language (perl). Stupid me. I guess I was anxious to get results.

I hope I did not bother you making an english page, however there will be many people how will benefit from your codes. Put it at your website!

-----------

Maybe you can help me with an other problem I have:

I also want to put a Top ten list at my homepage from the whole database. I managed to do this reading:
The result looks like this:

1. JNK Financieel Planbureau BV
2. Belastingplan 2001
3. Belastingdienst
4. Wijzerlijn
5. Tax Forum
6. De Nederlandse Orde van Belastingadviseurs
7. test

I want to add the number of hits, the average rating and votes to each line and sort it by hits. It is already sorted by hits.

Can you or anyone else help me with this ?????


Axel





Quote Reply
Re: [Axel2] Toplist at category pages ??? In reply to
Hey Axel2, did you manage to get everything together for the Top 3 on each cat?? I am not able to read german, I would thus really appreciate it if you (or anyone else) could post a summary of the needed steps.

Thanks in advance,
Aymeric.
Quote Reply
Re: [Aymeric] Toplist at category pages ??? In reply to
Actually, it's pretty straightforward and works great. Here it is, a good Top 5 per hits in each category, in english:

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;
}


This was taken from Alex2's link @ http://www.topsuche.net/.

Cheers,
Aymeric.









Quote Reply
Re: [Aymeric] Toplist at category pages ??? In reply to
Hi,

I tryed to put the hits per category mod in my scripts.
Everything looks good, when I build pages it says it builds de topsites in categories etc.
But when I look at my category pages, no top list is showing (i have added <%top%> also in my category.html)

So I checked and double checked and everything should work except for these parts ;

You say this :

Right after that:

Code:
$t1 = time();
print "** Creating What's Cool Page. . .\n";
&build_rate_page;
print "** Done (", time - $t1, " s)!\n\n";but it seems that you mix two routines (whats cool and top rated ?
Anyway, while building it says thats its building the top pages in category so
it should work anyway although I don't see pages being build like other pages.Can you help me out with this one?
Ravage
Quote Reply
Re: [ravage] Toplist at category pages ??? In reply to
I got the same sort of problem in my own links implementation when I applied these changes. I think the specificity of *where* some of the changes go is making the difference.... Perhaps it might do to have a more specific set of instructions on some of these changes?

Jon