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 $_GET Example (live stats);
Controls ·  Search New posts · Mark unread

You are in: Forum / Forum / General / General Chat

2023-01-15 09:22:03
Example of a ajax get call.
Retrieve data and show on another page. We use $_GET  to identify a id

replace anything underneath this line to modify for your own needs
// count & update stats (internal only should be wrapped)

if any only can improve this simple example like how to pass the id better please post.

visit yourdomain/test.php?id=(ADD NUMBER HERE)

create test.php
<?php
// get id from url
$id = (int) $_GET['id'] ?? 0;
?>
<p id="load_torrent_stats"></p>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
   function torrent_stats(){
       // load stats from page with id from url
       $('#load_torrent_stats').load('stat.php?id=<?= $id ?>');
   }
   setInterval( "torrent_stats()", 150000 );
torrent_stats();
</script>

create stat.php - enter db details at top
<?php
// db stuff
$servername = "localhost";
$username = "dbusername";
$password = "dbpassword";
$dbname = "dbname";

try {
 $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
 $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
 echo "Connection failed: " . $e->getMessage();
}

// get id from url
$id = (int) $_GET['id'];

// what we want in array
$stats = [
   'seeders' => 0,
   'leechers' => 0
];

// count & update stats (internal only should be wrapped)
$sth = $conn->prepare("SELECT seeder FROM peers  WHERE torrent = $id");
$sth->execute();
$result = $sth->fetchAll();
foreach ($result as $row) {
   if ($row['seeder'] == 'no') {
       $stats['leechers']++;
   } else {
       $stats['seeders']++;
   }
}

var_dump($stats);
Last edited by M-jay warn on 2023-01-15 09:23:15
2023-01-15 19:47:18
JS:


<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
let requestQueue = $.when();
function torrent_stats() {
const t_id = <?=intval($_GET['id'])?>;
requestQueue = requestQueue.then(function() {
return $.ajax({
type: 'GET',
url: '/stat.php',
dataType: 'JSON',
data: {
id: parseInt(t_id)
},
beforeSend: function() {
$('#load_ajax_error').html(data.status).hide();
$('#load_torrent_stats').html('Fetching...');
},
success: function(data) {
let html = '';
data.forEach(item => {
let html += 'Seeders: ' + item.seeders + ', Leechers: ' + item.leechers;
});
$('#load_torrent_stats').html(html);
},
error: function(data) {
$('#load_ajax_error').html(data.status).show();
}
});
});
}
torrent_stats();
setInterval(torrent_stats, 150000);
</script>

HTML

<p id="load_torrent_stats"></p>
<p id="load_ajax_error"></p>


PHP

<?php
$servername = "localhost";
$username = "dbusername";
$password = "dbpassword";
$dbname = "dbname";

try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}

$peers = $conn->prepare('SELECT seeder, leecher FROM peers WHERE torrent = :id');
$peers->execute([':id' => intval($_GET['id'])]);

$results      = array();
if ($check = $peers->fetchAll()) {
foreach ($check as $row) {
$data['seeder']  = intval($row['seeder']);
$data['leecher'] = intval($row['leecher']);
array_push($results, $data);
}
die(json_encode($results, JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK));
} else {
die(json_encode(array('seeder' => 0, 'leecher' => 0)));
}


Play with the predefined constants. Since this is dealing with 2 integers all you need to do is use JSON_NUMERIC_CHECK to be certain the array field is in fact a number and only a number.

In my case, for ratings I had to use JSON_PRESERVE_ZERO_FRACTION to preserve a value of "0.0" or else it converts it to "0" (like intval, same result). This had me stuck for 2 hours! This was necessary as we depend on a float no matter what.

----

If you want to cram lots of data into a single request, and parse it on the other end I can show you how to do that too (both JS + PHP), its become an art. You can render your entire index by reading plain text (JSON). My index is JSON that modifies existing DOM, so elements within each row (views, hits, dl, peers) are all updated through JSON.

Its super lightweight and takes no work on reloading the entire website all over again, just takes that 1 ajax file. Its essentially 80-150kb of plain text and that makes for a good responsive experience.
Last edited by STorrents2020 on 2023-01-15 20:22:55
2023-01-16 05:47:23
thanks for example  👍
2023-01-16 18:26:39
Loop is just to show how it works. You dont need foreach if the fields are named the same. You can just do

json_encode($table->fetchAll())


and it sends the column names as the JSON field names (either case, you are sending a array over to json_encode()).
Last edited by STorrents2020 on 2023-01-16 18:27:50
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: 9
  • Members: 0
  • Guests online: 9
  • Comments: 3
  • Messages: 1,728