TinyPortal

Development => Block Codes => Topic started by: Sarke on May 14, 2007, 12:10:39 AM

Title: Currently Active Topics
Post by: Sarke on May 14, 2007, 12:10:39 AM
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.
Title: Re: Weekly Active Topics
Post by: babjusi on May 14, 2007, 02:01:57 AM
Thanks for sharing it with us, I will give it a try
Title: Re: Weekly Active Topics
Post by: Sarke on May 15, 2007, 05:32:16 AM
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.
Title: Re: Currently Active Topics
Post by: babjusi on May 15, 2007, 10:00:38 PM
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
Title: Re: Currently Active Topics
Post by: jacortina on May 15, 2007, 11:24:06 PM
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.
Title: Re: Currently Active Topics
Post by: Sarke on May 16, 2007, 12:12:54 AM
Yeah, just remove the php tags <?php and ?>.
Title: Re: Currently Active Topics
Post by: babjusi on May 16, 2007, 03:24:28 AM
I will try it out again. Thank you both for your help
Title: Re: Currently Active Topics
Post by: kopijun on May 22, 2007, 07:51:43 PM
Is it posssible to select topics from certain boards ?
Title: Re: Currently Active Topics
Post by: tick on May 22, 2007, 07:55:55 PM
Take a look at this one.   ;)
Title: Re: Currently Active Topics
Post by: kopijun on May 22, 2007, 08:13:16 PM
Quote from: Tick on May 22, 2007, 07:55:55 PM
Take a look at this one.   ;)

What does it mean ?
Title: Re: Currently Active Topics
Post by: tick on May 22, 2007, 08:15:44 PM
I am sorry bro.  I am not thinking straight today.  I did not add the link.   :2funny:    Here see if this works better.  lol   http://www.tinyportal.net/index.php?topic=1234.0
Title: Re: Currently Active Topics
Post by: kopijun on May 22, 2007, 08:21:11 PM
I see  :2funny:
Title: Re: Currently Active Topics
Post by: kopijun on May 22, 2007, 08:28:43 PM
Well I've tried those links view days ago, but in fact I like the style of Sarke's code more better...Is there a way to select topics from certain board but with this Sarke's style ?

I mean with this style :

Quote from: Sarke on May 14, 2007, 12:10:39 AM

<?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>'
;
}

?>



Title: Re: Currently Active Topics
Post by: tick on May 22, 2007, 09:02:32 PM
I wish I could help  you there but I am not much of a coder.   :(  give it a little time and maybe one of the coders can help.
Title: Re: Currently Active Topics
Post by: kopijun on May 22, 2007, 09:17:38 PM
nevermind  :)
Thanks anyway...
Title: Re: Currently Active Topics
Post by: Sarke on May 23, 2007, 12:46:21 AM
kopijun, just replace


WHERE {$user_info['query_see_board']}


with


WHERE {$user_info['query_see_board']}
AND b.ID_BOARD IN (1, 2, 3)


Where the "1, 2, 3" part is the IDs of the boards you want.
Title: Re: Currently Active Topics
Post by: RoarinRow on September 16, 2008, 08:03:59 PM
Thanks for this code.  It's solved my problem with my Recent Topics block not showing the correct number of topics to guests and other grouped members.