Login
Username: Password:
Remember me

[Sign Up] / [Recover Password]

Theme / Language

Popular
New Torrents
new name
Size: 18.40 MB
Most active
new name
S: 74 L: 3
New Posters
AJAX/PDO Usage Stats
Controls ·  Search New posts · Mark unread

You are in: Forum / Forum / Torrent Trader / v3 Mods Completed

2020-05-21 10:28:30
Grab stats dynamically. Automatically updates users ratio, upload, download, slots and invites. Updates live. No need to refresh. Very good system. 1 week in use, only 1 update to byte calculation.

Here we go..

create as ajax_stats.php


<?
// Grab user Ratio, Download, Upload & Slot details dynamically
// Auto updates as they download and browse
require_once 'backend/functions.php';

$userid         = isset($_GET['id'])?intval($_GET['id']):0;

try {
   $conn = new PDO("mysql:host={$dbHost};dbname={$dbName}", $dbUsername, $dbPassword);
   $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e) {
   die('Unable to connect to database');
}

if ($CURUSER["uploaded"] > 0 && $CURUSER["downloaded"] == 0)
   $userratio = "Inf.";
elseif ($CURUSER["downloaded"] > 0)
   $userratio = number_format($CURUSER["uploaded"] / $CURUSER["downloaded"], 2);
else
   $userratio = "---";

$stmt = $conn->prepare("SELECT users.downloaded, users.uploaded, users.invites, users.id, COUNT(DISTINCT peers.torrent) AS slots FROM peers LEFT JOIN users ON peers.userid = users.id WHERE peers.userid = $userid");
$stmt->execute();
$array = array();
if ($stmt->rowCount() > 0) {
   $userlivestats = $stmt->fetchAll(PDO::FETCH_ASSOC);
   foreach ($userlivestats as $link => $row) {

       $row['downloaded']     = $link['downloaded'];     // Downloaded
       $row['uploaded']     = $link['uploaded'];    // Uploaded
       $row['invites']     = $link['invites'];        // Invites
       $row['slots']         = $link['slots'];       // Download Slots
       $row['class']         = $link['class'];       // User class
       array_push($array, $link);
   }
}
echo json_encode($userlivestats);

?>


Now open header.php and add


<? if ($CURUSER) { ?>
<script>
function formatBytes(bytes, decimals = 2) {
   if (bytes === null) return '0.00 Bytes'; // null in place of 0 - new user byte stats fix

   const k = 1024;
   const dm = decimals < 0 ? 0 : decimals;
   const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];

   const i = Math.floor(Math.log(bytes) / Math.log(k));

   return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
}

function fetchUserUsage() {
   $(document).ready(function () {
       $.ajax({
           url: 'ajax_stats.php?id=<?=$CURUSER["id"]; ?>',
           method: "GET",
           dataType: 'JSON',
           success: function (data) {
               var html_to_append = '';
               var ratio = '<? echo number_format($CURUSER["uploaded"] / $CURUSER["downloaded"], 2); ?>';
               var slots = '<? $maxslot = avail_slots($CURUSER["id"], $CURUSER["class"]); echo number_format($maxslot) . "/0"; ?>';
               $.each(data, function (i, item) {
                   html_to_append +=
                       '<div style="padding-left:15px;padding-top:20px"><span style="font-size:25px"><i class="material-icons" style="color:#E72225;font-size:35px;vertical-align:bottom;padding-right:10px">cloud_download</i>' +
                       formatBytes(item.downloaded) +
                       '</span><span style="font-size:25px;padding-left:30px"><i class="material-icons" style="color:rgba(46,168,4,1);font-size:35px;vertical-align:bottom;padding-right:10px">cloud_upload</i>' +
                       formatBytes(item.uploaded) +
                       '</span><span style="font-size:25px"><span style="font-size:25px;padding-left:50px"><i class="material-icons" style="color:#C0C0C0;font-size:35px;vertical-align:bottom;padding-right:10px">timelapse</i>' +
                       ratio +
                       '</span><span style="display:none;font-size:25px;padding-left:50px"><i class="material-icons" style="color:#F4964E;font-size:35px;vertical-align:bottom;padding-right:10px">reply</i> <a href="/invite.php" style="color:#000;text-decoration:underline">' +
                       item.invites +
                       '</a></span><span style="font-size:25px;padding-left:50px"><i class="material-icons" style="color:#0079FE;font-size:35px;vertical-align:bottom;padding-right:10px">widgets</i>' +
                       slots +
                       '</span></div>';
               });
               $("#userusage").html(html_to_append);
           }
       });
   });
}
fetchUserUsage();
setInterval(function () {
   fetchUserUsage()
}, 5000);
</script>
<? } ?>


Now add to very end of header.php


<?
if (stripos($_SERVER['PHP_SELF'],'index.php') && $CURUSER) {

   // AJAX User usage - May 21, 2020
   print("My Ratio to Members will go here..");
   print("<div id=\"userusage\"></div>");
}
?>


Change the page in code above (index.php), to the page you want only to show this on. Otherwise, use this code to show on ALL pages:


<?
if ($CURUSER) {

   // AJAX User usage - May 21, 2020
   print("My Ratio to Members will go here..");
   print("<div id=\"userusage\"></div>");
}
?>


On my end (updates by staring at it)

[ATTACH type="full" alt="UsageMod.png"]5547[/ATTACH]

As you can see, I've ended up dropping the download and upload objects in JSON in place of real data, which contradicts the script a bit but until I can calc it.. this is as good as it gets! Average speed will be added later on as a test feature to our private community.
UsageMod.png
UsageMod.png
2020-05-22 01:21:19
thank you very much, i will add it to my next theme (stargate)  ?
2020-05-22 05:13:14
"spiritblue, post: 68631, member: 401":

thank you very much, i will add it to my next theme (stargate)  ?

Very welcome ! I love this mod
Users online

No users online

Search



Statistics

    Torrents

  • Tracking: 3
  • Todays torrents: 0
  • Seeders: 0
  • Leechers: 0
  • Peers: 0
  • Downloaded: 89.96 GB
  • Uploaded: 9.86 GB

  • Members

  • We have: 838
  • New today: 0
  • Guest today: 9

  • Online

  • Total online: 6
  • Members: 0
  • Guests online: 6
  • Comments: 3
  • Messages: 1,728