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

Recent

Welcome to TinyPortal. Please login or sign up.

April 01, 2023, 12:07:42 AM

Login with username, password and session length
Members
Stats
  • Total Posts: 194,247
  • Total Topics: 21,147
  • Online today: 66
  • Online ever: 3,540
  • (September 03, 2022, 01:38:54 AM)
Users Online
Users: 1
Guests: 40
Total: 41

[Discussion] Random topic

Started by JPDeni, May 05, 2009, 04:33:13 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

JPDeni

Edit by Freddy : In the first part of this thread you will find code for SMF1.  If you want SMF 2 code then that can be found here...

Feel free to continue in this thread with discussion.  Cheers :)

==========================================

This is a continuation of the request for a block in this topic. I've given a few more options and compacted the code.

What it does: Displays the first post from random topics, rather like the "recent topics" functions. It will show topics from all boards that the user can see. You can limit it to one category of boards if you prefer. You can also limit the number of characters to display. You can set the number of posts.

Where to put it: In either a php block or a php article. If you want more than one topic to display, it would probably be best to put it into an article.


global $db_prefix, $txt, $user_info;

// If you want to limit it to one category, enter the category number here
// If you want to use all categories, set it to 0
$category = 0 ;

// Set how many posts you want to display
$limit = 5;

// If you want to limit the number of characters displayed, enter the length here
// If you want the whole post to be displayed, no matter what the length, set it to 0
$maxlength = 1000;

loadLanguage('Stats');

$result = db_query("
    SELECT m.ID_BOARD, 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, {$db_prefix}boards as b)
    LEFT JOIN {$db_prefix}members AS mem ON (mem.ID_MEMBER = m.ID_MEMBER)
  WHERE m.ID_MSG = t.ID_FIRST_MSG
  AND b.ID_BOARD = m.ID_BOARD
  AND $user_info[query_see_board] " .
  ($category > 0 ? "AND ID_CAT = $category" : "")
  . " ORDER BY RAND() LIMIT $limit", __FILE__, __LINE__);

while ($row = mysql_fetch_assoc($result)){
if ($maxlength > 0 && strlen($row['body']) > $maxlength)
{
$row['body'] = substr($row['body'], 0, $maxlength);
while (substr($row['body'],-1) <> ' ')
$row['body'] = substr($row['body'],0,-1);
$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']);

$news['icon'] = '<img src="' . $settings[$icon_sources[$row['icon']]] . '/post/' . $row['icon'] . '.gif" align="middle" alt="' . $row['icon'] . '" border="0" />';
$news['subject'] = $row['subject'];
$news['time'] = timeformat($row['posterTime']);
$news['body'] = $row['body'];
$news['href'] = $scripturl . '?topic=' . $row['ID_TOPIC'] . '.0';
$news['link'] = '<a href="' . $scripturl . '?topic=' . $row['ID_TOPIC'] . '.0">' . $row['numReplies'] . ' ' . ($row['numReplies'] == 1 ? $txt['smf_news_1'] : $txt['smf_news_2']) . '</a>';
$news['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>';
$news['poster'] = !empty($row['ID_MEMBER']) ? '<a href="' . $scripturl . '?action=profile;u=' . $row['ID_MEMBER'] . '">' . $row['posterName'] . '</a>' : $row['posterName'];
$news['locked'] = !empty($row['locked']);

echo '
  <div>
   <a href="', $news['href'], '">', $news['icon'], '</a> <b>', $news['subject'], '</b>
   <div class="smaller">', $news['time'], ' ', $txt[525], ' ', $news['poster'], '</div>
    <div class="post" style="padding: 2ex 0;">', $news['body'], '</div>
    ', $news['link'], $news['locked'] ? '' : ' | ' . $news['comment_link'], '<hr />
</div>';
}
mysql_free_result($result);


alving


alving

jpdeni, is it possible to include the posters' avatar on each of the randomly posted topics?  thanks in advance!  :)

JPDeni

I'm sure that it is possible, but I don't know how to do it. I don't do avatars. Sorry.

alving

can anyone please help?

thanks in advance

ZarPrime

ajg,

If you do a search in the block code snippets board for Avatar, you will find some snippets that display Avatars.  You may be able to look through some of those code snippets and use some of the code one of them uses and adapt it  to display the avatars in this block.

ZarPrime

Freddy


Freddy

#7
JP, just to let you know, you need $settings to be declared in your globals - otherwise the little post thingy icon doesn't show up I found .

Here's what I came up with.  I haven't done anything really to format the output, I left it to the end user to decide how best to display the avatar.  I just did the code to grab the avatar.

global $db_prefix, $txt, $user_info, $settings, $memberContext;

// If you want to limit it to one category, enter the category number here
// If you want to use all categories, set it to 0
$category = 0 ;

// Set how many posts you want to display
$limit = 5;

// If you want to limit the number of characters displayed, enter the length here
// If you want the whole post to be displayed, no matter what the length, set it to 0
$maxlength = 1000;

loadLanguage('Stats');

$result = db_query("
   SELECT m.ID_BOARD, 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, {$db_prefix}boards as b)
   LEFT JOIN {$db_prefix}members AS mem ON (mem.ID_MEMBER = m.ID_MEMBER)
 WHERE m.ID_MSG = t.ID_FIRST_MSG
 AND b.ID_BOARD = m.ID_BOARD
 AND $user_info[query_see_board] " .
 ($category > 0 ? "AND ID_CAT = $category" : "")
 . " ORDER BY RAND() LIMIT $limit", __FILE__, __LINE__);

while ($row = mysql_fetch_assoc($result)){
if ($maxlength > 0 && strlen($row['body']) > $maxlength)
{
$row['body'] = substr($row['body'], 0, $maxlength);
while (substr($row['body'],-1) <> ' ')
$row['body'] = substr($row['body'],0,-1);
$row['body'] .= ' ...';
}

// Get the avatar...
$memberID = $row['ID_MEMBER'];
loadMemberData($memberID);
loadMemberContext($memberID);
$news['avatar'] = $memberContext[$memberID]['avatar']['image'];

$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']);

$news['icon'] = '<img src="' . $settings[$icon_sources[$row['icon']]] . '/post/' . $row['icon'] . '.gif" align="middle" alt="' . $row['icon'] . '" border="0" />';
$news['subject'] = $row['subject'];
$news['time'] = timeformat($row['posterTime']);
$news['body'] = $row['body'];
$news['href'] = $scripturl . '?topic=' . $row['ID_TOPIC'] . '.0';
$news['link'] = '<a href="' . $scripturl . '?topic=' . $row['ID_TOPIC'] . '.0">' . $row['numReplies'] . ' ' . ($row['numReplies'] == 1 ? $txt['smf_news_1'] : $txt['smf_news_2']) . '</a>';
$news['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>';
$news['poster'] = !empty($row['ID_MEMBER']) ? '<a href="' . $scripturl . '?action=profile;u=' . $row['ID_MEMBER'] . '">' . $row['posterName'] . '</a>' : $row['posterName'];
$news['locked'] = !empty($row['locked']);

echo '
<div>' ,
$news['avatar'] , '
</div>
<div>
<a href="', $news['href'], '">', $news['icon'], '</a> <b>', $news['subject'], '</b>
<div class="smaller">', $news['time'], ' ', $txt[525], ' ', $news['poster'], '</div>
<div class="post" style="padding: 2ex 0;">', $news['body'], '</div>
   ', $news['link'], $news['locked'] ? '' : ' | ' . $news['comment_link'], '<hr />
</div>';
}
mysql_free_result($result);


You could put some padding round the avatar maybe, split it all into two halves or whatever.

ZarPrime

Hi freddy,

I figured you would pick up on this.  Good job. :up:

I knew that $memberContext was required in the globals but I had forgotten about $settings.  Keep up the good work. ;)

ZarPrime

Freddy

No problem Zar.  Funnily enough I was working on something the other day to grab the avatars myself for a mod I am working on.  Glad I could help.