News:

Bored?  Looking to kill some time?  Want to chat with other SMF users?  Join us in IRC chat or Discord

Main Menu

How to send topic stats to external site?

Started by Drug Mile, July 12, 2019, 09:06:44 AM

Previous topic - Next topic

Drug Mile

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"

Drug Mile

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:
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:
<?php require("SSI.php");
ssi_count_reply(44); 
?>


Result, it is giving statistics for topic 44:
QuoteReplies: 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.

Advertisement: