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

Recent

Welcome to TinyPortal. Please login or sign up.

March 28, 2024, 08:09:37 AM

Login with username, password and session length
Members
Stats
  • Total Posts: 195,104
  • Total Topics: 21,212
  • Online today: 143
  • Online ever: 3,540 (September 03, 2022, 01:38:54 AM)
Users Online
  • Users: 0
  • Guests: 129
  • Total: 129

[Block] Random Topics

Started by Freddy, October 18, 2010, 05:36:40 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Freddy

Name of Snippet : Random Topics
SMF/TP versions tested : SMF2RC3/TinyPortal 1.0 beta 5.2
Block Type : PHP Block or Article
Author : Started by JPDeni, adapted and updated by Freddy
Link to Discussion : http://www.tinyportal.net/index.php?topic=29484.0
Other Requirements : You need to add an image to your default theme directory - explained below.
Description :

Simply shows x number of random TOPICS with avatars, number of comments and a link to comment.  Can be set up to use certain categories too.

How to configure :

$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 so they all match, should look OK if your avatars are mostly square.

$noavatarurl - This is where the code will look if the member does not have an avatar - I doubt you will need to change it.  I've attached a few types of images you can use - just change the name of the one you like to 'noavatar.jpg' and drop it in your Themes/default/images folder, that way it will work with all themes.

The rest is explained in the block code.

Freddy

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

// 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 = $boardurl . '/Themes/default/images/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 = tpdb_query("
    SELECT m.id_board, m.icon, m.subject, m.body, IFNULL(mem.real_name, m.poster_name) AS poster_name, m.poster_time,
         t.num_replies, t.id_topic, m.id_member, m.smileys_enabled, 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['poster_name'] . '" title="' . $row['poster_name'] . '" src="' . $memberContext[$memberID]['avatar']['href'] . '" />';
        }
        else
        {
                 $news['avatar'] = '<img width="' . $avwidth . '" height="' . $avheight . '" style="border:1px solid ' . $avbordercol . ' ;padding: 2px" alt="' . $row['poster_name'] . '" title="' . $row['poster_name'] . '" src="' . $noavatarurl . '" />';
        }

$row['body'] = parse_bbc($row['body'], $row['smileys_enabled'], $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['poster_time']);
$news['body'] = $row['body'];
$news['href'] = $scripturl . '?topic=' . $row['id_topic'] . '.0';
$news['link'] = '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0">' . $row['num_replies'] . ' ' . ($row['num_replies'] == 1 ? $txt['ssi_comment'] : $txt['ssi_comments']) . '</a>';
$news['comment_link'] = !empty($row['locked']) ? '' : '<a href="' . $scripturl . '?action=post;topic=' . $row['id_topic'] . '.' . $row['num_replies'] . ';num_replies=' . $row['num_replies'] . '">' . $txt['ssi_write_comment'] . '</a>';
$news['poster'] = !empty($row['id_member']) ? '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['poster_name'] . '</a>' : $row['poster_name'];
$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['by'] , ' ' , $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);

Freddy

Updated code to use the $smcFunc in SMF 2....

Styling is not that great, but as the layout is a bit simpler to do I leave it up to you to be creative with that.


global $txt, $user_info, $settings, $memberContext, $scripturl, $boardurl, $smcFunc;

// Configuration...

// Percentage width of avatar area
$avatararea = 30;
// Colour of border around avatar
$avbordercol= '#fff';
// Default width and height of avatar
$avwidth = 35;
$avheight= 35;
// Path to 'no avatar' image.
$noavatarurl = $boardurl . '/Themes/default/images/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 = $smcFunc['db_query']('', "
SELECT m.id_board, m.icon, m.subject, m.body, IFNULL(mem.real_name, m.poster_name) AS poster_name, m.poster_time,
t.num_replies, t.id_topic, m.id_member, m.smileys_enabled, 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 = {int:cat}" : "")
. " ORDER BY RAND() LIMIT {int:limit}",
array(
'cat' => $category,
'limit' => $limit)
);

while ($row = $smcFunc['db_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['poster_name'] . '" title="' . $row['poster_name'] . '" src="' . $memberContext[$memberID]['avatar']['href'] . '" />';
}
else
{
$news['avatar'] = '<img width="' . $avwidth . '" height="' . $avheight . '" style="border:1px solid ' . $avbordercol . ' ;padding: 2px" alt="' . $row['poster_name'] . '" title="' . $row['poster_name'] . '" src="' . $noavatarurl . '" />';
}

$row['body'] = parse_bbc($row['body'], $row['smileys_enabled'], $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['poster_time']);
$news['body'] = $row['body'];
$news['href'] = $scripturl . '?topic=' . $row['id_topic'] . '.0';
$news['link'] = '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0">' . $row['num_replies'] . ' ' . ($row['num_replies'] == 1 ? $txt['ssi_comment'] : $txt['ssi_comments']) . '</a>';
$news['comment_link'] = !empty($row['locked']) ? '' : '<a href="' . $scripturl . '?action=post;topic=' . $row['id_topic'] . '.' . $row['num_replies'] . ';num_replies=' . $row['num_replies'] . '">' . $txt['ssi_write_comment'] . '</a>';
$news['poster'] = !empty($row['id_member']) ? '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['poster_name'] . '</a>' : $row['poster_name'];
$news['locked'] = !empty($row['locked']);

// Display it all...try your own styling. Might be better in a table.
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['by'] , ' ' , $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>';
}

$smcFunc['db_free_result']($result);