News:

Bored?  Looking to kill some time?  Want to chat with other SMF users?  Join us in IRC chat or Discord

Main Menu

Total Time Wasted on SMF Forum

Started by envoy, July 20, 2005, 09:50:56 PM

Previous topic - Next topic

envoy

As [Unknown] pointed out to me, this is available in the stats page, but I thought I would post it anyways incase someone wanted to use this on there main homepage. I didn't see it in the ssi_examples files, so I will post it.

OK, I just wrote this to show you guys a simple script for getting the top X users who have wasted the most amount of time on the board. The X can be set in the file as $limit. It also calculates the total time logged in for everyone and does an average for the time logged in. If anyone can improve on this code, please do. I have a feeling that there is a simpler way of transforming the numbers of days/hours/minutes logged in to readable form. Anywho, here is the code, I tried to comment it as much as possible. I must also mention, this is for 1.0.5. I am not sure if it works on 1.1b3.

<?
// Make sure this file goes into the same directory
// of your SMF forum.

// require the SMF Settings.php file
require("Settings.php");

// Connect to the database using the info from Setting.php
$connection=mysql_connect($db_server, $db_user, $db_passwd) OR DIE("Unable to connect to database");

// Select the database
@mysql_select_db("$db_name") or die( "Unable to select database");

// set the below variable to how many members you want to see
// example for the top 5 members would be
// $limit = 5;
$limit = 10;

// SQL/PHP Query to Get Total Time Online For Everyone
$sql = 'SELECT SUM(`totalTimeLoggedIn`) FROM `smf_members`';
$query = mysql_query($sql);
$array = mysql_fetch_array($query);
$total = $array[0];

// SQL/PHP to get total number of users registered
$sql = 'SELECT COUNT(*)
        FROM '.$db_prefix.'members';
$query = mysql_query($sql);
$array = mysql_fetch_array($query);
$members = $array[0];

// SQL Query to get top X members who have been on the most.
$sql = 'SELECT ID_MEMBER,memberName,totalTimeLoggedIn
        FROM '.$db_prefix.'members
        ORDER BY totalTimeLoggedIn DESC
        LIMIT 0,'. $limit;
$query = mysql_query($sql);
$total_count = mysql_numrows($query);

// lets end the php for now so we can create our table
?>
<table width="50%" cellspacing="0" cellpadding="0">
<tr>
<th width=\"25%\" align="left">Username</th>
<th width=\"25%\" align="left">Time Online</th>
</tr>
<?

// lets resume the php and do our for statement
for($n=0;$n<$total_count;$n++)
// for $n equals 0, and $n is less then $total_count, lets increase $n by 1.
{

// lets set our array from the query to $users_data
    $users_data = mysql_fetch_array($query);

// Time to calculate the time online in readable format
    $days = floor($users_data[totalTimeLoggedIn] / 86400);
    $hours = floor(($users_data[totalTimeLoggedIn] % 86400) / 3600);
    $minutes = floor(($users_data[totalTimeLoggedIn] % 3600) / 60);

// lets echo out the username in 1 column and time wasted online in the other column
    echo "<tr><td width=\"25%\">".$users_data[memberName]."</td>
    <td width=\"25%\">".$days."d".$hours."h".$minutes."m</td></tr>";
}

// make an average of the time online calculated for everyone
$avg = round($total/$members,2);

// now lets make that into readable form
$adays = floor($avg / 86400);
$ahrs = floor(($avg % 86400) / 3600);
$amins = floor(($avg % 3600) / 60);

// now lets echo out the columns for the average
echo "<tr><td width=\"25%\"><b>Average:</b></td><td width=\"25%\"><b>".$adays."d".$ahrs."h".$amins."m</b></td></tr>";

// and do the readable format for the total for everyone
$tdays = floor($total / 86400);
$thrs = floor(($total % 86400) / 3600);
$tmins = floor(($total % 3600) / 60);

// and then echo it out in columns and then end the table.
echo "<tr><td width=\"25%\"><b>Total:</b></td><td width=\"25%\"><b>".$tdays."d".$thrs."h".$tmins."m</b></td></tr>";
echo "<tr><td width=\"25%\"><b>Members:</b></td><td width=\"25%\"><b>".$members."</b></td></tr></table>";

?>


Here is the result:
hxxp:www.smcars.net/forums/toponline.php [nonactive]
hxxp:www.ptlovers.com [nonactive] :: hxxp:www.smcars.net/ [nonactive] :: hxxp:www.dodgefans.com/ [nonactive]

ArkServer

Can you tell me why it doesnt work?

i used your code and made a toponline.php but it doesnt load.

site:
http://arkserver.servehttp.com

direct link:
http://arkserver.servehttp.com/toponline.php

arod

possibly part of the problem is the opening tag "<?" instead of "<?php".
try to change it and see if it helps.

ArkServer

Quote from: arod on July 26, 2005, 01:37:06 PM
possibly part of the problem is the opening tag "<?" instead of "<?php".
try to change it and see if it helps.

nope didnt work

kegobeer

#4
Here's a SSI function that does just about the same thing.  Ahh, the power of code re-use.  ;)

function ssi_timeOnline($output_method = 'echo', $num_top = 10)
{
global $db_prefix, $txt, $modSettings;
loadLanguage('Stats');
$return = array();

// Get averages...
$result = db_query("
SELECT
SUM(posts) AS posts, SUM(topics) AS topics, SUM(registers) AS registers,
SUM(mostOn) AS mostOn, MIN(date) AS date
FROM {$db_prefix}log_activity", __FILE__, __LINE__);
$row = mysql_fetch_assoc($result);
mysql_free_result($result);

// This would be the amount of time the forum has been up... in days...
$total_days_up = ceil((time() - strtotime($row['date'])) / (60 * 60 * 24));

$return['average_posts'] = round($row['posts'] / $total_days_up, 2);
$return['average_topics'] = round($row['topics'] / $total_days_up, 2);
$return['average_members'] = round($row['registers'] / $total_days_up, 2);
$return['average_online'] = round($row['mostOn'] / $total_days_up, 2);

// Time online top 10.
$members_result = db_query("
SELECT ID_MEMBER, memberName, totalTimeLoggedIn
FROM {$db_prefix}members
ORDER BY totalTimeLoggedIn DESC
LIMIT $num_top", __FILE__, __LINE__);

$max_time_online = 1;
while ($row_members = mysql_fetch_assoc($members_result))
{
// Figure out the days, hours and minutes.
$timeDays = floor($row_members['totalTimeLoggedIn'] / 86400);
$timeHours = floor(($row_members['totalTimeLoggedIn'] % 86400) / 3600);

// Figure out which things to show... (days, hours, minutes, etc.)
$timelogged = '';
if ($timeDays > 0)
$timelogged .= $timeDays . $txt['totalTimeLogged5'];
if ($timeHours > 0)
$timelogged .= $timeHours . $txt['totalTimeLogged6'];
$timelogged .= floor(($row_members['totalTimeLoggedIn'] % 3600) / 60) . $txt['totalTimeLogged7'];

$return['members'][] = array(
'id' => $row_members['ID_MEMBER'],
'name' => $row_members['memberName'],
'time_online' => $timelogged,
'seconds_online' => $row_members['totalTimeLoggedIn'],
'href' => $scripturl . '?action=profile;u=' . $row_members['ID_MEMBER'],
'link' => '<a href="' . $scripturl . '?action=profile;u=' . $row_members['ID_MEMBER'] . '">' . $row_members['realName'] . '</a>'
);

if ($max_time_online < $row_members['totalTimeLoggedIn'])
$max_time_online = $row_members['totalTimeLoggedIn'];
}
foreach ($return as $i => $member)
$return['members'][$i]['time_percent'] = round(($member['seconds_online'] * 100) / $max_time_online);

$return['memberCount'] = $modSettings['memberCount'];

if ($output_method != 'echo')
return $return;

echo '
<b>', $txt[488], ': ', $return['memberCount'], '</b><br /><br />
<b>', $txt['average_online'], ': ', $return['average_online'], '</b><br /><br />
<b>', $txt['smf_stats_16'], '</b><br />
<table class="ssi_table" width="50%">
<tr>
<th width="50%" align="left">', $txt[35], ': </th>
<th width="50%" align="left">', $txt['totalTimeLogged1'], '</th>
</tr>
';
foreach ($return['members'] as $totals)
{
echo ' <tr>
<td>', $totals['name'], '</td>
<td>', $totals['time_online'], '</td>
</tr>
';
}
echo '</table>
';
}


It doesn't give average times or total time for all members on the board as those stats didn't seem too important to me, but it includes four more stats from Stats.php.  So... not exactly the same code as envoy, but close.
"The truth of the matter is that you always know the right thing to do. The hard part is doing it." - Norman Schwarzkopf
Posting and you (Click "WATCH THIS MOVIE")

ArkServer

Quote from: kegobeer on July 27, 2005, 11:48:16 PM
Here's a SSI function that does just about the same thing.  Ahh, the power of code re-use.  ;)

function ssi_timeOnline($output_method = 'echo', $num_top = 10)
{
global $db_prefix, $txt, $modSettings;
loadLanguage('Stats');
$return = array();

// Get averages...
$result = db_query("
SELECT
SUM(posts) AS posts, SUM(topics) AS topics, SUM(registers) AS registers,
SUM(mostOn) AS mostOn, MIN(date) AS date
FROM {$db_prefix}log_activity", __FILE__, __LINE__);
$row = mysql_fetch_assoc($result);
mysql_free_result($result);

// This would be the amount of time the forum has been up... in days...
$total_days_up = ceil((time() - strtotime($row['date'])) / (60 * 60 * 24));

$return['average_posts'] = round($row['posts'] / $total_days_up, 2);
$return['average_topics'] = round($row['topics'] / $total_days_up, 2);
$return['average_members'] = round($row['registers'] / $total_days_up, 2);
$return['average_online'] = round($row['mostOn'] / $total_days_up, 2);

// Time online top 10.
$members_result = db_query("
SELECT ID_MEMBER, memberName, totalTimeLoggedIn
FROM {$db_prefix}members
ORDER BY totalTimeLoggedIn DESC
LIMIT $num_top", __FILE__, __LINE__);

$max_time_online = 1;
while ($row_members = mysql_fetch_assoc($members_result))
{
// Figure out the days, hours and minutes.
$timeDays = floor($row_members['totalTimeLoggedIn'] / 86400);
$timeHours = floor(($row_members['totalTimeLoggedIn'] % 86400) / 3600);

// Figure out which things to show... (days, hours, minutes, etc.)
$timelogged = '';
if ($timeDays > 0)
$timelogged .= $timeDays . $txt['totalTimeLogged5'];
if ($timeHours > 0)
$timelogged .= $timeHours . $txt['totalTimeLogged6'];
$timelogged .= floor(($row_members['totalTimeLoggedIn'] % 3600) / 60) . $txt['totalTimeLogged7'];

$return['members'][] = array(
'id' => $row_members['ID_MEMBER'],
'name' => $row_members['memberName'],
'time_online' => $timelogged,
'seconds_online' => $row_members['totalTimeLoggedIn'],
'href' => $scripturl . '?action=profile;u=' . $row_members['ID_MEMBER'],
'link' => '<a href="' . $scripturl . '?action=profile;u=' . $row_members['ID_MEMBER'] . '">' . $row_members['realName'] . '</a>'
);

if ($max_time_online < $row_members['totalTimeLoggedIn'])
$max_time_online = $row_members['totalTimeLoggedIn'];
}
foreach ($return as $i => $member)
$return['members'][$i]['time_percent'] = round(($member['seconds_online'] * 100) / $max_time_online);

$return['memberCount'] = $modSettings['memberCount'];

if ($output_method != 'echo')
return $return;

echo '
<b>', $txt[488], ': ', $return['memberCount'], '</b><br /><br />
<b>', $txt['average_online'], ': ', $return['average_online'], '</b><br /><br />
<b>', $txt['smf_stats_16'], '</b><br />
<table class="ssi_table" width="50%">
<tr>
<th width="50%" align="left">', $txt[35], ': </th>
<th width="50%" align="left">', $txt['totalTimeLogged1'], '</th>
</tr>
';
foreach ($return['members'] as $totals)
{
echo ' <tr>
<td>', $totals['name'], '</td>
<td>', $totals['time_online'], '</td>
</tr>
';
}
echo '</table>
';
}


It doesn't give average times or total time for all members on the board as those stats didn't seem too important to me, but it includes four more stats from Stats.php.  So... not exactly the same code as envoy, but close.

thnx, just add it into ssi?

kegobeer

Yes, and call it like you would call any other ssi function.

ssi_timeOnline(); <- echo with 10 people listed
$topwasters = ssi_timeOnline('array'); <- returns 10 people into an array
ssi_timeOnline('echo', 5); <- echo with 5 people listed
"The truth of the matter is that you always know the right thing to do. The hard part is doing it." - Norman Schwarzkopf
Posting and you (Click "WATCH THIS MOVIE")

Advertisement: