TinyPortal

Development => Block Codes => Topic started by: seelie on December 07, 2006, 02:55:45 PM

Title: Random Post Block -- Advice Requested
Post by: seelie on December 07, 2006, 02:55:45 PM
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 :)
Title: Re: Random Post Block -- Advice Requested
Post by: JPDeni on December 07, 2006, 02:59:16 PM
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?
Title: Re: Random Post Block -- Advice Requested
Post by: seelie on December 07, 2006, 03:02:24 PM
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 :-\

Title: Re: Random Post Block -- Advice Requested
Post by: JPDeni on December 07, 2006, 03:19:24 PM
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.
Title: Re: Random Post Block -- Advice Requested
Post by: seelie on December 07, 2006, 03:45:19 PM
ooooooo!

Going to go try that now.   Back in a few. :)
Title: Re: Random Post Block -- Advice Requested
Post by: seelie on December 07, 2006, 04:01:35 PM
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?
Title: Re: Random Post Block -- Advice Requested
Post by: JPDeni on December 07, 2006, 05:31:12 PM
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.
Title: Re: Random Post Block -- Advice Requested
Post by: seelie on December 07, 2006, 08:00:24 PM
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:
Title: Re: Random Post Block -- Advice Requested
Post by: JPDeni on December 08, 2006, 12:50:32 AM
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>';
}

Title: Re: Random Post Block -- Advice Requested
Post by: Gargoyle on December 08, 2006, 02:02:52 AM
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!
Title: Re: Random Post Block -- Advice Requested
Post by: Thurnok on December 08, 2006, 04:26:14 AM
The original requestor placed JP Deni's code into the SSI.php file.  After doing that, you simply create a phpblock with the following:
ssi_randomquote();

However, you can put all of JP Deni's code into a phpblock itself without having to modify your SSI.php file.  The pros and cons in my opinion are:

Title: Re: Random Post Block -- Advice Requested
Post by: seelie on December 08, 2006, 03:49:18 PM
I am exceptionally pleased with how this turned out.  :up: :up:   And the site users rejoiced mightily ;)  If only to were so easy to make them happy with the Gallery ....  :o 
Title: Re: Random Post Block -- Advice Requested
Post by: JPDeni on December 08, 2006, 04:25:34 PM
I'm so glad that it worked out for you. :) And users rejoicing is always good.
Title: Re: Random Post Block -- Advice Requested
Post by: Gargoyle on December 09, 2006, 12:00:24 AM
I tried placing the code into a php block and I got errors...

Any idea what I am doing wrong ?
Title: Re: Random Post Block -- Advice Requested
Post by: Thurnok on December 09, 2006, 12:05:16 AM
If you put in a TP Block, do not use the function around the code.  In otherwords:

function ssi_randomquote(){

// all the various code is here

}


Make it instead:


// all the various code here

Title: Re: Random Post Block -- Advice Requested
Post by: Gargoyle on December 09, 2006, 12:12:38 AM
Still getting parse code errors...

Does it need echo statements ?
Title: Re: Random Post Block -- Advice Requested
Post by: Gargoyle on December 09, 2006, 12:18:13 AM
Found it... The code was missing a '; at the end.. ;D

Thanks!!!
Title: Re: Random Post Block -- Advice Requested
Post by: deniz on December 23, 2006, 02:44:36 PM
i need random topic with first message and few words..
can you modifiy this code ??
Title: Re: Random Post Block -- Advice Requested
Post by: JPDeni on December 23, 2006, 04:10:13 PM
Quoterandom topic with first message and few words..
How many is "few"? Can you be more explicit with what you want?
Title: Re: Random Post Block -- Advice Requested
Post by: deniz on December 23, 2006, 07:31:35 PM
about 200-300 words from first message :)
Title: Re: Random Post Block -- Advice Requested
Post by: JPDeni on December 23, 2006, 08:25:34 PM
I just realized that this is a completely different thing than you want. The code for this topic is "Pull a random post from a specified topic." You want "Pull the first post from a random topic."

So, to answer your first question, no, I can't modify this code to do what you want. :)

But I can modify one of the functions in SSI.php to do what you want. Put the following in a php block:


global $scripturl, $db_prefix, $txt, $settings, $modSettings, $context;
global $func;
$length= 300;

loadLanguage('Stats');

if ($length === null)
$length = isset($_GET['length']) ? (int) $_GET['length'] : 0;
else
$length = (int) $length;

// Make sure guests can see this board.
$request = db_query("
SELECT ID_BOARD
FROM {$db_prefix}boards
WHERE FIND_IN_SET(-1, memberGroups)
        ORDER BY rand()
LIMIT 1", __FILE__, __LINE__);

list ($board) = mysql_fetch_row($request);
mysql_free_result($request);

// Load the message icons - the usual suspects.
$stable_icons = array('xx', 'thumbup', 'thumbdown', 'exclamation', 'question', 'lamp', 'smiley', 'angry', 'cheesy', 'grin', 'sad', 'wink', 'moved', 'recycled', 'wireless');
$icon_sources = array();
foreach ($stable_icons as $icon)
$icon_sources[$icon] = 'images_url';

// Find the post ids.
$request = db_query("
SELECT ID_FIRST_MSG
FROM {$db_prefix}topics
WHERE ID_BOARD = $board
ORDER BY rand()
LIMIT 1", __FILE__, __LINE__);
$posts = array();
while ($row = mysql_fetch_assoc($request))
$posts[] = $row['ID_FIRST_MSG'];
mysql_free_result($request);

// Find the posts.
$request = db_query("
SELECT
m.icon, m.subject, m.body, IFNULL(mem.realName, m.posterName) AS posterName, m.posterTime,
t.numReplies, t.ID_TOPIC, m.ID_MEMBER, m.smileysEnabled, m.ID_MSG, t.locked
FROM ({$db_prefix}topics AS t, {$db_prefix}messages AS m)
LEFT JOIN {$db_prefix}members AS mem ON (mem.ID_MEMBER = m.ID_MEMBER)
WHERE t.ID_FIRST_MSG IN (" . implode(', ', $posts) . ")
AND m.ID_MSG = t.ID_FIRST_MSG
LIMIT 1", __FILE__, __LINE__);
$return = array();
while ($row = mysql_fetch_assoc($request))
{
// If we want to limit the length of the post.
if (!empty($length) && $func['strlen']($row['body']) > $length)
{
$row['body'] = $func['substr']($row['body'], 0, $length);
// The first space or line break. (<br />, etc.)
        $cutoff = max(strrpos($row['body'], ' '), strrpos($row['body'], '<'));

if ($cutoff !== false)
$row['body'] = $func['substr']($row['body'], 0, $cutoff);
$row['body'] .= '...';
}

$row['body'] = parse_bbc($row['body'], $row['smileysEnabled'], $row['ID_MSG']);

// Check that this message icon is there...
if (empty($modSettings['messageIconChecks_disable']) && !isset($icon_sources[$row['icon']]))
$icon_sources[$row['icon']] = file_exists($settings['theme_dir'] . '/images/post/' . $row['icon'] . '.gif') ? 'images_url' : 'default_images_url';

censorText($row['subject']);
censorText($row['body']);

$return[] = array(
'id' => $row['ID_TOPIC'],
'message_id' => $row['ID_MSG'],
'icon' => '<img src="' . $settings[$icon_sources[$row['icon']]] . '/post/' . $row['icon'] . '.gif" align="middle" alt="' . $row['icon'] . '" border="0" />',
'subject' => $row['subject'],
'time' => timeformat($row['posterTime']),
'timestamp' => forum_time(true, $row['posterTime']),
'body' => $row['body'],
'href' => $scripturl . '?topic=' . $row['ID_TOPIC'] . '.0',
'link' => '<a href="' . $scripturl . '?topic=' . $row['ID_TOPIC'] . '.0">' . $row['numReplies'] . ' ' . ($row['numReplies'] == 1 ? $txt['smf_news_1'] : $txt['smf_news_2']) . '</a>',
'replies' => $row['numReplies'],
'comment_href' => !empty($row['locked']) ? '' : $scripturl . '?action=post;topic=' . $row['ID_TOPIC'] . '.' . $row['numReplies'] . ';num_replies=' . $row['numReplies'],
'comment_link' => !empty($row['locked']) ? '' : '<a href="' . $scripturl . '?action=post;topic=' . $row['ID_TOPIC'] . '.' . $row['numReplies'] . ';num_replies=' . $row['numReplies'] . '">' . $txt['smf_news_3'] . '</a>',
'new_comment' => !empty($row['locked']) ? '' : '<a href="' . $scripturl . '?action=post;topic=' . $row['ID_TOPIC'] . '.' . $row['numReplies'] . '">' . $txt['smf_news_3'] . '</a>',
'poster' => array(
'id' => $row['ID_MEMBER'],
'name' => $row['posterName'],
'href' => !empty($row['ID_MEMBER']) ? $scripturl . '?action=profile;u=' . $row['ID_MEMBER'] : '',
'link' => !empty($row['ID_MEMBER']) ? '<a href="' . $scripturl . '?action=profile;u=' . $row['ID_MEMBER'] . '">' . $row['posterName'] . '</a>' : $row['posterName']
),
'locked' => !empty($row['locked']),
);
}
mysql_free_result($request);

foreach ($return as $news)
{
echo '
<div>
<a href="', $news['href'], '">', $news['icon'], '</a> <b>', $news['subject'], '</b>
<div class="smaller">', $news['time'], ' ', $txt[525], ' ', $news['poster']['link'], '</div>

<div class="post" style="padding: 2ex 0;">', $news['body'], '</div>

', $news['link'], $news['locked'] ? '' : ' | ' . $news['comment_link'], '
</div>';
}
Title: Re: Random Post Block -- Advice Requested
Post by: deniz on December 23, 2006, 09:42:23 PM
You are hero ..   :up:

That is exactly what i want .. thank you very much   ::)
Title: Re: Random Post Block -- Advice Requested
Post by: RvG on December 24, 2006, 11:36:00 AM
Thanks JP. :)

It works perfectly...