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

Recent

Welcome to TinyPortal. Please login or sign up.

Members
  • Total Members: 3,963
  • Latest: BiZaJe
Stats
  • Total Posts: 195,917
  • Total Topics: 21,308
  • Online today: 884
  • Online ever: 8,223 (February 19, 2025, 04:35:35 AM)
Users Online
  • Users: 0
  • Guests: 365
  • Total: 365

Currently Active Topics

Started by Sarke, May 14, 2007, 12:10:39 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Sarke

I made this for my own forum, and I might as well share the code.

It's basically a block that is sort of like the "Recent" block, but it takes the 5 (or whatever number you want) most active topics by posts during the last 7 days (or however many days you want, decimals ok).  It's basically a list of the popular topics during the past week.


<?php

if (!defined('SMF'))
die('Hacking attempt...');

global $context$user_info$db_prefix$scripturl;

$days 7;
$limit 5;

$time time() - 60*60*24*$days;

$result db_query("
SELECT b.ID_BOARD as board
FROM 
{$db_prefix}boards AS b
WHERE 
{$user_info['query_see_board']}
"
__FILE____LINE__);

$boards = array();
while ($row mysql_fetch_array($result))
{
$boards[] = $row['board'];
}
mysql_free_result($result);

$result db_query("
SELECT ID_TOPIC as id, COUNT(*) as posts
FROM 
{$db_prefix}messages
WHERE posterTime > 
$time
AND ID_BOARD IN (" 
implode(','$boards) . ")
GROUP BY ID_TOPIC
ORDER BY posts DESC
LIMIT 
$limit
"
__FILE____LINE__);

$topics = array();
while ($row mysql_fetch_array($result))
{
$topics[$row['id']]['posts'] = $row['posts'];
}
mysql_free_result($result);

if (empty($topics))
{
echo '<div class="smalltext">No topics to display.</div>';
}
else
{
echo '
<table cellspacing="0" cellpadding="0" style="width: 100%; margin: 0px; padding: 0px;">'
;

$result db_query("
SELECT ID_TOPIC as id, subject, MAX(ID_MSG) as msg, MAX(posterTime) as time
FROM 
{$db_prefix}messages
WHERE ID_TOPIC IN (" 
implode(','array_keys($topics)) . ")
GROUP BY ID_TOPIC
"
__FILE____LINE__);

while ($row mysql_fetch_array($result))
{
$topics[$row['id']]['subject'] = $row['subject'];
$topics[$row['id']]['msg'] = $row['msg'];
$topics[$row['id']]['time'] = $row['time'];
}
mysql_free_result($result);

foreach ($topics as $id => $topic)
{
if (empty($first))
{
$first true;
}
else
{
echo '
<tr><td colspan="2"><hr size="1" width="100%" class="hrcolor" /></td></tr>'
;
}

echo '
<tr>
<td align="left">
<div class="smalltext"><a href="'
$scripturl'?topic='$id'.msg'$topic['msg'], '#new">'$topic['subject'], '</a></div>
<div class="smalltext">['
timeformat($topic['time']), ']</div>
</td>
<td align="right"><div class="smalltext">'
$topic['posts'], '</div></td>
</tr>'
;
}

echo '
</table>'
;
}

?>



I use it as an included file, but you should be able to just paste it in a phpblock.

EDIT: I just realised that I forgot to make it check permissions, so it will list all topics (even the ones the user can't access).  I'll look into it.
EDIT 2: Fixed the permissions thing.

babjusi

Thanks for sharing it with us, I will give it a try

Sarke

#2
I fixed the problemn where it didn't check permissions, so it's all good now. 

BTW, this can also be a "Daily topics" thing, if you just set $days = 1; if you have a really busy board you can set $days = 1/24 for hourly and the $limit = 1.  That would basically give you the one topic that has been the most active the last hour.

babjusi

I tried this but I got the following error, can you tell me please why could that be, thank you

Parse error: parse error, unexpected '<' in /data/members/paid/d/o/mysite.com/htdocs/www/Sources/Load.php(1726) : eval()'d code(35) : eval()'d code on line 2

jacortina

If you are using it in a block, start at the "global" line and leave off the final line ("?>") and see if that clears up any errors.

Sarke

Yeah, just remove the php tags <?php and ?>.

babjusi

I will try it out again. Thank you both for your help

kopijun

Is it posssible to select topics from certain boards ?

tick

Take a look at this one.   ;)

kopijun


This website is proudly hosted on Crocweb Cloud Website Hosting.