Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
#1 2012-01-14 11:10:55
- phuture303
- Member
- Registered: 2008-09-14
- Posts: 127
Keep Variable for 24 hours (and then change it)
Hi,
I’m running a website for collegiate golfers. Actually, there are only two, but when the season starts, there will be up to 100-120 people published in the leaderboard. Every player has his own little profile with photo, handicap, and played tournaments.
What I’d like to do is to present a “player of the day” (randomly, just for fun).
What I CAN do I: Take a random-id and publish the randomly chosen article in my sidebar. But then it will change after every reload or new loaded pages of my website. So it’s not really the “player of the day”, but “player of this special web page and gone after reload”. Fail :-)
What I CAN do II: Choose every morning at 9am a random player and put the article-id into an custom field or a variable. But this is not really an acceptable procedure :-)
What I WANT my website to do for me: Dive into the pool of all players (section: player, status: live, etc.), choose a random article-id out of this pool, store this id into a variable – and publish this “player of the day” with his name and article-image in my sidebar. This player should visible from 9am to 8:59 am (24-hour-cycle), and the next cycle someone else will appear. (Ok, there are chances for having the same person again next day (and again and again), but i will take this risk. I simply won’t have enough players to appear only once per year :-)
Are there any chances for changing a variable randomly out of my players-pool “only” every 24-hours?
I don’t really have any knowlege in PHP or cookie-settings. Maybe there is a pro in here who can help! Or maybe my idea is simply stupid. Please be honest :-)
Thanks a lot!
David
Offline
Re: Keep Variable for 24 hours (and then change it)
I think you could try aks_cache to begin with.
Something like this somewhere at the top of your page templates (for example, in a form that is included on every page template).
<txp:variable name="random_player_id">
<txp:aks_cache id="" block="random_player_id" hour="24">
<txp:article_custom section="golfers" sort="rand()" limit="1">
<txp:article_id />
</txp:article_custom>
</txp:aks_cache>
</txp:variable>
This code will: ask for a random article in section “golfers”, and “print” its id inside a block to be cached by aks_cache during 24 hours. Then, on every page request, this cached block (which will just be a plain number, like “123”) will be assigned to a txp:variable, just for make it easier to re-use it across your website, in different places where you want this random id to be used.
So, then… down on your pages:
<txp:article_custom id='<txp:variable name="random_player_id" />'>
... your txp tags...
</txp:article_custom>
That would achieve half of what you have requested. Then, there is the next step: refresh this cached random id it at 9am, each day.
I think another aks_ plugin could help you there: aks_cron
I haven’t used it, but it seems it lets you add some cron jobs.
On that cron job, you could just run this function:
update_lastmod();
And it should clean/recreate the cache on next hit to the website (you have to check that “Reset cache if site was updated” is enabled in “Extensions -> aks_cache”).
If you try all this, let us know if it worked :)
Offline
#3 2012-01-14 14:00:33
- phuture303
- Member
- Registered: 2008-09-14
- Posts: 127
Re: Keep Variable for 24 hours (and then change it)
maniqui schrieb:
I think you could try aks_cache to begin with.
Hey maniqui,
thank you so much for that quick answer – and thanks for pointing me into the right direction! I tried to follow your tips but had some problems with aks_cron; maybe my server config doesn’t allow to use it (or I’m just dumb for it). But: no problem at all, at the aks_cache page I’ve found the perfect tip – no aks_cron needed!
<txp:aks_cache id="player-random" hour="24" noreset="1">
<txp:article_custom section="player" limit="1" sort="rand()"><txp:title /><txp:article_image /></txp:article_custom>
</txp:aks_cache>
Well, that was easy! It’s important to set noreset="1"
to keep the actual player in the cache if you’re updating your site. For testing I didn’t use hour="24"
but min="1"
because I didn’t want to wait that long… And it worked perfectly so I believe that i will do the same in a 24-hour-rhythm as well. All I have to do is to reset the cache once at 9am, from then on it will hopefully do its job every day at the same time :-)
David, very glad.
Last edited by phuture303 (2012-01-14 14:06:50)
Offline
#4 2012-01-14 16:20:50
- phuture303
- Member
- Registered: 2008-09-14
- Posts: 127
Re: Keep Variable for 24 hours (and then change it)
Well, after having a short break for shopping something came suddenly in my mind, somewhere between coffee, bread and frozen pizza:
Without any cronjob whichs calls my website every 24 hours, the randomly posted player won’t change automatically if no one comes to my website – so a player would be “internally cached” as “player of two days” if there are zero visitors for e.g. 48 hours – am I right? The aks_cache helps to save my random player for a minimum amout of time but not for a maximum (which was my personal error in reasoning).
The golf team an I have to discuss this here but my idea is to publish the “player of the day” every day on twitter and/or facebook. But if there’s no change of the player every day at 9am there’s no change e.g. in my RSS-Feed which pulls the trigger for Twitter to publish the message.
I’ve had my problems with aks_cron, but there’s a free-service calls cronjob.de. There I built a little cronjob which calls my website every 24 hours so that the cached “player of the day” has to change accurately every day at the same time. (And hopefully I’m right that now I can build a feed from that to tell the world that there’s a new player of the day.).
If I’m completely wrong please bring me back on track :-)
David
Offline
#5 2012-01-14 18:16:51
- uli
- Moderator
- From: Cologne
- Registered: 2006-08-15
- Posts: 4,315
Re: Keep Variable for 24 hours (and then change it)
Somebody reading here might also be interested in this solution.
In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links
Offline
Re: Keep Variable for 24 hours (and then change it)
uli wrote:
Somebody reading here might also be interested in this solution.
That works too. I.e.
<!--
Generate date based seed for rand() with strtotime
and store it in "day_seed" named variable.
-->
<txp:variable name="day_seed" value='<txp:php> echo strtotime("today"); </txp:php>' />
<!--
Use the day_seed variable with rand() in article/_custom tag's
sort attribute to provide daily changing random article
-->
<txp:article limit="1" sort='rand(<txp:variable name="day_seed"/>)' />
Last edited by Gocom (2012-01-14 19:33:32)
Offline
#7 2012-01-15 12:21:40
- phuture303
- Member
- Registered: 2008-09-14
- Posts: 127
Re: Keep Variable for 24 hours (and then change it)
Hi Uli & Gocom,
many thanks for that other idea and the ready-to-use-code! It works well (perfectly “day-fitted” from 0:00 to 23:59 h) and feels much more “stable” without using a cache and waiting for a cron to do its job :-)
If there are enough players on the leaderboard (randomizing has to make sense), I will implement this feature probably in the sidebar on my website. If anyone has an idea how to publish anything like “Player of the Day: Dailychangingname [with a link to the website]” on Twitter I’ll be glad to hear. Main problem for me seems that the daily changing players’ name (plus his photo) don’t result in a newly published article every day which i can put into a feed because only the source code of the sidebar is changing?!
Offline
#8 2012-02-03 08:29:02
- phuture303
- Member
- Registered: 2008-09-14
- Posts: 127
Re: Keep Variable for 24 hours (and then change it)
Hi,
the solution above is working perfectly, I’m just waiting for more players so that randomizing them makes sense.
But now I want to look behind the curtains and like to know WHY it works :-)
My variable <txp:variable name="day_seed" value='<txp:php> echo strtotime("today"); </txp:php>' />
brings a unique timestamp (e.g. for today: 1328223600). So far, so good.
This number is used in <txp:article_custom section="player" limit="1" sort='rand(<txp:variable name="day_seed"/>)' ><txp:article_image /></txp:article_custom>
to show the image of the “player of the day”. Okay!
The missing link for my limited brain is now the connection between rand(<txp:variable name="day_seed"/>
and how this catches a working article-ID. I think there’s a lack of understanding the random-function.
In other words: How/why does rand(1328223600)
lead to the article-ID 146?
Can anyone help? :-) Thanks!
David
Offline
Re: Keep Variable for 24 hours (and then change it)
phuture303 wrote:
In other words: How/why does
rand(1328223600)
lead to the article-ID 146?
The timestamp, 1328223600
, is used as a seed in the pseudo-random number generator algorithm. A (single) seed will always give a same random value. A seed is the whole thing that is used as the basis in the algorithm. As long as the timestamp stays the same (and thus the seed), you will get the same random article from the database.
When the day changes, you will get a different UNIX timestamp (for that day), and the algorithm gives out different results, selecting a different row from the database.
Offline
#10 2012-02-03 08:57:19
- phuture303
- Member
- Registered: 2008-09-14
- Posts: 127
Re: Keep Variable for 24 hours (and then change it)
Thank, Gocom! Now the bulb in my head shines a little brighter! (A clever and easy solution, I’m very thrilled about that :-)
Offline
Re: Keep Variable for 24 hours (and then change it)
Wouaou
thanks gocom for explanation.
I learned a new thing today!
Offline