TP-Docs
HTML5 Icon HTML5 Icon HTML5 Icon
TP on Social Media

Recent

Welcome to TinyPortal. Please login or sign up.

Members
  • Total Members: 3,973
  • Latest: NonStop
Stats
  • Total Posts: 196,234
  • Total Topics: 21,353
  • Online today: 639
  • Online ever: 8,223 (February 19, 2025, 04:35:35 AM)
Users Online
  • Users: 0
  • Guests: 216
  • Total: 216

Random Post Block -- Advice Requested

Started by seelie, December 07, 2006, 02:55:45 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

seelie

Greets! 

Previously, on one of the sites I manage, it was SMF and MKPortal.  I recently moved to TinyPortal ... One of the really popular features of the old software though, was the "Quotes" section.  User submitted quotes that displayed in page form and block.

Now, I've figured out a way to imitate it, after a fashion.  I created a Board and restricted it to "Replies  Only".  Then created one thread in which folks could reply to with their quotes ...

All I really need now is a block that displays random post replies from that specific thread.   I'll probably end up making into a center block, as it will have more room.  As far as display format of the actual post within the block, I think I'd like it similar to how "Recent Posts" does it (with the new! gif for new replies) but without the author's name, or the title of the parent thread.  Just the text of the single post reply.

I've tried to cobble together a couple things on my test site ... and have failed miserably thus far.

Any thoughts ideas?

Thanks :)

JPDeni

I'm not sure what you're looking for. Do you need help with the code to pull out a random post from the topic? Or are you looking for information about how to format it? Or.... ?  :)

What have you done that has failed?

seelie

Basically, I'm just looking for help / actual code on how to pull a random post from that specific topic.  I tried poking about with variations of the recent topics function ... and well, I'm honestly still a newb at coding *gets shifty eyed*   :o :-\


JPDeni

Okay. :)

Try this:


$topic_number = 123; // Replace this with the number of the topic where your quotes are
$query = db_query(
  "SELECT *
   FROM {$db_prefix}messages
   WHERE ID_TOPIC = $topic_number 
   ORDER BY rand() LIMIT 1", __FILE__, __LINE__);
$randomquote = mysql_fetch_assoc($query);


At that point, you'll have all of the information about that post in an array called $randomquote. The text will be in $randomquote['body'].

The other things that will be in the array are
$randomquote['ID_MSG'] -- the number of the message
$randomquote['posterTime'] -- the Unix time that it was posted
$randomquote['ID_MEMBER'] -- the member number of the poster
$randomquote['posterName'] -- the name of the poster
$randomquote['posterEmail'] -- the email address of the poster

When you print it out, you might want to send it through the bbc parser, in case there's formatting --


require_once('Subs.php');
echo parse_bbc($randomquote['body']);


This doesn't do the "new" thing, but if you're going display random quotes, I'm not sure whether that's really all that helpful.

seelie

ooooooo!

Going to go try that now.   Back in a few. :)

seelie

Meh.

I get reminded everyday how dense I can be :(

Ok.  I placed this at the bottom of SSI.php

function ssi_randomquote()
{

$topic_number = 3; // Replace this with the number of the topic where your quotes are
$query = db_query(
  "SELECT *
   FROM {$db_prefix}messages
   WHERE ID_TOPIC = $topic_number 
   ORDER BY rand() LIMIT 1", __FILE__, __LINE__);
$randomquote = mysql_fetch_assoc($query);
}


But when I create the PHP block and activiate it ... it bombs out.  I do believe I'm missing an array declaration or somesuch thingie somewhere?

JPDeni

You need to have

global $db_prefix;

as the first line.

Also, that won't get you much. There's nothing there to print it out.  :)

If you just want the post to print out, here's the code I would use:

function ssi_randomquote()
{
global $db_prefix;
$topic_number = 3; // Replace this with the number of the topic where your quotes are
$query = db_query(
  "SELECT *
   FROM {$db_prefix}messages
   WHERE ID_TOPIC = $topic_number 
   ORDER BY rand() LIMIT 1", __FILE__, __LINE__);
$randomquote = mysql_fetch_assoc($query);
echo parse_bbc($randomquote['body']);
}


If you get an error saying that the function parse_bbc can't be found, you'll need to use the require_once statement I had before, but I don't think it'll be a problem.

seelie

That works perfectly!  Now all I have to do is stick a link in there that will allow the user to "Add new Quote" ... which is just actually the link to reply to the topic ...

*goes off to munge around in php again*  :2funny:

JPDeni

#8
The link would be something like


echo '<a href="' . $scripturl . '?action=post;topic=' . $topic_number .  '">Add a quote</a>


You'll also have to add $scripturl to the "global" statement at the beginning of the code.

So the whole thing would be


function ssi_randomquote()
{
global $db_prefix,$scripturl;
$topic_number = 3; // Replace this with the number of the topic where your quotes are
$query = db_query(
  "SELECT *
   FROM {$db_prefix}messages
   WHERE ID_TOPIC = $topic_number 
   ORDER BY rand() LIMIT 1", __FILE__, __LINE__);
$randomquote = mysql_fetch_assoc($query);
echo parse_bbc($randomquote['body']);
echo '<br />';
echo '<a href="' . $scripturl . '?action=post;topic=' . $topic_number .  '">Add a quote</a>';
}


Gargoyle

This looks really cool... I am confused though as to what gets added to ssi.php and what gets put into the php block.

Could one of you help me out with that ?? I would love to implement this one!

This website is proudly hosted on Crocweb Cloud Website Hosting.