Topic: select random value from array (Read 958 times)
aka J Love Community Supporter?
Bill Gates is my home boy
Gender:
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
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 So how do I further randomize the $bannerID array, and then get that ID correctly? Thanks in advance for hte help
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?
I am a metal monkey!
Administrator Community Supporter?
Jedai Sword Master
Gender:
Posts: 8116
41653 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
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
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..