View Single Post
  #1 (permalink)  
Old 07-20-2005
imported_envoy imported_envoy is offline
Administrator
 

Join Date: Jul 2002
Posts: 600
Send a message via MSN to imported_envoy
[CODE] Wasted time online for SMF Forum

OK, I just wrote this to show you guys a simple script for getting the top 10 users who have wasted the most amount of time on the board, hehe. 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 Simple Machines Forum 1.0.5. I am not sure if it works on 1.1b3. I would do the same thing for phpbb, but phpbb doesn't count the total time a user wastes on a forum.

Code:
<?
// 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:
Click Here
Reply With Quote