<?
class search{
	var $db;
	var $tbprefix;
	var $table;         //查询表
	
	var $PageSize;     //每页多少条
	var $where;   	   //查询条件
	var $category_id;  //分类ID
	var $field;        //查询字段
	var $keyword;      //查询内容 
	
	function query()
	{
		$this->initialization();//初始化
		
		if(!$this->table or !$this->category_id or  !$this->field or !$this->keyword)
		{
			echo "<script>alert('对不起查询信息不齐全！无法查询!');
				   window.location='javascript:history.back(-1)';
				 </script>";
			exit;
		}
		
		if(is_numeric($this->field))
		{
			if($this->field==1)
			{
				$field='title';
			}elseif($this->field==2){
				
				$field='content';
			}else{
				$field='author';
			}
		}else{
			$field=$this->field;
		}
		
		if($this->where)
		{
			$sql = 'SELECT '.'a.*,b.category_id,b.category_name '.'FROM '.$this->tbprefix.$this->table.' a left JOIN '.$this->tbprefix.'category b ON a.category_id = b.category_id  '.$this->where;
		}else{
			if($this->act($field)) //判断字段是否存在
			{
				$sql = 'SELECT '.'a.*,b.category_id,b.category_name '.'FROM '.$this->tbprefix.$this->table.' a left JOIN '.$this->tbprefix.'category b ON a.category_id = b.category_id WHERE  b.category_id='.$this->category_id.' and a.'.$field.' like  "%'.$this->keyword.'%" ORDER BY a.post_time DESC';
			}else{
				$sql = 'SELECT '.'a.*,b.category_id,b.category_name '.'FROM '.$this->tbprefix.$this->table.' a left JOIN '.$this->tbprefix.'category b ON a.category_id = b.category_id WHERE  b.category_id='.$this->category_id.' ORDER BY a.post_time DESC';
			}
	    }
		
		$page = new ShowPage;
		if(!$this->PageSize)
		{
			$page->PageSize = 25;
		}else{
			$page->PageSize = $this->PageSize;
		}
		
		$page->Total = $this->db -> getCount($sql);
		$page->LinkAry = array(1,2);
		$re  = $this->db -> sql_query($sql." limit  ".$page->OffSet());
		$showpage = $page->ShowLink();
		$arr = $this->db -> sql_fetchrowset($re);
		
		return $arr;
	}
	
	function act($field)
	{
			$result = mysql_query("SELECT * FROM ".$this->tbprefix.$this->table);
					
			$fields = mysql_num_fields($result);	
			$i=0;
			while ($i < $fields)
			{
				$name = mysql_field_name ($result, $i); //取得字段名
				if($name==$field)
				{
					$act=1;
					break;
				}
				$i++;
			}
			
			if($act)
			{
				return true;
			}else{
				return false;
			}
	}
	
	function initialization()
	{
		if(!$this->db)
		{
			$this->db=$GLOBALS['db'];
		}
		
		if(!$this->tbprefix)
		{
			$this->tbprefix=$GLOBALS['tbprefix'];
		}
		
		if(!$this->table)
		{
			$this->table=$GLOBALS['table'];
		}

		if(!$this->PageSize)
		{
			$this->PageSize=$GLOBALS['PageSize'];
		}
		
		if(!$this->where)
		{
			$this->where=$GLOBALS['where'];
		}
		
		if(!$this->category_id)
		{
			$this->category_id=$GLOBALS['category_id'];
		}
		
		if(!$this->field)
		{
			$this->field=$GLOBALS['field'];
		}
		
		if(!$this->keyword)
		{
			$this->keyword=$GLOBALS['keyword'];
		}
		
		
		
	}
}
?>