Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2011-07-03 10:13:13

jelle
Member
Registered: 2006-06-07
Posts: 165

[Solved]Random daily article

Hi,

I want my site to randomly pick and display an article from the database on a daily basis.

EDIT: Solved it with the following code.

<txp:php>
  variable(array('name' => 'day', 'value' => date('z')));
 </txp:php>

<txp:article limit="1" sort='rand(<txp:variable name="day"/>)'>

The PHP generates a random value based on todays day of the year (0 – 365) and places it in the textpattern variable
“day”. The variable is passed to the random sort option of an article tag.

Last edited by jelle (2011-07-03 21:12:12)

Offline

#2 2011-07-07 16:09:17

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: [Solved]Random daily article

jelle wrote:

The PHP generates a random value based on todays day of the year (0 – 365) and places it in the textpattern variable

Excellent tip. Glad you found an answer to your question and shared it. That really elegant and simple solution :-)

Hmm, but what about next year? The same seeds are repeated annually. Might not be an issue, but the results for each day of the year will reoccur.

That should be easily avoidable. Instead of using current day of the year, one could use UNIX timestamp for the last midnight as the seed. That can be done by using strtotime() with date/time string of today. For example:

<txp:variable name="day" value='<txp:php> echo strtotime("today"); </txp:php>' />

Offline

Board footer

Powered by FluxBB