Page 1 of 1

Request: Display random entry

Posted: Wed Jan 31, 2007 6:47 pm
by gibtsNicht
Hello,

today I had the idea to display a random entry on the sidebar (like on the right side of www.ehrensenf.de) but it seems like there is no such Plugin right now.

Perfect would be a display with the linked title of the entry and the first x letters or words.

And in the backend it would be fine to have some filters (e.g. only out of the last x months, only the following catgories and so on).

I'm thinking about to have a deeper look in the plugin API but maybe someone else has fun to make such or plugin or has done something like this.

Re: Request: Display random entry

Posted: Thu Feb 01, 2007 10:56 am
by garvinhicking
Hi!

We have two plugins that come close - the 'recent entries' sidebar plugin (serendipity_plugin_recententries), and the 'History' plugin (serendipity_plugin_history).

However, both cannot randomly return an entry. Random matches can be solved using MySQL, but not pgsql or using SQLite, as far as I know. So it's hard to solve while being compatible too all the Databases we support...

Best regards,
Garvin

Re: Request: Display random entry

Posted: Fri Feb 02, 2007 3:48 pm
by gibtsNicht
Maybe we would need two accesses to the DB: First to get an array of all entry-IDs, then randomly choose one of this array-entries and then the second with the now known single Entry-ID.

I will see if I'll have some time this weekend to try this out.

Re: Request: Display random entry

Posted: Fri Feb 02, 2007 4:01 pm
by garvinhicking
Hi!

But fetching all entry IDs would mean to return a very large database set for blogs having several hundred entries, so this would be a huge impact onto the database...

So if you try that, check the query execution times :)

Best regards,
Garvin

Posted: Sun Feb 18, 2007 8:16 pm
by meine-erde
I don't know whether this statements are all supported by Postgres or SQLite, but one could first get the max and min id of the posts

Code: Select all

SELECT
    MIN(id) as id_min,
    MAX(id) as id_max
FROM `serendipity_entries`
WHERE 1;
Knowing these values, one could generate a random float between 0 and 1 in php (which acts as a percentage). Using a second query, one could get the random entry by using

Code: Select all

SELECT id
FROM `serendipity_entries`
WHERE id >= id_min + (id_max - id_min) * random_value
ORDER BY id ASC
LIMIT 1
There have to be checks, whether the entry really is public and stuff, but I think, that should do. Maybe one could also connect these queries into one. But I got no nerve today to do that...

Cheers, Holger

Posted: Sun Feb 18, 2007 11:17 pm
by stm999999999
just a little suggestion:

there is a plugin for "next/prev article" and additionaly "random article".

perhaps a look cannot be wrong?

Posted: Tue Feb 20, 2007 11:09 am
by garvinhicking
Hi!

That "random" thing uses a MySQL-only syntax. ;)

Best regards,
Garvin