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

Recent

Welcome to TinyPortal. Please login or sign up.

April 25, 2024, 10:04:54 PM

Login with username, password and session length
Members
  • Total Members: 3,885
  • Latest: Growner
Stats
  • Total Posts: 195,173
  • Total Topics: 21,219
  • Online today: 319
  • Online ever: 3,540 (September 03, 2022, 01:38:54 AM)
Users Online
  • Users: 0
  • Guests: 212
  • Total: 212

[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.

alving


Freddy

#21
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 :)

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);


alving

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..

Freddy

#23
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 :)

alving

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..

Freddy

Okay my friend, no pressure...and yes thanks to JPDeni from me too  :)

alving

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

Change


// 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


// 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


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


to


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


alving

thanks once again, jpdeni  :up: i will try out later... much appreciated

JPDeni

I didn't test it, but it should work. Just some simple substitutions.