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?

October 02, 2022, 10:21:49 AM

Login with username, password and session length
Members
Stats
  • Total Posts: 193513
  • Total Topics: 21092
  • Online today: 38
  • Online ever: 3540
  • (September 02, 2022, 06:38:54 PM)
Users Online
Users: 0
Guests: 43
Total: 43

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

0 Members and 1 Guest are viewing this topic.

Offline Freddy

  • Moderator
  • *
  • Posts: 1617
    • AiDreams
Re: [Discussion] Random topic
« Reply #60 on: October 15, 2015, 03:22:26 PM »
then i guess the script will only work with 1 category or all categories.
Yes that is correct. I just adapted it so you can exclude categories. Try this...

Just set this to zero...

Code: [Select]
$category = 0;
Then set $exclude to an array of cat ids. For example...

Code: [Select]
$exclude = array(12, 5, 3);
and it will ignore those.

Code: [Select]
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. If you want to exclude cats below set this to zero too.
$category = 0 ;

// Exclude categories ? Use the category ids eg. $exclude = array(3,6);
// Also set $category above to 0 (zero) for this to work.
$exclude = array();

// 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}" : "")
.
(!empty($exclude) ? " AND id_cat NOT IN ({array_int:exclude})" : "")
. "
ORDER BY RAND() LIMIT {int:limit}",
array(
'exclude' => $exclude,
'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);

Offline ayozeve

  • Jr. Member
  • **
  • Posts: 6
Re: [Discussion] Random topic
« Reply #61 on: October 15, 2015, 04:40:26 PM »
Thanks a lot but i have just insert it and it doesnt't work for me :-[. I wrote this:

Code: [Select]
global $txt, $user_info, $settings, $memberContext, $scripturl, $boardurl, $smcFunc;

// Configuration...

// Percentage width of avatar area
$avatararea = 8;
// 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. If you want to exclude cats below set this to zero too.
$category = 0 ;

// Exclude categories ? Use the category ids eg. $exclude = array(3,6);
// Also set $category above to 0 (zero) for this to work.
$exclude = array(54,40,13,43,47,14,38,86,19,83,61,30,34,16,32,17,28,53,41,8,44,48,9,39,45,85,18,82,60,29,35,11,31,10,26,64,59,65,66,77,78,67,68,84,69,81,70,71,72,73,74,75,76);

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

// 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}" : "")
.
(!empty($exclude) ? " AND id_cat NOT IN ({array_int:exclude})" : "")
. "
ORDER BY RAND() LIMIT {int:limit}",
array(
'exclude' => $exclude,
'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);


I only let 2 board to show (to see it is correct), but it doesn't work. Are there any bad????
The problem with this script is that can show private content from boards, i guess.

Thanks so much.

Offline Freddy

  • Moderator
  • *
  • Posts: 1617
    • AiDreams
Re: [Discussion] Random topic
« Reply #62 on: October 16, 2015, 04:59:56 AM »
$exclude needs to be the cats you want left out.

Offline ayozeve

  • Jr. Member
  • **
  • Posts: 6
Re: [Discussion] Random topic
« Reply #63 on: October 16, 2015, 05:43:09 AM »
Yes, i understood but it didn't work for me. It show all the boards and the exlude boards too.

Offline illori

  • Administrator
  • *
  • Posts: 2075
Re: [Discussion] Random topic
« Reply #64 on: October 16, 2015, 05:47:25 AM »
are you entering the numbers for the CATEGORIES or the numbers for the BOARDS. you need to make sure they are the id numbers for the CATEGORIES, if you have a category with both public and private boards in it, then you need to exclude that CATEGORY.

Offline ayozeve

  • Jr. Member
  • **
  • Posts: 6
Re: [Discussion] Random topic
« Reply #65 on: October 16, 2015, 06:17:27 AM »
are you entering the numbers for the CATEGORIES or the numbers for the BOARDS. you need to make sure they are the id numbers for the CATEGORIES, if you have a category with both public and private boards in it, then you need to exclude that CATEGORY.

You have rights, I was put id boards instead id cats. Thanks a lot men!!

Offline Ken.

  • Support Team
  • *
  • Posts: 4362
    • Our FamilyForum
Re: [Discussion] Random topic
« Reply #66 on: October 16, 2015, 11:30:51 AM »
Thanks a lot men!!

Some are... some aren't.  8)
" If everything seems under control, you’re not going fast enough." - Mario Andretti
Yesterday When I was Young.

Offline VladTepes

  • Jr. Member
  • **
  • Posts: 90
Re: [Discussion] Random topic
« Reply #67 on: March 31, 2017, 06:13:54 AM »
How can I get this to display in exactly the same manner / format as the 'Recent Topics' block?

(So just avatar, title, author, date/time)

I don't want any preview etc.

Thanks