Reply
Randomly List Items
Old 04-07-2008, 04:02 AM Randomly List Items
choskins102's Avatar
Super Talker

Posts: 124
Name: Casey
Hey, guys. I have a list of ten item and I want to randomly select 5 of these items and display them. The problem is, I don't want the items to be stored in a MySQL database and I need to make sure the same item is not selected twice. Any ideas?
choskins102 is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
     
Old 04-07-2008, 04:47 AM Re: Randomly List Items
mtishetsky's Avatar
Super Spam Talker

Posts: 880
Location: Volendam, Netherlands
PHP Code:
<?
    $list 
range(11,22);
    
$selected = array();
    
$indexes = array();

    
$iterations 0;
    while (
count($selected) < 5) {
        
$index rand(0count($list) - 1);
        if (!isset(
$indexes[$index])) {
            
$indexes[$index] = 1;
            
$selected[] = $list[$index];
        }
        
$iterations += 1;
    }

    
print_r($selected);
    
print_r($indexes);
    print 
$iterations."\n";
                   
?>
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Reply     « Reply to Randomly List Items
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.11017 seconds with 13 queries