<?php
require(dirname(__FILE__)."/"."global.php");

$keyword=str_replace("%","\%",$keyword);
$keyword=trim($keyword);
$rows=20;


if( ($_GET[type]||$_POST[type]) && !$keyword )
{
	showerr("關鍵字不能為空");
}


if( $_GET[keyword] || $_POST[keyword] )
{
	$search_module=0;
	if(!$type)
	{
		$type='username';
	}
	if(!$web_admin)
	{
		if(!$groupdb[SearchArticleType]){
			showerr("你所在用戶組,無權使用搜索!");
		}
	}

	if($_POST[keyword]&&$_COOKIE[searchTime])
	{
		showerr("3秒鐘內,請不要重復提交查詢");
	}
	setcookie("searchTime",1,$timestamp+3);

	$SQL=" 1 ";

	if($type=='id')
	{
		$SQL.=" AND A.id = '$keyword' ";
	}
	elseif($type=='username')
	{
		$SQL.=" AND BINARY A.username = '$keyword' ";
	}
	elseif($type&&$mid&&is_table("{$pre}form_content_$mid")&&table_field("{$pre}form_content_$mid",$type))
	{
		$SQL.=" AND BINARY C.$type LIKE '%$keyword%' ";
		$search_module=1;
	}

	if($mid&&is_table("{$pre}form_content_$mid"))
	{
		$search_url='';
		foreach( $postdb AS $key=>$value)
		{
			if( $value!='' && table_field("{$pre}form_content_$mid",$key) )
			{
				$SQL.=" AND C.`$key`='$value' ";
				$rsdb[$key][$value]=" selected ";
				$search_module=1;
			}
			$value=urlencode($value);
			$search_url.="&postdb[{$key}]=$value";
		}
	}

	if($page<1)
	{
		$page=1;
	}
	$min=($page-1)*$rows;

	$_moduleSql=$_moduleSqlSearch="";
	if($search_module==1)
	{
		$_moduleSql=" LEFT JOIN {$pre}form_content_$mid C ON A.id=C.id ";
		$_moduleSqlSearch=" C.*, ";
	}
	
	$showpage=getpage("{$pre}form_content A $_moduleSql","WHERE $SQL","?type=$type&$search_url&keyword=".urlencode($_POST[keyword]).urlencode($_GET[keyword])."",$rows);

	unset($listdb);

	$query = $db->query("SELECT {$_moduleSqlSearch}A.* FROM {$pre}form_content A $_moduleSql WHERE $SQL  ORDER BY A.id DESC LIMIT $min,$rows");
	while($rs = $db->fetch_array($query))
	{
		if(!$IS_BIZ)continue;
		$rs[posttime] = date("Y-m-d",$rs[posttime]);
		$listdb[] = $rs;
	}
	
	if(!$listdb)
	{
		showerr("很抱歉,沒有找到你要查詢的內容");
	}

	
	$typedb[$type]=' selected ';
}


$module_select="<select name='mid' onChange=\"window.location.href='?mid='+this.options[this.selectedIndex].value\">";
$query = $db->query("SELECT * FROM {$pre}form_module ORDER BY list DESC");
while($rs = $db->fetch_array($query)){
	$ckk=$mid==$rs[id]?' selected ':' ';
	$module_select.="<option value='$rs[id]' $ckk>$rs[name]</option>";
}
$module_select.="</select>";


$type||$type='username';
$typedb[$type]=" checked ";


require(PHP168_PATH."inc/head.php");
require(PHP168_PATH."php168/form_tpl/search_$mid.htm");
require(PHP168_PATH."inc/foot.php");

?>