/    Sign up×
Community /Pin to ProfileBookmark

Random text generator that updates every week

I will be given 50 different jokes.

I need to create a script that randomly chooses a joke and displays it for a [B]week[/B]. Switching jokes each week.

Can someone show me an example of how to go about creating this.

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@svidgenApr 30.2010 — So, are you looking to have these displayed in sequence then? Or, at the start of a new week, does the first page-view need to trigger a rand() call to pick a record for the week?
Copy linkTweet thisAlerts:
@porsche21authorApr 30.2010 — Yes, in sequence will work!
Copy linkTweet thisAlerts:
@svidgenApr 30.2010 — The simple version is like so:

[code=php]// populate this however you need/want.
// each record is a joke.
$jokes = array();

// grab the week-of-year.
$week = (int)date('W');

// if you wish to adjust this based on day-of-week, do that here.
// for instance, if we want the rollover on wednesday ... we might do:
if ((int)date('w') >= 3) {
$week++;
}

// grab the joke.
$todays_joke = $jokes[$week];[/code]


Make sense?
Copy linkTweet thisAlerts:
@porsche21authorApr 30.2010 — This is what I have but it doesn't give me a joke. Could you explain a little more what is happening with the week & date?

[code=php]
// populate this however you need/want.
// each record is a joke.
$jokes = array();

$jokes[] = 'joke1';
$jokes[] = 'joke2';
$jokes[] = 'joke3';
$jokes[] = 'joke4';
$jokes[] = 'joke5';
$jokes[] = 'joke6';
$jokes[] = 'joke7';

// grab the week-of-year.
$week = (int)date('W');

// if you wish to adjust this based on day-of-week, do that here.
// for instance, if we want the rollover on wednesday ... we might do:
if ((int)date('w') >= 3) {
$week++;
}

// grab the joke.
$todays_joke = $jokes[$week];
echo $todays_joke;

[/code]
Copy linkTweet thisAlerts:
@svidgenApr 30.2010 — Yeah, you won't see anything with that code as is, b/c date('W') should return 17, whereas your array has a max index of 6 (7 entries); So ... I guess I should have mentioned, to start mid-year, you'll need to offset the week--probably after adjusting for day-of-week:

[code=php]$week -= 17;[/code]

... in full:

[code=php]// populate this however you need/want.
// each record is a joke.
$jokes = array();

$jokes[] = 'joke1';
$jokes[] = 'joke2';
$jokes[] = 'joke3';
$jokes[] = 'joke4';
$jokes[] = 'joke5';
$jokes[] = 'joke6';
$jokes[] = 'joke7';

// grab the week-of-year.
$week = (int)date('W');

// if you wish to adjust this based on day-of-week, do that here.
// for instance, if we want the rollover on wednesday ... we might do:
if ((int)date('w') >= 3) {
$week++;
}

// suppose we started 2 weeks ago ...
$week -= 15;

// grab the joke.
$todays_joke = $jokes[$week];
echo $todays_joke; [/code]


Test for off-by-one errors of course. (I haven't)
Copy linkTweet thisAlerts:
@svidgenApr 30.2010 — [CODE]Could you explain a little more what is happening with the week & date?[/CODE]

date(str) returns a formatted date. W returns the week-of-year. w returns the day of week, 0 to 6. And again, test for off-by-one errors before putting this into production ?
Copy linkTweet thisAlerts:
@NogDogApr 30.2010 — You can use the modulus operator, e.g.:
[code=php]
$jokes = array(
"joke one",
"joke two",
"et cetera..."
);
echo $jokes[date('W') % count($jokes)];
[/code]
×

Success!

Help @porsche21 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...