Gossamer Forum
Home : Products : Links 2.0 : Customization :

RE: Glennu - randlinks.cgi

(Page 1 of 2)
> >
Quote Reply
RE: Glennu - randlinks.cgi
Hi Glennu....Have a problem..well not a problem..wish to change your randlinks around a bit...have been trying..with some amazing results *grins*..This is what I wish to do..

Like to use randlinks for featured links for each category..ok...but umm below is a example of the structure of my links..

top category Blues
subcategories A through to Z
top catergory POP
subcategories A through to Z
So forth and so Forth.

OK...The top categories have no Links (this is where I wish to put the featured links)....
Now what I wish to do is for the randlinks to pull 5 links out of the subcategorys under each topcategory...so I can then use them as featured links in each top category...but yep is always a but....I would like the scipt to print them out so I would have say blues.txt or html whatever pop.txt soforth with the 5 rand links for that topcategory which have been pulled out of the subcategories underneath...so then i can place them in the topcategory with the include statement....
Think its a new mod all together ha!
As I said have been trying to do myself but alais not much success. So if you could wrap your brains around it and come up with a answer I will shout you a virtual drink or two *grins*.

Admittedlamb.

PS I can't use SSI #exe cgi CMD on my server as been disabled.

Quote Reply
Re: RE: Glennu - randlinks.cgi In reply to
Yeah wouldn't be too hard to do. You could achieve it by adding a sub in nph-build.cgi. Just by using the code from the cgi script and adding some code to open a file for each cateogry and write the results onto it.

Glenn

Links 2 Mods Site:
http://cgi-resource.co.uk/pages/links2mods.shtml
Quote Reply
Re: RE: Glennu - randlinks.cgi In reply to
Here's a suggestion on how to go about doing it: Should work as is but might need a few alterations ie. I haven't tested it. It's set up to write 5 random links to random_links.txt could be altered to do different ones for each category. Should be able to do this by using what ever method you used before to pull them out foreach category.


Make a directory called Random and make it just off of the main links directory.


In links.cfg add:

# PATH and URL of random files.
$build_random_path = "$build_root_path/Random";
$build_random_url = "$build_root_url/Random";
$num_random_links = "5";
$random_page = "random_links.txt";


In nph-build...


In sub build staggered find:


print "Updating ratings .. \n";
$t1 = time();
&build_update_ratings;
print "Done (", time - $t1, " s)\n\n";

Beneath it add:

print "Updating random links .. \n";
$t1 = time();
&build_update_random_links;
print "Done (", time - $t1, " s)\n\n";


in sub build_all find:

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

Beneath it add:


# Create Random links pages
$use_html ?
print "Building <A HREF=\"$build_random_url/$build_index\">Random Links</A> . . .\n" :
print "Building Top Rated . . .\n";
&build_random_links;
print "Done\n\n";

Add the following sub:


sub build_random_links {
# --------------------------------------------------------

# Pull out the total number of links.
open (COUNT, "<$db_hits_path/index.count") or &error ("unable to open index count file: $db_hits_path/index.count. Reason: $!");
$count = int <COUNT>;
close COUNT;


for ($i = 0; $i < $num_random_links; $i++) {
# Get the random line from the url lookup database.

$find = 0; $rand = int (rand ($count + 0.5));
($rand == $count) and ($rand--);
open (URL, "<$db_links_name") or &error ("unable to open links database: $db_url_name. Reason: $!");
while (<URL>) {

(/^#/) and next LINE;
(/^\s*$/) and next LINE;
chomp;
@data = &split_decode($_);
$find++ == $rand or next;
%rec = &array_to_hash (0, @data);
last;
}
close URL;

$link_output .= &site_html_link (%rec) . "\n";

}
open (RANDOM, ">$build_random_path/$random_page") or &cgierr ("unable to open random page: $build_random_path/$random_page. Reason: $!");
print RANDOM "$link_output";
close RANDOM;

}











Glenn

Links 2 Mods Site:
http://cgi-resource.co.uk/pages/links2mods.shtml
Quote Reply
Re: RE: Glennu - randlinks.cgi In reply to
Hi Glennu...thanks for the above...as is works well..except showing the same link twice sometimes...which I see you have fixed in another post. But my prblem is..well be damed if I can change it to do what I wish to do...I have become so frustrated I have given up...can you help before I lose all my hair?. What I wish to do as I stated in my first post...is to have it pull 5 random links below each top category and to print it out onto a seperate page... so is you have 15 top category's..you would have 15 pages with 5 random links from all the subcategory's underneath. That way I can include them into the top category page's, which have no links...and use the 5 random links as featured links for each top category.
Does that explain what I wish...I no it's to hard for me to do...just not expert enough yet..but getting there....

Admittedlamb.

Quote Reply
Re: RE: Glennu - randlinks.cgi In reply to
If I have a few minutes next week I'll take another look.

I think it could be done by editing the sub build category pages and pulling out links at random from one of the store arrays.

Glenn

Links 2 Mods Site:
http://cgi-resource.co.uk/pages/links2mods.shtml
Quote Reply
Re: RE: Glennu - randlinks.cgi In reply to
Okay working version... builds a file called randomlinks.txt for each category if the category has a good enough amount of links to take random ones from.


go into nph-build.cgi, sub build_category_pages


Right near the bottom above:

print "\n";


Add:

if ($category_name =~ m,^([^/]*)$,) {
$num_random_links = 5;
}
else {
$num_random_links = 15;
}

if ($numlinks > ($num_random_links + 10)){

$link_output = "";
for ($r = 0; $r < $num_random_links; $r++) {
# Get the random line from the url lookup database.
$find = 0; $rand = int (rand ($numlinks + 0.5));
($rand == $numlinks) and ($rand--);
LINK: for ($i = 0; $i < $numlinks; $i++) {
#last LINK if ($tmp{$db_key} eq "");
$find++ == $rand or next;
$c = 0;

foreach $set (@visited) {
if ($rand == $set){
$c++;
}
}
if ($c == 0){
push (@visited, $rand);
%tmp = &array_to_hash ($i, @{$links{$cat}});
$link_output .= &site_html_link (%tmp) . "\n";

}
else {
$r = $r - 1;

}
last;
}
}
# Create the random links page.
open (CAT, ">$dir/randomlinks.txt") or &cgierr ("unable to open category page: $dir/$build_index. Reason: $!");
print CAT "$link_output";
close CAT;




Glenn

Links 2 Mods Site:
http://cgi-resource.co.uk/pages/links2mods.shtml
Quote Reply
Re: RE: Glennu - randlinks.cgi In reply to
Well I just tested it..and nope didn't work..well not sure whether it will...because it just timed out..the build reached the first subcategory..and it then just sat there until it timed out?

Question..why for the else statement and why is it set at 15.....anyway decreased it to 2 and 3 and still timed out..then went in to server admin and increased the cgi timeout my 5 minutes..and it still timed out...so there is no errors coming up..just timing out???

Admittedlamb.

Quote Reply
Re: RE: Glennu - randlinks.cgi In reply to
why for the else statement and why is it set at 15.....


cause if it's not a top level category then get 15 random links instead.


Are you building via telnet or online via admin? If your building online via admin... build via telnet instead!

Also put before the if/else statement:

$link_output ="";


Glenn

Links 2 Mods Site:
http://cgi-resource.co.uk/pages/links2mods.shtml
Quote Reply
Re: RE: Glennu - randlinks.cgi In reply to
Um actually I am sitting right next to my server! which is a dual p3 933 1024mb memory and the only thing runing at the time was the build...O and it's windows 2000 advance server. And there is no links at all on the top level categorys.. I have 15 top level categorys with 26 subcategorys underneath each one....thats what I wish to do pull 5 random links out of the 26 subcats under each topcat and insert in the topcat to use as featured links.. Ok did as you suggested..insert that code on top..no difference still stops...also run at command prompt (that would be the same as using telnet)..it justs stops when reaching the above code. And I mean just that it stops as if it as come to the end of the script???

Admittedlamb.

Programming ..just like women ..can't live with them and can't live without them!

Quote Reply
Re: RE: Glennu - randlinks.cgi In reply to
Oh just noticed something. Change:

for ($i = 0; $i < $num_random_links; $i++) {

To:

for ($r = 0; $r < $num_random_links; $r++) {

and change:

$i = $i - 1;

To:

$r = $r - 1;


That was prob causing it to loop to infinity which would explain the time out!







Glenn

Links 2 Mods Site:
http://cgi-resource.co.uk/pages/links2mods.shtml
Quote Reply
Re: RE: Glennu - randlinks.cgi In reply to
Ok Glennu..did as suggested above...and nope still the same...it stops still or loops still...O and by the way this..<$dir/category_name.txt.. that wouldn't work..for the topcat it would..but for the subcats..it would try and build in blues/A/blues/A/A.txt....and of course no such directory..thats my nickles worth..anyway it stops before then...did fiddle no success though..tis a bummer..its the one thing I really need..Ok you perl guru's..see if you can help Glennu and me work it out..I just no it can be done!

Admittedlamb.

Quote Reply
Re: RE: Glennu - randlinks.cgi In reply to
When I get back home I'll have access to test it and will take a quick look. Cause as you say it shouldn't be too hard to do. Also it's easier when I can see it in action and see what's going on (plus I'd also find a solution to this useful!).


One more thing to change (this'll get rid of the error you said about):

Change:

$category_name.txt

To:

randomlinks.txt



Glenn

Links 2 Mods Site:
http://cgi-resource.co.uk/pages/links2mods.shtml
Quote Reply
Re: RE: Glennu - randlinks.cgi In reply to
Okay tested it online (made a number of alterations) and it now works! Instead of posting all the code again I'll alter my post above so it'll have the final code in it...

Glenn

Links 2 Mods Site:
http://cgi-resource.co.uk/pages/links2mods.shtml
Quote Reply
Re: RE: Glennu - randlinks.cgi In reply to
Works like a charm...except *grins*...its not exactly what I wanted..but I will use it. What I actually wished to do..was not do random links for each and every category. But to pull 5 links out of the "whole 26 subcats" not individually..under each topcat..um...example I have a topcat called blues..underneath that topcat I have 26 subcats named from A through to Z. So in effect I 15 topcats..and 26 subcats underneath each one name A to Z.. so it looks like this....

Homepage/
blues/
blues/a/
blues/b/

pop/
pop/a
pop/b

Soforth..
So what i wish to do is pull 5 random links out of the 26 subcats "just 5 links out of the whole 26" and to insert in the topcat with a include...does that explain it??...the one ya done works great for what it does...and i could use that to put say 1 featured link for each subcat.

Admittedlamb.

Quote Reply
Re: RE: Glennu - randlinks.cgi In reply to
Ah I see, sorry I should had read your post a bit more. What you want can be done. Way I'd do it is to instead put the code in a new sub. Then add some code to sub build stats:


if ($subcategory =~ m,^$values[$db_category]/([^/]+)/*,) {
push (@{$random_links2{$category}}, @values)
}

else {
$category = $values[$db_category];
push (@{$random_links{$category}}, @values)
}

Then alter the random code to include a loop to go thru each top level cat and then go thru each subcategory. (you'd also need to alter it slightly so instead of $links{$cat} it'd now be $random_links{$category})




Glenn

Links 2 Mods Site:
http://cgi-resource.co.uk/pages/links2mods.shtml
Quote Reply
Re: RE: Glennu - randlinks.cgi In reply to
Ok Glennu...will give that a shot..see what happens...see if I got it right...put the if else statment into the the build stats sub....change $links{cat}...then create a loop right?
First question in this push (@{$random_links2{$category}}, @values)....where is the "$random_links2" defined??.

Admittedlamb.


Quote Reply
Re: RE: Glennu - randlinks.cgi In reply to
Sorry ignore what I was saying above, just re-read your post:

Instead use the code as you were before in sub build category pages but...

Replace:

if ($category_name =~ m,^([^/]*)$,) {
$num_random_links = 5;
}
else {
$num_random_links = 15;
}

With:

$num_random_links = 5;


Replace:

if ($numlinks > ($num_random_links + 10)){


With:

if (($numlinks > ($num_random_links + 10)) and ($category_name =~ m,.*/([^/]+)$,)){


And replace:

last;
}
}
# Create the random links page.
open (CAT, ">$dir/randomlinks.txt") or &cgierr ("unable to open category page: $dir/$build_index. Reason: $!");
print CAT "$link_output";
close CAT;

With:

last;
}
# Create the random links page.
open (CAT, ">$dir/randomlinks.txt") or &cgierr ("unable to open category page: $dir/$build_index. Reason: $!");
print CAT "$link_output";
close CAT;
}

Glenn

Links 2 Mods Site:
http://cgi-resource.co.uk/pages/links2mods.shtml
Quote Reply
Re: RE: Glennu - randlinks.cgi In reply to
Hi Glennu, yep did as you suggested..but didn't seem to change anything...built exactly the same why as before..for every subcat???..I have tried various things...but mainly get errors...but unlike you I don't no link2 that well..and takes me ages to find where all the definitions and variables come from.

Admittedlamb.

Quote Reply
Re: RE: Glennu - randlinks.cgi In reply to
Yeah sorry I'm getting confused as to what you want, is this correct:

say you had:

music
rock, pop, trance


You'd want 5 random links pulled from all those subcats together.

Glenn

Links 2 Mods Site:
http://cgi-resource.co.uk/pages/links2mods.shtml
Quote Reply
Re: RE: Glennu - randlinks.cgi In reply to
If you want to do as I mentioned above it's a little bit more work. You'll want to create a new sub to run it all in you'll need to include the sub in the build all and staggered...


In sub build staggered find:

print "Updating ratings .. \n";
$t1 = time();
&build_update_ratings;
print "Done (", time - $t1, " s)\n\n";

Beneath it add:

print "Updating random links .. \n";
$t1 = time();
&build_update_random_links;
print "Done (", time - $t1, " s)\n\n";


in sub build_all find:

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

Beneath it add:


# Create Random links pages
$use_html ?
print "Building <A HREF=\"$build_random_url/$build_index\">Random Links</A> . . .\n" :
print "Building Top Rated . . .\n";
&build_random_links;
print "Done\n\n";


Then add this to sub build stats.

if ($subcategory =~ m,^$values[$db_category]/([^/]+)/*,) {
push (@{$random_links{$category}}, @values)
}


Then create the new sub build_random_links.

to this sub add the random code used before except put it in a loop 'foreach $category (@{$random_links{$category}}){ etc...'. Also change the $links{$cat}. You'll also need to add some code to calc $numlinks.


Glenn

Links 2 Mods Site:
http://cgi-resource.co.uk/pages/links2mods.shtml
Quote Reply
Re: RE: Glennu - randlinks.cgi In reply to
Thats nearly right....um ok in my home page i have 15 categorys...won't name them all...blues, pop, rock ect. ok.
click on blues..you bring up the blues page..this as no links on it whats soever...on the blues page there are 26 cats named A through to Z ok.....what I wish to do is pull 5 random links out the cats A through to Z (not 5 links out of each of them) and put them 5 links into the blues page to use as featured links for blues. So I would have 5 radoms links on the blues page..the pop page soforth.

Does that better explaine it..I would never make a teacher!

Admittedlamb.


PS: I must have be typing this out as you was typing the above.

Quote Reply
Re: RE: Glennu - randlinks.cgi In reply to
Ok I will give that a go but not now..tis 2:01am in the morning here (Australia) and my brain is just fudge at the moment....but if you get a spare moment *grins* would you be able to print out the sub routine for me...but only if have time..you have spent considerable time on this now..and must have other more urgent things to do...and beleive me I apprecaite the help you have given!

Admittedlamb.

Quote Reply
Re: RE: Glennu - randlinks.cgi In reply to
The instructions above should get it so you'd have a random links page (taking a total of 5 links chosen at random from all of the sub categories for that main category) This info is stored in a txt file in the same directory as the each main category. So all you'd need to do is include this file.

This would this for all your main categories, but you could of course just alter the if statement, if you wanted it to be done for blues category only.

Glenn

Links 2 Mods Site:
http://cgi-resource.co.uk/pages/links2mods.shtml
Quote Reply
Re: RE: Glennu - randlinks.cgi In reply to
nah not just for blues..just used that as a example...for all..and again thanks...if you need any help with asp, jscript..thats my forte.

Admittedlamb.

Quote Reply
Re: RE: Glennu - randlinks.cgi In reply to
No problem, I will prob come back to this again at some point as I will prob use it for a future site directory I have in mind.

The code/instructions I provided above should do it, it's off the top of my head, I haven't tested it and so it might need a little playing around with...

Glenn

Links 2 Mods Site:
http://cgi-resource.co.uk/pages/links2mods.shtml
> >