Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Aiheen aloitti: Dylert - marraskuu 26, 2013, 03:59:28 IP

Otsikko: Recent topics block with Topic starter
Kirjoitti: Dylert - marraskuu 26, 2013, 03:59:28 IP
(To the moderators: Can be moved to Tips and tricks?)

If you want a "Recent topics" block that includes Topic starter, you can use the one below. Thanks to http://www.phphelp.com/ for helping me with this code. I have combined the code with the Tabbed block code from SimplePortal: http://simpleportal.net/index.php?topic=9143.0

The result can be shown at the bottom of my site http://www.hellasforum.net

$num_recent = 40; // HOW MANY RECENT TOPICS TO OUTPUT?
     $include_boards = null; // 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 '
    <div class="tabsmenucontent" style="padding: 2px">
    <table border="0" width="100%" cellspacing="1" cellpadding="2" class="bordercolor">
    <tr class="titlebg">
                    <td valign="middle">Topic</td>
    <td valign="middle">By</td>
    <td valign="middle">Topic starter</td>
    <td valign="middle">Views/posts</td>
    <td valign="middle">Board</td>
    <td valign="middle">Time</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>
 
    <td class="windowbg2" valign="middle">';
   
    if ($settings['images_url'] != $settings['theme_url'] . '/images' || file_exists($settings['theme_dir'] . '/images/icons/last_post.gif'))
    echo '
    <a href="', $topic['href'], '"><img src="', $settings['images_url'], '/icons/last_post.gif" alt="Last Post" title="Last Post" border="0" style="float: right;" /></a>';
    }
 
  echo '
  </td>
  </tr>
  </table>
  </div>';
Otsikko: Re: Recent topics block with Topic starter
Kirjoitti: Arantor - marraskuu 26, 2013, 04:04:58 IP
I really hope you never delete accounts.
Otsikko: Re: Recent topics block with Topic starter
Kirjoitti: Dylert - marraskuu 26, 2013, 04:06:23 IP
What do you mean?
Otsikko: Re: Recent topics block with Topic starter
Kirjoitti: Arantor - marraskuu 26, 2013, 04:08:21 IP
If you delete accounts, that query is going to misbehave.
Otsikko: Re: Recent topics block with Topic starter
Kirjoitti: Dylert - marraskuu 26, 2013, 04:10:37 IP
Ok, good to know! I have never deleted an account before.... :)