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.
Thanks for sharing it with us, I will give it a try
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.
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
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.
Yeah, just remove the php tags <?php and ?>.
I will try it out again. Thank you both for your help
Is it posssible to select topics from certain boards ?
Take a look at this one. ;)
Quote from: Tick on May 22, 2007, 07:55:55 PM
Take a look at this one. ;)
What does it mean ?
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
I see :2funny:
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>';
}
?>
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.
nevermind :)
Thanks anyway...
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.
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.