Page 1 of 2

X viewing forums

Posted: Tue Oct 23, 2007 12:46 am
by wiseman1161980
I think there was a mod in phpbb2, but I'd like to see a mod showing how many people are viewing a certain forum next to the forum name on board index.

Image

Re: X viewing forums

Posted: Sun Jun 22, 2008 9:09 am
by wiseman1161980
I think I'm due for a bump :lol:

anyone?

Re: X viewing forums

Posted: Sun Jun 22, 2008 12:42 pm
by tommekemc
okay, I did it like this:
in includes/functions_display.php find:

Code: Select all

		// Output moderator listing ... if applicable
add before:

Code: Select all

$viewers_mysql=$db->sql_query("SELECT `session_forum_id` 
		FROM ". SESSIONS_TABLE ." 
		WHERE `session_last_visit`>".(time()-($config['load_online_time']*60)));
		$forum_viewers=0;
		while($viewers_mysql_row=$db->sql_fetchrow($viewers_mysql)){
			if($viewers_mysql_row['session_forum_id']==$row['forum_id']){
				$forum_viewers++;
			}
		}
find:

Code: Select all

			'S_IS_CAT'			=> false,
add before:

Code: Select all

			'FORUM_VIEWERS'		=> $forum_viewers,
in styles/YOUR STYLE/forumlist_body.html
add this code where you want the text to come:

Code: Select all

<b> ({forumrow.FORUM_VIEWERS} viewing)</b>

Re: X viewing forums

Posted: Sun Jun 22, 2008 6:06 pm
by wiseman1161980
I tried that and it threw
[phpBB Debug] PHP Notice: in file /includes/functions_display.php on line 364: mysql_fetch_row(): supplied argument is not a valid MySQL result resource

Re: X viewing forums

Posted: Sun Jun 22, 2008 7:28 pm
by tommekemc
my mistake, had some of my old code in there, it works on my forum :)

Re: X viewing forums

Posted: Sun Jun 22, 2008 8:15 pm
by wiseman1161980
awesome, works like a charm.

Thanks a million... again. :D

Re: X viewing forums

Posted: Mon Jun 23, 2008 8:30 pm
by darkonia
great little mini addon...thx :D

Re: X viewing forums

Posted: Mon Jun 23, 2008 8:39 pm
by tommekemc
well, I did a lidle patch, for the ones already having this mod:
some users were still 'browsing' when they already showed offline...
and more patching to come :p

Re: X viewing forums

Posted: Mon Jun 23, 2008 8:58 pm
by tommekemc
delete me, accident...

Re: X viewing forums

Posted: Mon Jun 23, 2008 9:01 pm
by darkonia
lol whats up dude... :shock:

Re: X viewing forums

Posted: Mon Jun 23, 2008 9:19 pm
by Highway of Life
The code posted has several errors and I don't recommend using it, as-is.

Firstly, it is running an SQL Query inside of a loop, this is never a good idea, you’d end up increasing the load on both the web server and most especially the database server by increasing the SQL queries astronomically and unnecessarily.

Second, you’re using hard-coded mysql_* functions, this is again, a big no-no for code within phpBB3. It should be using the DBAL (Database Abstraction Layer) .

Thirdly, you’re hard-coding the table prefix, so a user who has a table prefix other than 'phpbb_', this code will not work for them.

Fourth, it could be done using a single fetchfield query without the unnecessary extra rows:

Code: Select all

$sql = 'SELECT COUNT(session_id) AS forum_viewers
FROM ' . SESSIONS_TABLE . '
WHERE session_forum_id = ' . (int) $row['forum_id'] . ' AND session_last_visit >= ' . (time() - $config['load_online_time']);
$result = $db->sql_query($sql);
$forum_viewers = $db->sql_fetchfield('forum_viewers'); 
Note that this is only a demonstration and is no way intended to be used live, it is still running the query from the loop, but is only meant to demonstrate what the query should look like.

Re: X viewing forums

Posted: Mon Jun 23, 2008 9:21 pm
by tommekemc
I know, I'm editing it at this very moment :)
generated a load of bugs while making it as you want :p

Re: X viewing forums

Posted: Mon Jun 23, 2008 9:28 pm
by tommekemc
1st thing: I found out that fetchfield only fetches the data once (hereby only calculating the data for the 1st forum, so I had to place it inside the loop.)
2nd: just worked that out
3rd: worked away, updating :)
4th: sorry, I'm used to working with mysql in a completely different way...

and last: I'm only trying to help... :(

Re: X viewing forums

Posted: Mon Jun 23, 2008 9:53 pm
by Highway of Life
tommekemc wrote:1st thing: I found out that fetchfield only fetches the data once (hereby only calculating the data for the 1st forum, so I had to place it inside the loop.)
Yes, of course... it would only work on a per-forum basis if you use a count, therefore, you’d take an array of the forum_ids and do the following query...

($forum_ary is your array of forum ids.)

Code: Select all

$sql = 'SELECT session_forum_id, COUNT(session_forum_id) AS forum_visitors
FROM ' . SESSIONS_TABLE . '
WHERE ' . $db->sql_in_set('session_forum_id', $forum_ary) . '
    AND session_last_visit >= ' . (time() - $config['load_online_time']) . '
GROUP BY session_forum_id';  
Now just match up $row['forum_visitors'] with $row['session_forum_id'] to give you a count of total visitors within each forum.
tommekemc wrote:and last: I'm only trying to help... :(
And that’s okay, but if you’re not sure if your method is good, it might be a good idea to put a disclaimer on there, as other non-coders are going to trust that you’re going to provide them with good, secure, and optimised code, which it wasn’t and I therefore had to come out with a stern warning not to use the code until it is perfected.
Not trying to pick on you, just ensuring people were not going to use broken code. No worries. :)

Re: X viewing forums

Posted: Mon Jun 23, 2008 10:18 pm
by wiseman1161980
tommekemc wrote:well, I did a lidle patch, for the ones already having this mod:
some users were still 'browsing' when they already showed offline...
and more patching to come :p
If your gonna put the time into this, why not start a topic in mods in development and become a modder? :mrgreen:

I know several people would like to use this. I know I appreciate it.