Topic: CGI: Forum post stats

Here's a script I made to show post/topic statistics on my site pages with server side includes.

Coded in TCL, depends on TCL and mysqltcl.

Config script forumstats-config.cgi

#!/usr/bin/tclsh
### CONFIG ###

## Database host
set my_host "host"

## Database name
set my_name "database"

## Username
set my_user "username"

## Password
set my_pass "password"

## Forum stats table name (replace PREFIX with the prefix of your forum tables)
set my_table "PREFIX_forums"

## Forum online stats table name (replace PREFIX with the prefix of your forum tables)
set my_oltable "PREFIX_online"

## Path to direct access attempt log
set dalog "/tmp/stat-da-attempts.text"

## Full path to this script (relative to system, not webserver)
set filename "/var/www/cgi-bin/forumstats-config.cgi"

### END CONFIG ###
if {[string match -nocase $env(SCRIPT_FILENAME) $filename]} {
        set dasock [open $dalog a+]
        puts "Content-type: text/html\n"
        puts "You're not allowed to look at this! Go away!"
        puts $dasock "Direct access attempt from $env(REMOTE_ADDR) on [clock format [clock seconds]]"
        flush $dasock
        close $dasock
}

the main script forumstats.cgi

#!/usr/bin/tclsh
puts "Content-type: text/html\n"
###

source forumstats-config.cgi
package require mysqltcl

set mydb [::mysql::connect -host $my_host -user $my_user -password $my_pass -db $my_name]

set poststats [::mysql::sel $mydb "SELECT num_posts FROM $my_table" -flatlist]
set topicstats [::mysql::sel $mydb "SELECT num_topics FROM $my_table" -flatlist]
set online [::mysql::sel $mydb "SELECT ident FROM $my_oltable" -flatlist]

set totalposts [expr [join $poststats "+"]]
set totaltopics [expr [join $topicstats "+"]]
set avgposts [expr $totalposts/[llength $poststats]]
set avgtopics [expr $totaltopics/[llength $topicstats]]
if {$online == ""} {
        set onlinemsg "No users are currently logged in"
} elseif {[llength $online] == 1} {
        set onlinemsg "OK"
        set onlinecount [llength $online]
        set onlineunits "is user"
} else {
        set onlinemsg "OK"
        set onlinecount [llength $online]
        set onlineunits "are users"
}

puts "<div id=\"forumstats\">"
puts "There are $totalposts posts in $totaltopics topics in [llength $topicstats] forums<br />"
puts "Each forum has an average of $avgposts posts and $avgtopics topics<br />"

if {$onlinemsg != "OK"} {
        puts "$onlinemsg"
} else {
        puts "There [lindex [split $onlineunits] 0] currently $onlinecount [lindex [split $onlineunits] 1] logged in:<br /> \($online\)"
}
puts "</div>"
::mysql::close $mydb

If you change the filename of the config script, don't forget to update the filename on the line in the main script that begins with 'source'. Integrated into site with SSI 'include virtual' directive. Oh, it also uses good grammar for the totals tongue

Hope this helps someone wink