/    Sign up×
Community /Pin to ProfileBookmark

array order depending from day

I’m gonna add to my site couple of messages stored in array and I like to they show in exact order, different for every another day. Let’s say I’ll have 10 messages and one day I want to they be in order from 1st to last:

[I]message 1
message 2

message 10[/I]

Another day:

[I]message 10
message 1

message 9[/I]

Day after another day:

[I]message 9
message 10

message 8[/I]

and so on (in cycle).

I need to those messages update when date is change – every day last array entry be show as 1st array.

Any example how to make it, please?

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@NogDogApr 06.2009 — One possibility (sequence may jump a bit between Dec. 31 and Jan. 1):
[code=php]
<?php
$messages = array(
'one',
'two',
'three',
'four',
'five',
'six',
'seven',
'eight',
'nine',
'ten'
);
$numMsg = count($messages);
$day = date('z');
$num = $day &#37; $numMsg;
for($i=$num; $i < $numMsg; $i++)
{
echo "<p class='message'>".$messages[$i]."</p>n";
}
if($num != 0)
{
for($i = 0; $i < $num; $i++)
{
echo "<p class='message'>".$messages[$i]."</p>n";
}
}
[/code]
Copy linkTweet thisAlerts:
@AnnaccondauthorApr 06.2009 — Thank you very much, I'll try it ?

EDIT:

I got some error in line "$num = $day &#37; $numMsg;" I removed "&#37" and now messages show in order 1 - 10 but I don't know will they rotate tomorrow. I don't understand also how this part of script: "$day = date('z');" works.
Copy linkTweet thisAlerts:
@DasherApr 06.2009 — I do it quite similarly;

[code=php]
$quotes = array(
'one',
'two',
'three',
'four',
'five',
'six',
'seven',
'eight',
'nine',
'ten'
);

$numinarray = count($quote); // auto increase the modulus as quotes added.

$selected_quote = date ("z") % $numinarray; // modulus divider = number of elements in array. z is day of the year

$quote = stripcslashes($quote[$selected_quote]);

echo "$quote";[/code]
Copy linkTweet thisAlerts:
@AnnaccondauthorApr 06.2009 — I gave a try Dasher's version but it shows error as well:

Warning: Division by zero in
[CODE]$selected_quote = date ("z") % $numinarray;[/CODE]

Any idea why? Maybe because of "%" ?
Copy linkTweet thisAlerts:
@NogDogApr 06.2009 — I don't know why my code showed that & #37;, but it should have been just a percent sign (for modulus).
[code=php]
$num = $day%$numMsg;
[/code]

As to your question, [b]date('z')[/b] gets the current day of the year (0 - 365).
Copy linkTweet thisAlerts:
@AnnaccondauthorApr 07.2009 — Now it seems to work. Thank you NogDog ?
×

Success!

Help @Annaccond 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 5.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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...