Jump to content

Help needed with 'link picker'


DamienRoche

Recommended Posts

I've decided to construct a random link picker.

 

I am stuck on one stage. Just to let you know where I am.

 

I have used glob and now have the following four files in an array:

 


(1)link.txt 
(23)link.txt
(43)link.txt
(12)link.txt

///// the array is $link[0-4]

 

I have no trouble using the array. What I am having trouble with is randomly selecting 2 of those links from the array. I have done it with one...simply using:

 

$randlink = rand(0, $linknum);

 

But obviously that doesn't recognize unique entries.

 

is there a solution to this?

 

Thanks.

Link to comment
Share on other sites

quick script. I know it can be written better, but this was the fastest I could come up with.

<?php
function pick_link(){
$link[] = "http://jonsjava.com"; //hehe, a plug for my site
$link[] = "http://google.com";
$link[] = "http://php.net";
$link[] = "http://linux.org";
$link_totals = count($link) - 1;
$random_link = rand(0,$link_totals);
$random_link = $link[$random_link];
return $random_link;
}


/* Usage:
* Pretty simple, actually, just tell it to run the pick_link in a loop
*/
$count = 2;
while ($count > 0) {
$mylink = pick_link();
print "<a href='$mylink'>Link$count</a>\n";
$count--;
}

What it does is creates a function (pick_link), which looks through all the links you have, and picks a random one. You then request a link from the function in a while loop (or however you like), and it will give you a random one every time.

Link to comment
Share on other sites

If you do mean that you'd like to show more than one random link at a time, i'd do it like this:

 

$links[] = 'link one';
$links[] = 'link two';
$links[] = 'link three';
$links[] = 'link four';

function randomlinks($links,$numberoflinks){
if(count($links) < $numberoflinks){
	return false;
}
shuffle($links);
for($x=0;$x<$numberoflinks;$x++){
	echo $links[$x].'<br />';
}
}
randomlinks($links,2);

 

If you meant that you'd like to show a different link over time each time a user comes to your site, then it becomes a bit more complicated and you'll need to store what was shown in a database.

 

jonsjava: yours does not ensure all links returned are unique.

Link to comment
Share on other sites

and if you want to go a step further:

<?php
$links[] = 'link one';
$links[] = 'link two';
$links[] = 'link three';
$links[] = 'link four';

function randomlinks($links,$numberoflinks=1){
if(count($links) < $numberoflinks){
	return false;
}
shuffle($links);
for($x=0;$x<$numberoflinks;$x++){
	echo $links[$x].'<br />';
}
}
randomlinks($links,2);

now, if you do randomlinks($links), it will fetch one automatically.

Link to comment
Share on other sites

I've actually run into another little niggle.

 

Is it possible to put the two random links into an array? (I'm using Gingers code).

 

So I have:

 

randomlinks($links, 2);

 

But how do I put the two that were chosen into their own array?

 

Thanks for the extension matt, I'll try that.

Link to comment
Share on other sites

Or just use array_slice

 

$links[] = 'link one';
$links[] = 'link two';
$links[] = 'link three';
$links[] = 'link four';

function randomlinks($links,$numberoflinks=1){
if(count($links) < $numberoflinks){
	return false;
}
shuffle($links);
return array_slice( $links, 0, $numberoflinks );
}
$random = randomlinks($links,2);

print_r( $random );

Link to comment
Share on other sites

Hey guys, thanks for the all the input.

 

Here's what I've gone with:

 

foreach(glob("links/(*)link.*") as $linkfile){

$links["$loop2"] = $linkfile;
echo "Link File:$linkfile<br>";

$loop2++;

}$loop2 = $loop2 -1;

shuffle($links);

echo "<br><b>Chosen Links:</b><br>$links[1]<br>$links[2]";

 

So I've kept the original array I had and simply shuffled it, yet used the same numbers.

 

I hope is isn't deprecated anytime soom..eek.

 

Much appreciate all of the advice and info. Bookmarked this so I can come back should I need to extend my script.

 

Thanks again!

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.