<?php
if (!defined('webid')) {
    exit();
}
?>
<h3>Форум</h3>
<table class="otab" width="100%" border="0" cellpadding="0" cellspacing="1">
<thead style="background: #ebebeb;">
    <th style="padding: 5px;" align="left" colspan="2">Форум</th>
    <th style="padding: 5px;" width="30" align="center">Темы</th>
    <th style="padding: 5px;" width="135" align="center">Последняя тема</th>
</thead>
<?
$forum = mysql_query("SELECT * FROM tb_forum ORDER BY id ASC");
while ($frow = mysql_fetch_array($forum)) {
    ?>
    <tr>
        <td colspan="4" style="background: #FAF7BA; padding: 5px; color: #212121;"><?=$frow['forumname']?></td>
    </tr>
    <?
    $forumcat = mysql_query("SELECT * FROM tb_forum_category WHERE forumid = '" . $frow['id'] . "'");
    while ($fcrow = mysql_fetch_array($forumcat)) {
        $topicc = mysql_query("SELECT * FROM tb_forum_mes WHERE tid IN (SELECT id FROM tb_forum_topic WHERE catid = '" . $fcrow['id'] . "') ORDER BY date DESC");
		$topiccp = mysql_query("SELECT id, topicname, avtor, date FROM tb_forum_topic WHERE catid = '" . $fcrow['id'] . "' ORDER BY date DESC");
		if (mysql_num_rows($topiccp) > 0) {
            $count = mysql_num_rows($topiccp);
        } else {
            $count = 0;
        }
        $danie = mysql_fetch_array($topicc);
        $usname = mysql_fetch_array(mysql_query("SELECT username FROM tb_users WHERE id = '" . $danie['userid'] . "'"));
        $topicnames = mysql_fetch_array(mysql_query("SELECT topicname FROM tb_forum_topic WHERE id = '" . $danie['tid'] . "'"));
		
        ?>
        <tr>
            <td width="40" align="center" class="lt"><img src="<?=ROOT?>/theme/img/forum/img_forum.png" width="24" height="24" /></td>
            <td class="lt" align="left"><a href="/topic/<?=$fcrow['id']?>" class="link"><?=$fcrow['categoryname']?></a><br /><span style="color:#cccccc; font-size: 11px;"><?=$fcrow['des']?></span></td>
            <td align="center" class="rt"><?=$count?></td>
            <td class="lt" align="left">
			    <a href="/posts/<?=$danie['tid']?>" class="link"><?=substr($topicnames['topicname'], 0, 18)?></a><br /> 
				<font color="#212121"><?=$usname['username']?></font><br />
				<font color="#cccccc">
				<?
		        if (isset($_SESSION['username']) && isset($_SESSION['password'])) { 
		
		            $resxf = mysql_fetch_array( mysql_query( "SELECT(SELECT count(*) FROM forum_visit_status WHERE iduser = '".$_SESSION['iduser']."' AND idtopic = '".$danie['tid']."') forumvisitf" ) );
                    if ($resxf["forumvisitf"] == 0) 
					{
					    if($danie['mes'] == ''){echo '';}else{ 
		                ?>  
		                <font style='color: #339933;'>Новый ответ в теме</font>
						<image class="timeimgs" title="В данной теме появились свежие ответы которых вы еще не читали." align="right" width="14" height="14" src="theme/images/control_bottom_right.png" />
		                <?
						}					
					}
		            else
		            {
		                if($danie['mes'] == ''){echo '';}else{echo date('d.m.Y', $danie['date']);echo ' | ';echo date('H:i', $danie['date']);}
		            }
		        }
		        else
		        {
		            if($danie['mes'] == ''){echo '';}else{echo date('d.m.Y', $danie['date']);echo ' | ';echo date('H:i', $danie['date']);} 
		        }				  
				?>
				</font>
			</td>
        </tr>
        <?
    }
    ?>
    <?
}
?>
</table>