Feature requests » Random Link display

added on May 6, 2007 by donnan
I wish to display links randomly on my links page.

I can't see any documentation on this.

Can you let me know if it can be done and where I can find any info on it.
added on May 7, 2007 by v0id
hi donnan.

Saw your post on the other thread also.
Sorry for the late reply, was away for the weekend.

The only randomness you can get in LinkEX is to the set "Order" to random in the category setting.
In this way, each time the category is flushed to the output file it will be randomized. Just remember that the file is
only updated whenever needed (like when a link in the category has been added or changed), so the links will not change
so often.

I still have no documentation on this, so if you would like more info, please let me know

- v0id
added on May 7, 2007 by donnan
As the script is run everytime my links page is opened as I have it setup to use ssl, there must be a way to randomise
the links before they are written to my links page.

Can you tell me where in your code, I can find where it opens the file and retrieves the links data. I may be able to
add a randomise code in there.

Cheers
donnan
added on May 7, 2007 by v0id
Hey.

Actually the script works the other way around, so to speak.

When the links are displayed on your site, there are no dynamic things going on. It's a simple include and output
whatever is in the file operation going on.
If you open your FTP and points it to the linkex/data/output directory and have a look at the files there, you'll see
the complete html you also see where you include the files on your homepage.

All the dynamics takes place when "needed", when you change a link, or add a new link.

Between your homepage and the data files in linkex there are no actual code going on. LinkEX lives it's own life,
creating the data files, which you include. To make it randomizing them you would have to add some code between your
homepage and the output file from linkex.

- v0id
added on May 8, 2007 by donnan
Is there any chance you could write me a function to randomize the links before it writes the code to the web page?

Would be really appreciated.

Cheers
donnan
added on May 14, 2007 by v0id
Hi,
sorry about the delay, but I managed to come up with a solution while thinking about bit outside "the box".

I took advantage of the {IF} template variables:

My template looks like this:

{IF $FIRST}<?php
$links = <<<END{/IF}
<a href="{$URL}" title="{$ANCHOR}">{$ANCHOR}</a><br />{IF $LAST}
END;
srand((float)microtime() * 1000000);
$links = explode("\n", $links );
shuffle( $links );
echo join( "\n", $links );
?>
{/IF}

What it does, it to wrap some PHP code around the actual links, putting the links into a variable, which is then
processed by PHP before being displayed.

If people don't change the category template, you can see the result here http://demo.linkex.dk/random.php
Try reloading the page a few times to see the effect

It might not be the most robust solution, but it gets the job done :)

- v0id
added on May 15, 2007 by donnan
Thanks v0id,

I knew you were clever enough to sort this problem out.

Thanks alot. Most appreciated.

Can you tell me exactly where to place the code. I assume it is in the web page, but I don't really want to change the
.htm file to a .php file.

Cheers
donnan
added on May 15, 2007 by v0id
lol thanks for the kind words :)

eh you say you don't want to change the .htm file, how do you include the linkex output right now? Perhaps you mean it's
a .shtml?

The code is going in the category template, and should be included in a .php file for it to work.

- v0id
added on May 16, 2007 by donnan
Thanks v0id,

I thought that might be the case.

Anyhow, I wrote a little perl script that calls the links from their data file and randomises them before displaying
them in my links page.

Thanks again for your help.

Cheers
donnan
added on May 16, 2007 by donnan
it is only a .htm file.
added on May 16, 2007 by v0id
ah you must have been playing with your webserver setup then :)
added on May 17, 2007 by donnan
No, not at all.

Just wrote a perl cgi script and then called the code through that. The code will place the code straight into a .htm
page. No need to have your page named .php

shuffle.cgi----------------------------------
[code]
#!/usr/bin/perl

&parse_query; ###Get any info###

$file = ($query {'f'});

use List::Util qw/shuffle/;

print "Content-type: text/html\n\n";

$dbfile = "../links/data/output/$file";

open (DATA,"$dbfile");
flock(DATA, 2) or die "Cannot Lock File: $!";
seek DATA, 0, 0;
@db = <DATA>;
close(DATA);

my $mystring = "@db";

#if ($mystring =~ m/<tr(.* ?)\/tr>/) {
#print "<tr$1/tr>";
#}
my $html = "@db";
my @links = split '</tr>',
$html; my %hash = @links;
@links = shuffle keys %hash;
print "$_</tr>$hash{$_}</tr>\n\n" foreach @links;


sub parse_query {
@pairs = split(/&/, $ENV{'QUERY_STRING'});
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ s/!/=/g; #change ! to = if double querystrings change all = to ! in the second url.
$value =~ s/%/?/g; #change % to ? if double querystrings change all ? to % in the second url.
$value =~ s/£/&/g; #change £ to & if double querystrings change all & to £ in the
second url.
$query{$name} = $value;
}
}

[/code]

changed <--! #include file="links/data/output">
to <--!#include virtual="cgi-bin/shuffle.cgi?f=1003>

Easy. No server changes needed.

Add reply

This thread has been inactive for more than 14 days, so it has been locked.

Feel free to create a new thread with your questions.