25, July 2008

select random value from array - webmaster forum

 
Webdigity webmaster forums
This forum shares its ad revenue with its members!
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: select random value from array
« previous next »
Pages: [1] Print

Author Topic: select random value from array  (Read 868 times)
aka J Love
Community Supporter ?
Bill Gates is my home boy
*****
Gender: Male
Posts: 884
1636 credits
Members referred : 4



« on: Jun 07, 2007, 11:10:19 AM »

i have tried a few different methods, but seem to run into a few problems with each.. what im basically trying to do is load a random ID from an array of banner_id's . this random ID (whatever it may be based on the solution found from your help in this thread) will be used in a function to display the banner based on DB data of course  Cool

here is what i have for putting the banner_id's into an array, for some reason when i print_r on this array it shows some really strange results, as if there are like 6-8 banner ID's where there are really only 4 in the DB currently.

$bannerQ dbquery("SELECT * FROM ".$db_prefix."banners WHERE banner_state = '1'"); 

while (
$bannerData dbarray($bannerQ)) 
$bannerID[] = $bannerData['banner_id']; 


Then, we have to shuffle or randomize them somehow.. what i have here sort of works, but sometimes displays no banner or never the other 2 banners (only shows the first two)

$banners array_rand($bannerID); 


I have also tried to use shuffle() on the bannerID, but im not doing it right or something because it returns empty result  Sad So how do I further randomize the $bannerID array, and then get that ID correctly? Thanks in advance for hte help

Visit through proxy Visit through proxy Visit through proxy

Last blog : phpHaze 1.59.1 in Development
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6280
38506 credits
Members referred : 374


It's time to use PHP5!


« Reply #1 on: Jun 07, 2007, 11:38:06 AM »

that works much better:


$arr
[] = 'value 1';
$arr[] = 'some value';
$arr[] = 'another value';


$count count($arr)-1;
$rand rand(0$count);

echo 
$arr[$rand];


Last blog : 4th of July Lottery from TemplateMonster.com
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 7975
40807 credits
Members referred : 3



« Reply #2 on: Jun 07, 2007, 12:08:39 PM »

shuffle() is not returning anything. You should use it like this :

 shuffle$array );

and then the array will be shuffled.

Now if you want to get only one random id you should do this with SQL :

$bannerQ dbquery("SELECT * FROM ".$db_prefix."banners WHERE banner_state = '1' ORDER BY RAND() LIMIT 1");

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : MIA - Where Nick and Tim
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6280
38506 credits
Members referred : 374


It's time to use PHP5!


« Reply #3 on: Jun 07, 2007, 12:10:59 PM »

yeah a random order inside the sql is nice but not very flexible, I guess the function I suggested is the best one Cheesy

(thats what I read in the manual)


Last blog : 4th of July Lottery from TemplateMonster.com
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 7975
40807 credits
Members referred : 3



« Reply #4 on: Jun 07, 2007, 12:14:16 PM »

In case you want to do this with php there is a faster way Smiley

echo $arr[array_rand($arr)];

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : MIA - Where Nick and Tim
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6280
38506 credits
Members referred : 374


It's time to use PHP5!


« Reply #5 on: Jun 07, 2007, 12:25:58 PM »

In case you want to do this with php there is a faster way Smiley

echo $arr[array_rand($arr)];

yeah thats a nice one Smiley


Last blog : 4th of July Lottery from TemplateMonster.com
aka J Love
Community Supporter ?
Bill Gates is my home boy
*****
Gender: Male
Posts: 884
1636 credits
Members referred : 4



« Reply #6 on: Jun 07, 2007, 07:18:26 PM »

thanks for the help guys, this seems to work pretty accurately. i chose the SQL method as it seems the most simple Cool

Visit through proxy Visit through proxy Visit through proxy

Last blog : phpHaze 1.59.1 in Development
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 7975
40807 credits
Members referred : 3



« Reply #7 on: Jun 07, 2007, 08:00:22 PM »

Using sql when you can is better because in a high traffic site that has different servers for http and database the overhead is distributed better.

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : MIA - Where Nick and Tim
aka J Love
Community Supporter ?
Bill Gates is my home boy
*****
Gender: Male
Posts: 884
1636 credits
Members referred : 4



« Reply #8 on: Jun 07, 2007, 08:04:00 PM »

Using sql when you can is better because in a high traffic site that has different servers for http and database the overhead is distributed better.

i have configured the showbanner function to automatically optimize the banner table in the mysql db each time a banner is shown (As it increases impressions and click count etc).. that should help a bit yes? Cheesy

Visit through proxy Visit through proxy Visit through proxy

Last blog : phpHaze 1.59.1 in Development
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 7975
40807 credits
Members referred : 3



« Reply #9 on: Jun 07, 2007, 08:06:26 PM »

That will make the server really slow if there are many concurrent requests.

In general is better to keep data that change in different tables, or instead of what you do, optimize the table with some kind of scheduled task (crontab or whatever)

The OPTIMIZE command will lock the table for some time, and you don't want that to a production server Wink

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : MIA - Where Nick and Tim
aka J Love
Community Supporter ?
Bill Gates is my home boy
*****
Gender: Male
Posts: 884
1636 credits
Members referred : 4



« Reply #10 on: Jun 07, 2007, 08:08:26 PM »

That will make the server really slow if there are many concurrent requests.

In general is better to keep data that change in different tables, or instead of what you do, optimize the table with some kind of scheduled task (crontab or whatever)

The OPTIMIZE command will lock the table for some time, and you don't want that to a production server Wink

alright i see your point thanks Nik; i will just add a maintenance section for the banners admin where you can run optimizer on the table ever-so-often and add it to cron for my own sites..

Visit through proxy Visit through proxy Visit through proxy

Last blog : phpHaze 1.59.1 in Development
Trackback URI for this entry : http://www.webdigity.com/trackback.php?topic=6762
Tags : php random banners shuffle array_rand Bookmark this thread : Digg Del.icio.us Dzone more....

Topic sponsors:
Get a permanent link here for $1.99!


Pages: [1] Print 
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: select random value from array
« previous next »
Jump to:
User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
Jul 25, 2008, 12:06:04 AM





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!


Forum Statistics
Total Posts: 35.717
Total Topics: 7.379
Total Members: 3.711
Tutorials : 56
Resources : 143
Designs : 220
Latest Member: Asimina

30 Guests, 5 Users online :

12 users online today:



Readers

Web Design Gallery · Whois Lookup · Pagerank · Tag Browsing · Lo-fi version · Syndication · Webmaster forum history · Advertise
Developed by HumanWorks © 2005 - 2008 Webdigity webmaster community · sublime directory
Webdigity Webmaster Forums | Powered by SMF 1.0.12. © 2001-2005, Lewis Media. All Rights Reserved.