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
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
create stat.php - enter db details at top
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
on 2023-01-15 09:23:15
on 2023-01-15 09:23:15