Login  |  Register
HTML5 Icon HTML5 Icon HTML5 Icon
TP on Social Media

Recent

Welcome, Guest. Please login or register.
Did you miss your activation email?

August 12, 2022, 05:34:42 PM

Login with username, password and session length
Members
  • Total Members: 3842
  • Latest: Kokoro
Stats
  • Total Posts: 193479
  • Total Topics: 21088
  • Online today: 50
  • Online ever: 1035
  • (November 26, 2020, 04:45:12 AM)
Users Online
Users: 0
Guests: 45
Total: 45

Author Topic: [Discussion] Random topic  (Read 42056 times)

0 Members and 1 Guest are viewing this topic.

JPDeni

  • Guest
[Discussion] Random topic
« on: May 05, 2009, 09:33:13 AM »
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.

Code: [Select]
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);

« Last Edit: October 18, 2010, 10:45:52 AM by Freddy »

alving

  • Guest
Re: [BLOCK] Random topic
« Reply #1 on: May 05, 2009, 06:36:48 PM »
thanks heaps, Jpdeni! 

alving

  • Guest
Re: [BLOCK] Random topic
« Reply #2 on: August 06, 2009, 06:18:32 AM »
jpdeni, is it possible to include the posters' avatar on each of the randomly posted topics?  thanks in advance!  :)

JPDeni

  • Guest
Re: [BLOCK] Random topic
« Reply #3 on: August 06, 2009, 06:50:35 AM »
I'm sure that it is possible, but I don't know how to do it. I don't do avatars. Sorry.

alving

  • Guest
Re: [BLOCK] Random topic
« Reply #4 on: August 08, 2009, 09:27:27 PM »
can anyone please help?

thanks in advance

Offline ZarPrime

  • Friends
  • *
  • Posts: 4653
Re: [BLOCK] Random topic
« Reply #5 on: August 09, 2009, 03:39:30 AM »
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

Offline Freddy

  • Moderator
  • *
  • Posts: 1615
    • AiDreams
Re: [BLOCK] Random topic
« Reply #6 on: August 09, 2009, 06:18:57 AM »
Working on this...

Offline Freddy

  • Moderator
  • *
  • Posts: 1615
    • AiDreams
Re: [BLOCK] Random topic
« Reply #7 on: August 09, 2009, 07:01:22 AM »
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.

Code: [Select]
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.
« Last Edit: August 09, 2009, 07:16:33 AM by freddy888 »

Offline ZarPrime

  • Friends
  • *
  • Posts: 4653
Re: [BLOCK] Random topic
« Reply #8 on: August 09, 2009, 10:19:42 AM »
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

Offline Freddy

  • Moderator
  • *
  • Posts: 1615
    • AiDreams
Re: [BLOCK] Random topic
« Reply #9 on: August 09, 2009, 12:22:42 PM »
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.