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

Recent

Welcome to TinyPortal. Please login or sign up.

May 20, 2024, 11:27:19 PM

Login with username, password and session length
Members
Stats
  • Total Posts: 195,197
  • Total Topics: 21,221
  • Online today: 144
  • Online ever: 3,540 (September 03, 2022, 01:38:54 AM)
Users Online
  • Users: 0
  • Guests: 246
  • Total: 246

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


tick

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

kopijun


kopijun

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

?>




tick

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.

kopijun

nevermind  :)
Thanks anyway...

Sarke

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.

RoarinRow

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.

SMF 2.0 RC3
TP 1.0 beta 5-1
Wordpress 3.0