Greenguy's Board


Go Back   Greenguy's Board > Programming & Scripting
Register FAQ Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rating: Thread Rating: 1 votes, 5.00 average. Display Modes
Prev Previous Post   Next Post Next
Old 2008-08-22, 10:44 AM   #4
cd34
a.k.a. Sparky
 
cd34's Avatar
 
Join Date: Sep 2004
Location: West Palm Beach, FL, USA
Posts: 2,396
Code:
<pre>
<?php
$links = array(
               "arrayitem1",
               "arrayitem2",
               "arrayitem3",
               "arrayitem4",
               "arrayitem5",
               "arrayitem6",
               "arrayitem7",
               "arrayitem8",
               "arrayitem9",
               "arrayitem10",
               "arrayitem11"
              );
$randomized_links=array_rand($links,3);

print_r($randomized_links);

foreach ($randomized_links as $linknumber) {
  print $links[$linknumber] . "\n";
}

shuffle($links);
print_r(array_slice($links,0,4));
?>
Something like that might get you started. array_rand has a parameter that allows you to choose the number of array keys to return. It will make sure it reports no duplicates and remember, arrays start with an index of 0. If you intend to display the entire array, and don't really care about the keys, you can also use shuffle. shuffle combined with array_slice might be a little faster. Remember that shuffle alters your existing array -- if you need to reference it later in the original order, make a copy before you shuffle it.

With SQL (and I really hate when this is done on a live site) you can do something like:

Code:
select linkname,linkurl from tablename where category='teen' order by rand() limit 10;
__________________
SnapReplay.com a different way to share photos - iPhone & Android
cd34 is offline   Reply With Quote
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 01:28 PM.


Mark Read
Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
© Greenguy Marketing Inc