 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
10-23-2008, 10:22 PM
|
#1 (permalink)
|
|
The Wanderer
Join Date: Nov 2007
Location: Edinburgh
Posts: 21
Thanks: 1
|
Showing random links with an array
Code:
<?php
$links = array(
array('url'=>'#', 'label'=>'Proxy #1'),
array('url'=>'#', 'label'=>'Proxy #2'),
array('url'=>'#', 'label'=>'Proxy #3'),
array('url'=>'#', 'label'=>'Proxy #4'),
array('url'=>'#', 'label'=>'Proxy #5'),
array('url'=>'#', 'label'=>'Proxy #6'),
array('url'=>'#', 'label'=>'Proxy #7'),
array('url'=>'#', 'label'=>'Proxy #8'),
array('url'=>'#', 'label'=>'Proxy #9'),
array('url'=>'#', 'label'=>'Proxy #10')
);
$random_keys = array_rand($links, 5);
foreach ($random_keys as $key) {
printf("<a href=\"%s\" class=\"menu_child_links\">%s</a>", $links[$key]['url'], $links[$key]['label']);
}
?>
this code allows me to show 5 random links, however when it loads, sometimes there is like:
Proxy #1
Proxy #1
Proxy #1
Proxy #3
true enough, its random but i dont want dupliates, i need all random like:
Proxy #6
Proxy #7
Proxy #9
Proxy #2
Proxy #3
What can i add to the code to get it without duplicates?
|
|
|
|
10-23-2008, 10:38 PM
|
#2 (permalink)
|
|
The Addict
Join Date: Jun 2008
Posts: 335
Thanks: 2
|
PHP Code:
<?php
$links = array( array('url'=>'#', 'label'=>'Proxy #1'), array('url'=>'#', 'label'=>'Proxy #2'), array('url'=>'#', 'label'=>'Proxy #3'), array('url'=>'#', 'label'=>'Proxy #4'), array('url'=>'#', 'label'=>'Proxy #5'), array('url'=>'#', 'label'=>'Proxy #6'), array('url'=>'#', 'label'=>'Proxy #7'), array('url'=>'#', 'label'=>'Proxy #8'), array('url'=>'#', 'label'=>'Proxy #9'), array('url'=>'#', 'label'=>'Proxy #10') ); $used = array(); $count = count($links) -1; $iterations = 5; for($i = 0; $i < $iterations; ++$i) { $num = rand(0, $count); if( !in_array($num, $used) ) { $used[$i] = $num; printf("<a href=\"%s\" class=\"menu_child_links\">%s</a><br />\r\n", $links[$num]['url'], $links[$num]['label']); } else { --$i; } } ?>
|
|
|
|
10-23-2008, 10:56 PM
|
#3 (permalink)
|
|
The Frequenter
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
|
@ Enfernikus, the most regular and correct way of increasing or decreasing is $i++ and $i--.
PHP Code:
<?php
error_reporting( E_ALL | E_STRICT ); ob_start("ob_gzhandler");
$proxyArray = array("proxy.address.com/proxy1", "proxy.address.com/proxy2", "proxy.address.com/proxy3", "proxy.address.com/proxy4", "proxy.address.com/proxy5", "proxy.address.com/proxy6", "proxy.address.com/proxy7", "proxy.address.com/proxy8", "proxy.address.com/proxy9", "proxy.address.com/proxy10"); $proxyArraySize = (sizeof($proxyArray) - 1); shuffle($proxyArray); $proxiesToDisplay = 5; $displayArray = array(); for($i = 0; $i < $proxiesToDisplay; $i++) { $randomProxyNumber = mt_rand(0, $proxyArraySize); if (!in_array($proxyArray[$randomProxyNumber], $displayArray)) { array_push($displayArray, $proxyArray[$randomProxyNumber]); } else { $i--; } } foreach($displayArray as $displayProxy) { echo $displayProxy . '<br>' . "\n"; }
Naturally, you can apply the same with the function in_array again. I only check for the values. Since you've got a unique value, you should check for both. Assuming they don't match up.
The best thing to do is to make it database driven, because then you can later add the URL which makes checking easier.
Also, the mt_rand can be greatly increased in random numbers. Insert them all into an array (about 20 times, of course in the for loop) and then choose with the mt_rand again a number (key) out of that array, which resembles another number. For example, you'll get mt_rand 20 times, on key 19 you're going to get the number 8. So, if the next mt_rand (that finalizes the random) poops out the key 19, you'll get 8 as the random number. This would mean that you can make about 10000 keys in the array, all matching from 1 to the array size of your proxy array. In this case, 10000 different numbers from 0 to 10.
Good luck!
__________________
"Life is a bitch, take that bitch on a ride"
|
|
|
10-23-2008, 11:11 PM
|
#4 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
|
Quote:
Originally Posted by paulOr
this code allows me to show 5 random links, however when it loads, sometimes there is like:
Proxy #1
Proxy #1
Proxy #1
Proxy #3
true enough, its random but i dont want dupliates
|
Using the code that you provided, there shouldn't be any duplicates (unless you have duplicates in the original array). Running this code a million times, there's not even one duplicated value (from a quick test) which confirms my suspicion.
If you're wanting another approach, copy the original array into a new temporary variable. Then, shuffle it and array_slice however many you want.
|
|
|
|
10-23-2008, 11:22 PM
|
#5 (permalink)
|
|
The Addict
Join Date: Jun 2008
Posts: 335
Thanks: 2
|
Yeah I know, but the method I use is faster and ++$i and $i++ look nearly identical - there are some drawbacks but I think it's worth the increase.
|
|
|
|
10-24-2008, 08:49 AM
|
#6 (permalink)
|
|
The Prestige
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
|
Yea Salathe is correct, there wont be any duplicates unless you introduce them into the array. Therefor this is probably the easiest way of doing it:
PHP Code:
<?php $links = array(array('url'=>'#', 'label'=>'Proxy #1'), array('url'=>'#', 'label'=>'Proxy #2'), array('url'=>'#', 'label'=>'Proxy #3'), array('url'=>'#', 'label'=>'Proxy #4'), array('url'=>'#', 'label'=>'Proxy #5'), array('url'=>'#', 'label'=>'Proxy #6'), array('url'=>'#', 'label'=>'Proxy #7'), array('url'=>'#', 'label'=>'Proxy #8'), array('url'=>'#', 'label'=>'Proxy #9'), array('url'=>'#', 'label'=>'Proxy #10'));
shuffle($links); $temp = array_slice($links, 0, 5);
foreach ($temp as $link) { printf("<a href=\"%s\" class=\"menu_child_links\">%s</a>", $link['url'], $link['label']); }
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
|
|
|
|
10-24-2008, 09:28 AM
|
#7 (permalink)
|
|
The Frequenter
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
|
Quote:
Originally Posted by Enfernikus
Yeah I know, but the method I use is faster and ++$i and $i++ look nearly identical - there are some drawbacks but I think it's worth the increase.
|
Next to that, I can honestly say it's more readable than the -- before.  But that's just a matter of taste.
Slicing the array is also a good method, but I prefer my way. 
__________________
"Life is a bitch, take that bitch on a ride"
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|