DamienRoche Posted September 23, 2008 Share Posted September 23, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/125497-help-needed-with-link-picker/ Share on other sites More sharing options...
F1Fan Posted September 23, 2008 Share Posted September 23, 2008 Select a random element of the array, then do it again with a while loop that checks if it is the same element. If it is, loop and grab another random one. Keep doing that until you have two unique ones. Quote Link to comment https://forums.phpfreaks.com/topic/125497-help-needed-with-link-picker/#findComment-648801 Share on other sites More sharing options...
jonsjava Posted September 23, 2008 Share Posted September 23, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/125497-help-needed-with-link-picker/#findComment-648805 Share on other sites More sharing options...
GingerRobot Posted September 23, 2008 Share Posted September 23, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/125497-help-needed-with-link-picker/#findComment-648812 Share on other sites More sharing options...
jonsjava Posted September 23, 2008 Share Posted September 23, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/125497-help-needed-with-link-picker/#findComment-648825 Share on other sites More sharing options...
DamienRoche Posted September 23, 2008 Author Share Posted September 23, 2008 Wow! thank you both for your time. Gingers worked best for what I was looking for. Thanks for pointing that out jons. Well, both are stored in my swipe file Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/125497-help-needed-with-link-picker/#findComment-648831 Share on other sites More sharing options...
discomatt Posted September 23, 2008 Share Posted September 23, 2008 for($x=0;$x<$numberoflinks;$x++){ echo $links[$x].'<br />'; } can be replaced with array_slice( $links, 0, $numberoflinks ); Quote Link to comment https://forums.phpfreaks.com/topic/125497-help-needed-with-link-picker/#findComment-648845 Share on other sites More sharing options...
DamienRoche Posted September 23, 2008 Author Share Posted September 23, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/125497-help-needed-with-link-picker/#findComment-648856 Share on other sites More sharing options...
F1Fan Posted September 23, 2008 Share Posted September 23, 2008 Replace the echo with code that adds values to a new array, then return the array. Quote Link to comment https://forums.phpfreaks.com/topic/125497-help-needed-with-link-picker/#findComment-648871 Share on other sites More sharing options...
discomatt Posted September 23, 2008 Share Posted September 23, 2008 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 ); Quote Link to comment https://forums.phpfreaks.com/topic/125497-help-needed-with-link-picker/#findComment-648913 Share on other sites More sharing options...
DamienRoche Posted September 23, 2008 Author Share Posted September 23, 2008 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! Quote Link to comment https://forums.phpfreaks.com/topic/125497-help-needed-with-link-picker/#findComment-648927 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.