<?php
$text="";
	if (! $lchyphname) { $lchyphname=lchyph($name); }
	$filename="latestforums/".$lchyphname.".txt";
        $text=getsavedfile($filename,3);
        if (! $text) {

$text='
            <div class="sidetitle">
		Game Discussions
            </div>
            <div class="sidebox" id="latest_forums">
';
connect(supercheats_forum);
	$latest=array();
        $query="SELECT id FROM forums WHERE name='".addslashes($name)."'";
        $result = mysql_query($query) or die(mysql_error());
        if (mysql_num_rows($result)) {
        $forum = mysql_fetch_array($result, MYSQL_ASSOC);
        $query="SELECT title,postscount,id,first_postid FROM topics WHERE forumid='".$forum["id"]."' and (status='ACTIVE' or status='PINNED') ORDER BY last_postid DESC LIMIT 6";
        $result = mysql_query($query) or die(mysql_error());
        if (mysql_num_rows($result)) {
                while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
                        $latest[]=$row;
                }
        }
        }
	$m=8-count($latest);if ($m>5) { $m=5; }
	$query="SELECT title,postscount,id,first_postid FROM topics WHERE forumid='531' and (status='ACTIVE' or status='PINNED') ORDER BY last_postid DESC LIMIT $m";
        $result = mysql_query($query) or die(mysql_error());
        if (mysql_num_rows($result)) {
                while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
                        $general[]=$row;
                }
        }


        if ($latest) {
                $text.='
                        <p class="gi"><span class="fright">Posts</span>Discussion</p>';
                foreach ($latest as $row) {
			$pquery="SELECT status FROM fullposts WHERE id='".$row["first_postid"]."'";
                        $presult = mysql_query($pquery) or die(mysql_error());
                        $prow = mysql_fetch_array($presult, MYSQL_ASSOC);
                        if ($prow["status"]=="") {
                        $text.='<p class="gi"><span class="fright">'.$row["postscount"].'</span><a href="https://forums.supercheats.com/video-games/'.lchyph($name).'/'.$row["id"].'/'.urltext($row["title"]).'.htm">'.$row["title"].'</a></p>';
			}
                }
        } else {
                $text.='<p>No discussions open for '.$name.' at the moment. </p>';
        }
	$text.='<p><a href="/calls/starttopic.php?forum='.$forum["id"].'" rel="modal:open" class="sc-button">Start a chat</a></p>';
	if ($general) {
		$text.='<hr size="1" />';
                foreach ($general as $row) {
			$pquery="SELECT status FROM topics WHERE id='".$row["first_postid"]."'";
                        $presult = mysql_query($pquery) or die(mysql_error());
                        $prow = mysql_fetch_array($presult, MYSQL_ASSOC);
                        if ($prow["status"]=="") {
                        $text.='<p class="gi"><span class="fright">'.$row["postscount"].'</span><a href="https://forums.supercheats.com/topic.php?topic='.$row["id"].'">'.$row["title"].'</a></p>';
			}
                }
	}
	$text.='<hr size="1" /><p>';
	#$text.='Get Involved and join the fun. Go to our <a href="https://forums.supercheats.com/video-games/'.$lchyphname.'">'.$name.' forums</a> to talk about the game, or visit our other forums to chat to gamers.';
        $text.='</p>
	<div class="cboth_sm">&nbsp;</div>
	';
	#$text.='
        #<div class="fleft"></div>
        #<div class="fright"><a href="https://forums.supercheats.com/" class="sc-button">Forums</a></div>
        #<div class="cboth_sm">&nbsp;</div>';

connect(supercheats);
$text.='
            </div>
            <div class="cboth">&nbsp;</div>
';
	putsavefile($filename,$text);
	}

echo $text;
?>

