<?php

function listDiscuss($id,$num=10,$type_sql=false){
	global $db_tablepre,$cg_s;

	if ($type_sql == 1) $return_str = listDiscuss_str($id,$num,$type_sql);
	elseif ($cg_s=='yes') $return_str = listDiscuss_str($id,$num,$type_sql=3);
	else {
		$sql = "SELECT BC_id, BC_channel  FROM ".$db_tablepre."channel  WHERE BC_Tschannel=".$id.' order by BC_order asc ';
		$r = sql_return($sql,3);
		if ($r) {
			$r = sql_return($sql);
			$return_str = '';
			while ($row = sql_return($r,'FetchRow')) {
				$return_str .= '<div id="listTitle"><h1><span><a href="/discuss/index.php?id='.$row['BC_id'].'"><img src="images/right.gif" alt="goto" /></a></span> ◎ <a href="/discuss/index.php?id='.$row['BC_id'].'">'.$row['BC_channel'].'</a></h1></div>';
				$return_str .= listDiscussStr($row['BC_id'],$num,$type_sql);
			}
		}
		else $return_str = listDiscuss_str($id,$num,$type_sql=2);
	}
	return  $return_str;
}

function listDiscuss_str($id,$num=10,$type_sql){
	global $db_tablepre,$cg,$cg_title;
	$return_str = '';
	$sql = "SELECT BC_id,BC_channel  FROM ".$db_tablepre."channel  WHERE BC_id=".$id;
	$r = sql_return($sql,2);
	$return_str .= '<div id="listTitle"><h1><span><a href="/discuss/index.php?id='.$r['BC_id'].'"><img src="images/right.gif" alt="goto" /></a></span> ◎ <a href="/discuss/index.php?id='.$r['BC_id'].'">'.$r['BC_channel'];
	if ($type_sql==3) $return_str .= '   --   '.urldecode($cg_title);
	$return_str .= '</a></h1></div>';

	if ($type_sql==1) $return_str .= listDiscussStrTop($id,$num);
	elseif ($type_sql==2) $return_str .=  listDiscussContent($id,$type_sql);
	elseif ($type_sql==3) $return_str .=  listDiscussContent($id,$type_sql,$cg);
	else $return_str .= listDiscussStr($id,$num,$type_sql);

	return  $return_str;
}

function listDiscussStr($id,$num){
	global $db_tablepre;
	$sql_cg = "SELECT BC_categories FROM ".$db_tablepre."channel WHERE BC_id=(SELECT BC_Tschannel FROM ".$db_tablepre."channel WHERE BC_id=".$id.")";
	$result_cg = sql_return($sql_cg,2);
	$result_cg = explode('|',$result_cg['BC_categories']);
	array_pop($result_cg);
	foreach ($result_cg as $key=>$value){
		$return_str .= '
		<div id="listdiscuss_div">
		<ul>
<li id="listdiscuss_div_1i_1"><a href="/discuss/index.php?id='.$id.'&cg_s=yes&cg_title='.urlencode($value).'&cg='.$key.'"  target="_blank"><b>'.$value.': </b></a></li>';
		$sql = "SELECT discuss_id,discuss_bc_id,discuss_channel,discuss_title,discuss_uptimes  ";
		$sql .=" FROM ".$db_tablepre."discuss  WHERE  discuss_channel=".$id." AND discuss_cg=".$key." order by discuss_count_num desc limit ".$num;
		$result = sql_return($sql);
		while ($row = sql_return($result,'FetchRow')) {
			$return_str .= '<li id="listdiscuss_div_1i_2"><a href="/discuss/discuss.php?id='.$row['discuss_id'].'&channel='.$row['discuss_bc_id'].'"  target="_blank">'.$row['discuss_title'].'</a></li>';
			//.'</a>('.$row['discuss_pub_num'].') 
		}
		$return_str .= '</ul></div>';
	}
	return  $return_str;
}


function listDiscussStrTop($id,$num){
	global $db_tablepre;
	$sql = "SELECT BC_categories FROM ".$db_tablepre."channel WHERE BC_id=".$id;
	$result_cg = sql_return($sql,2);
	$result_cg = explode('|',$result_cg['BC_categories']);
	array_pop($result_cg);
	$sql = "SELECT BC_id,BC_channel FROM ".$db_tablepre."channel WHERE BC_Tschannel=".$id.' order by BC_order asc ';
	$result_channel = sql_return($sql);
	while ($row_channel = sql_return($result_channel,'FetchRow')) {
		$return_str .= '<div id="listdiscuss_div">
		<ul><li id="listdiscuss_div_1i_1"><a href="/discuss/index.php?id='.$row_channel['BC_id'].'"  target="_blank"><b>'.$row_channel['BC_channel'].'</b></a>:  </li>';
		$sql = "SELECT discuss_id,discuss_bc_id,discuss_title FROM ".$db_tablepre."discuss WHERE discuss_channel=".$row_channel['BC_id']." order by discuss_count_num desc limit ".$num;
		$result = sql_return($sql);
		while ($row = sql_return($result,'FetchRow')) {
			$return_str .= '<li id="listdiscuss_div_1i_2"><a href="/discuss/discuss.php?id='.$row['discuss_id'].'&channel='.$row['discuss_bc_id'].'"  target="_blank">'.$row['discuss_title'].'</a>  </li>';
		}
		$return_str .= '</ul></div>';
	}


	return  $return_str;
}

function  listDiscussContent($id,$type_sql,$cg=false){
	global $db_tablepre,$listDiscussNum;
	$num = $listDiscussNum;
	$return_str .= '<div id="listdiscuss_div"><ul>';
	$sql = "SELECT * FROM ".$db_tablepre."discuss ";
	if ($type_sql == 2) $sql .= " WHERE discuss_channel=".$id;
	elseif ($type_sql == 3) $sql .= " WHERE discuss_channel=".$id." AND discuss_cg=".$cg;
	
	$results  = sql_return($sql);
	$pageNum = sql_return($results,'count');
	$page = page_top($num,$pageNum);
	$num_2 = $num;
	$num = ($page*$num).",".$num;
	
	$sql .= " order by discuss_count_num desc limit ".$num;
	$result = sql_return($sql);
	while ($row = sql_return($result,'FetchRow')) {
		$return_str .= '<li id="listdiscuss_div_1i_2"><a href="/discuss/discuss.php?id='.$row['discuss_id'].'&channel='.$row['discuss_bc_id'].'"  target="_blank">'.$row['discuss_title'].'</a>  </li>';
	}
	$return_str .= '</ul></div>';

	$page_foot = page_foot($page,$num_2,$pageNum);
	$return_str .= '<div id="listPage">'.$page_foot.'</div>';

	return  $return_str;
}


function  discussListIndex($id,$num=20){
	global $db_tablepre,$channel,$timestamp;
	$sql = "SELECT  * FROM ".$db_tablepre."discuss_index WHERE d_i_discuss_id=".$id;
	$result  = sql_return($sql);
	$pageNum = sql_return($result,'count');
	$page = page_top($num,$pageNum);
	$num_2 = $num;
	$num = ($page*$num).",".$num;


	$chk_time = strtotime(date('Y-m-d',$timestamp));

	$sql = "SELECT d_i_id,d_i_click,d_i_reply,d_i_title,d_i_uid,d_i_u_name_2,d_i_lastReply_uid,d_i_r_u_name_2,d_i_last_uptimes,d_i_state,d_i_top FROM ".$db_tablepre."discuss_index WHERE d_i_discuss_id=".$id;

	$sql .= " order by d_i_top,d_i_last_uptimes desc limit ".$num;

	$result = sql_return($sql);
	$i=0;
	while ($row = sql_return($result,'FetchRow')) {
		if ($row['d_i_last_uptimes'] >= $chk_time) $d_i_last_uptimes = date("H:i",$row['d_i_last_uptimes']);
		else $d_i_last_uptimes = date("m-d",$row['d_i_last_uptimes']);

		$return_str .= '<tr';
		if ($i%2) $return_str .= ' class="tbl_alt"';
		$return_str .= '>
		<td>'.$row['d_i_click'].'</td>
		<td>'.$row['d_i_reply'].'</td>
		<td><a href="/discuss/discuss_list.php?id='.$row['d_i_id'].'&channel='.$channel.'" >'.$row['d_i_title'].'</a>';
		//target="_blank"
		if ($row['d_i_top']==1) $return_str .= '[<font class="red">置顶</font>]';
		if ($row['d_i_state']==1) $return_str .= '[<font class="red">精品</font>]';
		$return_str .= '</td>
		<td>'.$row['d_i_u_name_2'].'</td>
		<td>'.$row['d_i_r_u_name_2'].'&nbsp;  '.$d_i_last_uptimes.'</td>
		</tr>';
		$i++;
	}

	$page_foot = page_foot($page,$num_2,$pageNum);

	return  $return_str.'|<div id="listPage">'.$page_foot.'</div>';
}


function  discussListMain($id,$num=20){
	global $db_tablepre,$channel;

	$sql = "SELECT  * FROM ".$db_tablepre."discuss_main WHERE d_m_d_i_id=".$id;
	$result  = sql_return($sql);
	$pageNum = sql_return($result,'count');
	$page = page_top($num,$pageNum);
	$num_2 = $num;
	$num = ($page*$num).",".$num;

	$sql = "SELECT  d_m_uid,d_m_u_name_2,d_m_title,d_m_content,d_m_img,d_m_uptimes,d_m_order,d_m_d_i_id   FROM ".$db_tablepre."discuss_main WHERE d_m_d_i_id=".$id;

	$sql .= " order by d_m_id asc limit ".$num;

	$result = sql_return($sql);

	while ($row = sql_return($result,'FetchRow')) {
		$return_str .= '<a name="#'.$row['d_m_order'].'"></a>
<table width="72%" border="0" bgcolor="#FFFFFF">
<tr>
<td  height="17" align="center" width="3%" class="gray">'.$row['d_m_order'].'</td>
<td class="p1" align=left >
<font color=#0000cc>'.$row['d_m_title'].'</font>
</td></tr>
<tr><td></td>
<td align=left width="97%" >
<table class="wr" border="0" cellspacing="0" cellpadding="0"><tr><td class="gray1"> 
'.$row['d_m_content'].'
</td></tr></table>
</td></tr>
<tr><td height="17"></td><td class="p1"></td></tr>
<tr><td height="17"></td>
<td align=left class="au"><div class="uau">作者：'.$row['d_m_u_name_2'].'
<font class="gray1"> '.date("Y-m-d H:i:s",$row['d_m_uptimes']).' </font>　
 <a href="#sub" class=t onclick="ResetReplyTitle('.$row['d_m_order'].',\''.$row['d_m_title'].'\');">回复此发言</a> &nbsp; 
</div></td></tr>
<tr><td></td><td><hr align=left width="87%" size=1 >
</td>
</tr></table>
';

		$d_o = $row['d_m_order'];
	}

	$page_foot = page_foot($page,$num_2,$pageNum);
	$return_str .= '<div id="listPage">'.$page_foot.'</div>';

	return  $return_str.'|'.$d_o;
}

function  discuss_presentation($id){
	global $db_tablepre;
	$sql = "SELECT discuss_presentation  FROM ".$db_tablepre."discuss  WHERE discuss_id=".$id;
	$row = sql_return($sql,2);
	return  $row['discuss_presentation'];
}

?>