Forum Moderators: coopster

Message Too Old, No Replies

Need help with random php string please.

         

Simone100

9:48 am on Sep 3, 2006 (gmt 0)

10+ Year Member



Hello, I would like to change some php to calling on something by day.
The one I have only calls on something randomly. Do you know how I can
alter this string to accomodate this? Thanks!

$banner_no = (rand()%(count($s_con)-1));

Just so you know what the above does, gets a random number which is in the content of my file. for instance if I have
1-some text
2-different text
3-some other text

It grabs one of these numbers and the text along with it to display randomly. I would like it to display by day and in order instead.

UserFriendly

4:41 pm on Sep 3, 2006 (gmt 0)

10+ Year Member



Could you use modulo arithmetic instead?

So it would be:

$day = 3 + floor(mktime() / (60 * 60 * 24));
$chosen = $day % 7;

if you wanted $chosen to take a value from 0 to 6 representing Sunday to Saturday.

I've tested that and it seems to work. Change the % 7 to % $number_of_options to suit you.

[edited by: UserFriendly at 4:58 pm (utc) on Sep. 3, 2006]

smatts9

5:45 pm on Sep 3, 2006 (gmt 0)

10+ Year Member



or $day=date(j); ? Would give you a number 1-31 for whichever day it is.

inquireorenquire

4:55 am on Sep 4, 2006 (gmt 0)

10+ Year Member



If I get this right, you want it to have the same random value all day, but to change the next day?


<?php
$seed = floor(time()/24*60*60); //number of days since 1970
srand($seed); //seed the random number generator with this
$item = (rand()%(count($s_con)-1)); //as previously
?>

By setting the seed of the random numer generator before using rand() you should be able to fix the number for a given seed, by using the seed as the day number you then fix the value of rand() for the whole day.

Simone100

5:41 am on Sep 4, 2006 (gmt 0)

10+ Year Member



Gosh I don't know which one to try, maybe I better answer the last question and hopefully you can tell me which one is best suited for me.

Yes I only want it to change once per day. And for it to change to the next one in sequential number order, the next day. But having a different one for 31 days would be nice. Thank you very much for your help!

smatts9

6:26 am on Sep 4, 2006 (gmt 0)

10+ Year Member



set
$banner_no = date("j");

This will set $banner_no = to the number of whichever day it is (1-31) and will start back at one at the first of every month.