Advertisement:

Author Topic: How to send topic stats to external site?  (Read 693 times)

Offline Drug Mile

  • Semi-Newbie
  • *
  • Posts: 25
  • Gender: Male
    • Perforum
How to send topic stats to external site?
« on: July 12, 2019, 09:06:44 AM »
I've been looking into SSI functions manual as described here: https://www.simplemachines.org/community/ssi_examples.php

...but still not sure if I can use it to get stats from specific topic. I.e. I create article outside of SMF and in the bottom I link it to discussion on forum. At the moment, link only shows "Read comments", but would be nice if link looks like this "Read (88) comments, latest 16/Jun/2019 15:54 by Dragon"

Offline Drug Mile

  • Semi-Newbie
  • *
  • Posts: 25
  • Gender: Male
    • Perforum
Re: How to send topic stats to external site?
« Reply #1 on: July 12, 2019, 05:43:11 PM »
Messing up with SQL queries for the first time so I'm not sure if I'm doing this correct, but this appears to work:

I just added new function at the bottom of ssi.php:
Code: [Select]
function ssi_count_reply($topicid)
{
$servername = "localhost";
$username = "xxx";
$password = "xxx";
$dbname = "xxx";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT `num_replies`,`num_views`,`id_last_msg` FROM `smf_topics` WHERE `id_topic`=$topicid";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "Replies: " . $row["num_replies"]. " <br> Views: " . $row["num_views"]. " <br>";
$lastmsg=$row["id_last_msg"];
    }
}
else {
    echo "0 results";
}

$sql = "SELECT `poster_time` FROM `smf_messages` WHERE `id_msg`= $lastmsg";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "Time: " . timeformat($row["poster_time"]). "<br>";
    }
}
else {
    echo "0 results";
}
$conn->close();
}

And then created simple page that is calling new function:
Code: [Select]
<?php require("SSI.php");
ssi_count_reply(44); 
?>

Result, it is giving statistics for topic 44:
Quote
Replies: 6
Views: 2677
Time: 25 February, 2014, 14:00:27

Please check if my code is safe enough or needs to be altered to work better.