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 15, 2022, 10:14:52 PM

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

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

0 Members and 1 Guest are viewing this topic.

alving

  • Guest
Re: [BLOCK] Random topic
« Reply #20 on: August 19, 2009, 05:05:52 PM »
no worries! cheers, mate

Offline Freddy

  • Moderator
  • *
  • Posts: 1615
    • AiDreams
Re: [BLOCK] Random topic
« Reply #21 on: August 20, 2009, 05:40:20 AM »
OK here goes...

There's some new configurations which are pretty self-explanatory but I will just mention them here.

$avatararea - sets the width of the avatar container - this is a percentage.  Depending on how wide your forum is you will probably need to play with this setting for the best and most even layout.

$avwidth, $avheight - override the actual size of the avatar image, should look OK if your avatars are mostly square.

$noavatarurl - currently set to look in the root directory - ie in the same location as your SMF index.php for a file called 'noavatar.jpg'.  Just change this to whereever you want to put it or just leave it alone.  You will need the 'no avatar' image copied to your root - I've attached a few versions - just change the name of the one you like to 'noavatar.jpg' and drop it in your forum root.

I tested this on a few themes and it seems to be fine.

Let me know how you get on :)

Code: [Select]
global $db_prefix, $txt, $user_info, $settings, $memberContext;

// Configuration...

// Percentage width of avatar area
$avatararea = 15;
// Colour of border around avatar
$avbordercol= '#fff';
// Default width and height of avatar
$avwidth = 75;
$avheight= 75;
// Path to 'no avatar' image.
$noavatarurl = 'noavatar.jpg';

// 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 and build the image link for it, if it exists...
$memberID = $row['ID_MEMBER'];
loadMemberData($memberID);
loadMemberContext($memberID);

        if (!empty($memberContext[$memberID]['avatar']['href']))
       {
       $news['avatar'] = '<img width="' . $avwidth . '" height="' . $avheight . '" style="border:1px solid ' . $avbordercol . ' ;padding: 2px" alt="' . $row['posterName'] . '" title="' . $row['posterName'] . '" src="' . $memberContext[$memberID]['avatar']['href'] . '" />';
        }
        else
        {
                 $news['avatar'] = '<img width="' . $avwidth . '" height="' . $avheight . '" style="border:1px solid ' . $avbordercol . ' ;padding: 2px" alt="' . $row['posterName'] . '" title="' . $row['posterName'] . '" src="' . $noavatarurl . '" />';
        }

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

// Display it all...
echo '
<div style="width:100%;display:table">
<div style="width: ' , $avatararea , '%;float:left;text-align:center">
               ' , $news['avatar'] , '
</div>
<div style="width: ' , 99 - $avatararea ,  '%;float:right">
<a href="', $news['href'], '">', $news['icon'], '</a> <strong>', $news['subject'], '</strong>
<div class="smaller">', $news['time'], '<br />' , $txt[525] , ' ' , $news['poster'], '</div>
</div>
<div class="post" style="padding: 2ex 0;clear:both">', $news['body'], '</div>
   ', $news['link'], $news['locked'] ? '' : ' | ' . $news['comment_link'], '<hr />
</div>';
}

mysql_free_result($result);
« Last Edit: August 20, 2009, 06:17:23 AM by freddy888 »

alving

  • Guest
Re: [BLOCK] Random topic
« Reply #22 on: August 20, 2009, 06:11:04 AM »
thanks freddy888!  just another thing, can you please include the word "by" before the username.  sorry, i know i asked for it to be deleted by it just doesn't look nice without it..  my bad.. thanks heaps, though.. maybe edit the above code so as not to confuse the others..

Offline Freddy

  • Moderator
  • *
  • Posts: 1615
    • AiDreams
Re: [BLOCK] Random topic
« Reply #23 on: August 20, 2009, 06:18:19 AM »
No problem - that's been edited now...  :D

Maybe you can drop a link so others can see it in action ?  I don't have a site I need to use it on at the moment.

Cheers :)
« Last Edit: August 20, 2009, 06:20:38 AM by freddy888 »

alving

  • Guest
Re: [BLOCK] Random topic
« Reply #24 on: August 22, 2009, 07:48:57 AM »
thanks again freddy888.. i'm still playing with my site a bit, so once it's full on, i'll provide a link.. thanks once again freddy888 and  jpdeni..

Offline Freddy

  • Moderator
  • *
  • Posts: 1615
    • AiDreams
Re: [BLOCK] Random topic
« Reply #25 on: August 22, 2009, 09:21:24 AM »
Okay my friend, no pressure...and yes thanks to JPDeni from me too  :)

alving

  • Guest
Re: [BLOCK] Random topic
« Reply #26 on: April 26, 2010, 03:34:54 AM »
jpdeni and/or freddy888,

 this code can display random topics from certain category/ies.  how can i display random topics from a specific board/s.  i still want the avatars displayed..

thanks in advance guys..  still using TP098 and SMF 11

JPDeni

  • Guest
Re: [BLOCK] Random topic
« Reply #27 on: April 26, 2010, 08:16:27 AM »
Change

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

to

Code: [Select]
// If you want to limit it to one board, enter the category number here
// If you want to use all boards, set it to 0
$board = 0 ;

And change

Code: [Select]
AND $user_info[query_see_board] " .
  ($category > 0 ? "AND ID_CAT = $category" : "")
  . " ORDER BY RAND() LIMIT $limit", __FILE__, __LINE__);

to

Code: [Select]
AND $user_info[query_see_board] " .
  ($board > 0 ? "AND b.ID_BOARD = $board" : "")
  . " ORDER BY RAND() LIMIT $limit", __FILE__, __LINE__);

alving

  • Guest
Re: [BLOCK] Random topic
« Reply #28 on: April 26, 2010, 03:18:53 PM »
thanks once again, jpdeni  :up: i will try out later... much appreciated

JPDeni

  • Guest
Re: [BLOCK] Random topic
« Reply #29 on: April 26, 2010, 06:58:06 PM »
I didn't test it, but it should work. Just some simple substitutions.