TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Showing random links with an array (http://www.talkphp.com/advanced-php-programming/3518-showing-random-links-array.html)

paulOr 10-23-2008 10:22 PM

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?

Enfernikus 10-23-2008 10:38 PM

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;
        }
        
    }
    
?>


ReSpawN 10-23-2008 10:56 PM

@ 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!

Salathe 10-23-2008 11:11 PM

Quote:

Originally Posted by paulOr (Post 19077)
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.

Enfernikus 10-23-2008 11:22 PM

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.

sketchMedia 10-24-2008 08:49 AM

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($links05);

    foreach (
$temp as $link) {
        
printf("<a href=\"%s\" class=\"menu_child_links\">%s</a>"$link['url'], $link['label']);
    }


ReSpawN 10-24-2008 09:28 AM

Quote:

Originally Posted by Enfernikus (Post 19084)
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. ;-)


All times are GMT. The time now is 01:44 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0