Sublime directory Surf the web anonymous Pagerank Monitor


select random value from array

Meth0d
Thu 7 June 2007, 10:10 am GMT +0200
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  8)

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

olaf
Thu 7 June 2007, 10:38 am GMT +0200
that works much better:


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


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

echo 
$arr[$rand];
?>

Nikolas
Thu 7 June 2007, 11:08 am GMT +0200
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");?>

olaf
Thu 7 June 2007, 11:10 am GMT +0200
yeah a random order inside the sql is nice but not very flexible, I guess the function I suggested is the best one :D

(thats what I read in the manual)

Nikolas
Thu 7 June 2007, 11:14 am GMT +0200
In case you want to do this with php there is a faster way :)

echo $arr[array_rand($arr)];?>

olaf
Thu 7 June 2007, 11:25 am GMT +0200
In case you want to do this with php there is a faster way :)

echo $arr[array_rand($arr)];?>

yeah thats a nice one :)

Meth0d
Thu 7 June 2007, 06:18 pm GMT +0200
thanks for the help guys, this seems to work pretty accurately. i chose the SQL method as it seems the most simple 8)

Nikolas
Thu 7 June 2007, 07:00 pm GMT +0200
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.

Meth0d
Thu 7 June 2007, 07:04 pm GMT +0200
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? :D

Nikolas
Thu 7 June 2007, 07:06 pm GMT +0200
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 ;)

Meth0d
Thu 7 June 2007, 07:08 pm GMT +0200
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..

Archive for SMF v1.00 by N.P. Valid XHTML 1.0 Transitional