Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Topic started by: Dylert on August 17, 2019, 03:51:42 PM

Title: Fantstic recent post block, but....
Post by: Dylert on August 17, 2019, 03:51:42 PM
I have a fantastic recent posts block with columns of information. Unfortunately the block slows down my forum so I can't use it.
What can I do to fix the code? Any suggestions?

The code:

/*
The Setup is inside the tab!
Have Fun,


/***************************************************************************
*************** START THE TAB **********************************************
***************************************************************************/

$num_recent = 30; // HOW MANY RECENT TOPICS TO OUTPUT?
  $include_boards = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150); // IF ALL BOARDS null | IF SOME BOARDS array( ID1, ID2, ID3)
 
 
 
  // code, code, code and some more code bahhh!
  global $smcFunc, $scripturl, $context, $settings, $db_prefix, $user_info;
 
  if (is_array($include_boards) || (int) $include_boards === $include_boards)
{
$include_boards = is_array($include_boards) ? $include_boards : array($include_boards);
}
elseif ($include_boards != null)
{
$output_method = $include_boards;
$include_boards = array();
}

$topics_result = $smcFunc['db_query']('', '
    SELECT z.member_name as topic_member, t.id_member_started, m.poster_name, m.poster_time, m.id_msg, t.num_replies, t.num_views, t.id_member_updated, m.subject, m.body, m.id_topic, b.name, b.id_board, t.id_last_msg, u.avatar, g.online_color, ' . ($user_info['is_guest'] ? '1 AS is_read, 0 AS new_from' : '
   
IFNULL(lt.id_msg, IFNULL(lmr.id_msg, 0)) >= m.id_msg_modified AS is_read,
IFNULL(lt.id_msg, IFNULL(lmr.id_msg, -1)) + 1 AS new_from') . '
    FROM {db_prefix}topics AS t
    INNER JOIN {db_prefix}boards AS b ON (t.id_board = b.id_board)
    INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_last_msg)
    INNER JOIN smf_members AS z ON (t.id_member_started = z.id_member)
    LEFT JOIN {db_prefix}members AS u ON (t.id_member_updated = u.id_member)
    LEFT JOIN {db_prefix}membergroups AS g ON (g.id_group = CASE WHEN u.id_group = 0 THEN u.id_post_group ELSE u.id_group END)' . (!$user_info['is_guest'] ? '
LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = t.id_topic AND lt.id_member = {int:current_member})
LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = b.id_board AND lmr.id_member = {int:current_member})' : '') . ' 
    WHERE m.approved=1' . (empty($include_boards) ? '' : '
AND b.id_board IN ({array_int:include_boards})') . '
    ORDER BY t.id_last_msg DESC
    LIMIT ' . $num_recent,
array(
'current_member' => $user_info['id'],
'include_boards' => empty($include_boards) ? '' : $include_boards,
)   
    );
$topics = array();
while ($row_topics = $smcFunc['db_fetch_assoc']($topics_result))
{
    $topics[] = array(
       'topic' => $row_topics['id_topic'],
       'poster' => '<a style="color: ' . ';" href="' . $scripturl . '?board=' . $row_topics['id_member_updated'] . '">' . $row_topics['poster_name'] . '</a>',
       'link' => '<a title="In ' . $row_topics['name'] .'" href="' . $scripturl . '?topic=' . $row_topics['id_topic'] . '.msg' . $row_topics['id_last_msg'] . ';topicseen#new">' . $row_topics['subject'] . '</a>',
       'href' => $scripturl . '?topic=' . $row_topics['id_topic'] . '.msg' . $row_topics['id_last_msg'] . ';topicseen#new',
  'time' => timeformat($row_topics['poster_time']),
  'new' => !empty($row_topics['is_read']),
  'views_replies' => $row_topics['num_views'] . '/' . $row_topics['num_replies'],

        'name' =>  '<a style="color: ' . ';" href="' . $scripturl . '?board=' . $row_topics['id_board'] . '">' . $row_topics['name'] . '</a>',
 
      'topic_member' => '<a style="color: ' . ';" href="' . $scripturl . '?action=profile;u=' . $row_topics['id_member_started'] . '">' . $row_topics['topic_member']. '</a>',
    );
}
$smcFunc['db_free_result']($topics_result);
echo '

<table id="table_box"
border="0" width="100%" cellspacing="1" cellpadding="2" class="bordercolor">
<tr class="titlebg">
                  <td valign="blueh">Emne</td>
  <td valign="middle">Av</td>
  <td valign="middle">Trådstarter</td>
  <td valign="middle">Lest/svar</td>
  <td valign="middle">Forum</td>
  <td valign="middle">Tid</td>
 
  <td valign="middle"></td>
</tr>';

foreach ($topics as $topic)
{
echo '
<tr>

<td class="windowbg" valign="middle">', str_replace('Sv:' , '' , $topic['link']);


// new log! What a headache!
if (!$topic['new'] && $context['user']['is_logged'])
echo '
<a href="', $scripturl, '?topic=', $topic['topic'], '.from', $topic['time'], '#new"><img src="', $settings['images_url'], '/', $context['user']['language'], '/new.gif" alt="new" border="0" /></a>';

echo '
</td>
<td class="windowbg2" valign="middle">', $topic['poster'], '</td>
<td class="windowbg2" valign="middle">', $topic['topic_member'], '</td>
<td class="windowbg2" valign="middle">', $topic['views_replies'], '</td>
<td class="windowbg2" valign="middle">', $topic['name'], '</td>
<td class="windowbg2" valign="middle">', $topic['time'], '</td>

';

if ($settings['images_url'] != $settings['theme_url'] . '/images' || file_exists($settings['theme_dir'] . '/images/icons/last_post.gif'))
echo '
</a>';
}

echo '
</td>
</tr>
</table>
</div>';
/***************************************************************************
*************** END THE TAB ************************************************
***************************************************************************/






?>
Title: Re: Fantstic recent post block, but....
Post by: vbgamer45 on August 17, 2019, 04:35:30 PM
Are you do all boards?
If so change
$num_recent = 30;   
$include_boards = null;
Title: Re: Fantstic recent post block, but....
Post by: Dylert on August 17, 2019, 04:58:44 PM
Thanks a lot! It seems faster now. Anyway I want to exclude one board. How can I do that? The boards is number 110.
Title: Re: Fantstic recent post block, but....
Post by: Diego Andrés on August 17, 2019, 06:25:05 PM
Also you could add some cache to the query (1 minute or something) if the forum is not extremely active to make it even faster.

For excluding boards you can use the same you were using for including them in the first post.


$exclude_boards = array(1,3,etc);


AND b.id_board NOT IN ({array_int:exclude_boards})

array(
'current_member' => $user_info['id'],
'exclude_boards' => empty($exclude_boards) ? '' : $exclude_boards,
Title: Re: Fantstic recent post block, but....
Post by: aryansindy on August 19, 2019, 08:58:08 AM
Ok its nice,
how to add member or poster avatar to it??
Thanks.
Title: Re: Fantstic recent post block, but....
Post by: moorlord on November 20, 2019, 07:15:05 AM
I want to keep only topic and remove posts please help :)