Request: Display random entry

Creating and modifying plugins.
Post Reply
gibtsNicht
Regular
Posts: 11
Joined: Tue May 16, 2006 11:06 am
Location: Berlin, Germany
Contact:

Request: Display random entry

Post 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.
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: Request: Display random entry

Post 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
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
gibtsNicht
Regular
Posts: 11
Joined: Tue May 16, 2006 11:06 am
Location: Berlin, Germany
Contact:

Re: Request: Display random entry

Post 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.
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: Request: Display random entry

Post 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
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
meine-erde
Regular
Posts: 8
Joined: Sat Jan 13, 2007 1:52 pm
Location: Potsdam, Germany
Contact:

Post 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
stm999999999
Regular
Posts: 1531
Joined: Tue Mar 07, 2006 11:25 pm
Location: Berlin, Germany
Contact:

Post by stm999999999 »

just a little suggestion:

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

perhaps a look cannot be wrong?
Ciao, Stephan
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking »

Hi!

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

Best regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
Post Reply