How to post 1 entry, randomly selected from only the latest 3 entries? 
Posted: 09 May 2008 06:52 PM   [ Ignore ]  
Grad Student
Avatar
Rank
Total Posts:  77
Joined  2002-06-01

I’m trying to figure out a way to post only one entry, which is randomly picked from the last three entries from a specific category.  I have a feeling the answer is very simple, but I’m having a difficult time figuring this out.

Profile
 
 
Posted: 12 May 2008 07:27 PM   [ Ignore ]   [ # 1 ]  
Grad Student
Avatar
Rank
Total Posts:  77
Joined  2002-06-01

Is there no easy way to do this?

Profile
 
 
Posted: 12 May 2008 07:33 PM   [ Ignore ]   [ # 2 ]  
Summer Student
Total Posts:  20
Joined  2008-04-15

I would be interested in the answer to this - I wanted to use it for a latest random product or random recent testimonials.

I’m thinking it could be done by pulling three recent entries from the weblog then pulling the data from that with a limit of one - like yourself I am not quite sure how would be the easiest way to tackle this.

 Signature 

vibes

Profile
 
 
Posted: 13 May 2008 01:33 AM   [ Ignore ]   [ # 3 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  11077
Joined  2004-05-15
paulp - 09 May 2008 06:52 PM

I’m trying to figure out a way to post only one entry, which is randomly picked from the last three entries from a specific category.  I have a feeling the answer is very simple, but I’m having a difficult time figuring this out.

I don’t think it is very simple. Can it be done? Of course. With built-in EE functionality? I don’t think so. We need some code, so let’s break out PHP:

<?php global $DB;

$query = 'SELECT * FROM `exp_weblog_titles`
LEFT JOIN `exp_weblog_data` ON exp_weblog_titles.entry_id = exp_weblog_data.entry_id
LEFT JOIN `exp_category_posts` ON exp_weblog_titles.entry_id = exp_category_posts.entry_id
WHERE cat_id = "127"
ORDER BY entry_date DESC LIMIT 3'
;

$entry_ids = $DB->query($query)->result;
$i = rand(0,2);

echo
$entry_ids[$i]['entry_id'];
echo
$entry_ids[$i]['title'];
echo
$entry_ids[$i]['field_id_2'];

?>

That should get you started. Probably requires PHP 5, and you’ll have to adjust the cat_id and probably the output somewhat.

 Signature 

Everything will be good in the end. If it’s not good, it’s not the end.
No PM’s, please—use regular mail.

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 1149, on July 16, 2007 10:33 AM
Total Registered Members: 58134 Total Logged-in Users: 25
Total Topics: 68142 Total Anonymous Users: 10
Total Replies: 366992 Total Guests: 422
Total Posts: 435134    
Members ( View Memberlist )
Newest Members:  Doug LernerATATJacob SmithalixJonathon VSgoodbergerDelia RitherdonbborunXofzainabob