Title: Trending Topics Block Code Post by: bio_man on Sep 4, 2018 This code has been designed for Biology Forums to show the most popular topics in the past day, 2 days, 3 days. You are free to use it on your forum too.
Source Code function Trending($limit = 15, $exclude_boards = null) { global $db_prefix, $modSettings, $user_info, $context, $scripturl, $settings; $past['time'] = !empty($_GET['past']) ? (int) mysql_real_escape_string($_GET['past']) : 0; $context['page_title'] = 'Trending Topics - '.$context['forum_name']; loadTemplate('Recent'); $context['sub_template'] = 'trending'; if ($exclude_boards === null && !empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0) $exclude_boards = array($modSettings['recycle_board']); else $exclude_boards = empty($exclude_boards) ? array() : $exclude_boards; $time = time(); if($past['time'] == 2) { $time2 = $time - (86400*2); $timing = 48; $context['past48'] = true; } elseif($past['time'] == 3) { $time2 = $time - (86400*3); $timing = 72; $context['past72'] = true; } else { $time2 = $time - 86400; $timing = 24; $context['past24'] = true; } $request = db_query(" SELECT COUNT(mes.id_topic) as t, mes.id_topic, mes2.subject, b.id_board, b.name FROM {$db_prefix}messages AS mes LEFT JOIN {$db_prefix}boards as b ON (mes.id_board = b.id_board) LEFT JOIN {$db_prefix}topics as top ON (mes.id_topic = top.id_topic) LEFT JOIN {$db_prefix}messages AS mes2 ON (mes2.id_msg = top.ID_FIRST_MSG) WHERE mes.posterTime <= $time AND mes.posterTime >= $time2" . (empty($exclude_boards) ? '' : " AND b.ID_BOARD NOT IN (" . implode(', ', $exclude_boards) . ")") . " AND $user_info[query_see_board] GROUP BY id_topic ORDER BY t DESC, id_topic DESC LIMIT $limit", __FILE__, __LINE__); $context['PopTopics'] = array(); while ($row = mysql_fetch_assoc($request)) { $context['PopTopics'][] = array( 'coll' => $row['t'], 'id_topic' => $row['id_topic'], 'subject' => $row['subject'], 'board_id' => $row['id_board'], 'board_name' => $row['name'], 'timing' => $timing ); } mysql_free_result($request); $context['linktree'][] = array( 'url' => $scripturl.'?action=trending', 'name' => 'Trending Topics', ); } Template function template_trending() { global $context, $scripturl; theme_linktree(); echo' <div class="messagefilter"> <ul> <li><i class="fa fa-clock-o" aria-hidden="true"></i> Trending in the past:</li> <li><a href="',$scripturl,'?action=trending"',isset($context['past24'])?' class="selected"':'','>24 Hours</a></li> <li><a href="',$scripturl,'?action=trending;past=2"',isset($context['past48'])?' class="selected"':'',' rel="nofollow">48 Hours</a></li> <li><a href="',$scripturl,'?action=trending;past=3"',isset($context['past72'])?' class="selected"':'',' rel="nofollow">72 Hours</a></li> </ul> </div> <div style="width: 90%; margin: 1em auto 0 auto;" id="trendbox">'; foreach ($context['PopTopics'] as $what) { echo' <div class="message notify information"> <div class="trend_div_1"> <a href="',$scripturl,'?topic=',$what['id_topic'], '.0">', $what['subject'], ' <span class="normaltext"><i class="fa fa-sign-out fa-fw" aria-hidden="true"></i></span></a> </div> <div class="trend_div_2 extra_info"> <i class="fa fa fa-sitemap fa-fw" aria-hidden="true"></i> <a href="',$scripturl,'?board=', $what['board_id'], '">', $what['board_name'], '</a> <i class="fa fa-reply fa-fw" aria-hidden="true"></i> <b>', $what['coll'], '</b> ',$what['coll'] != 1 ? 'posts' : 'post',' made in the past ',$what['timing'],' hours </div> </div>'; } echo' </div>'; } |