<?php 
/*
+---------------------------------------
* UBB 列表转换类
+---------------------------------------
*/
class sqllist{
	public $titlenums;
	public $style;
	public $mkstr;
	public $type;
	
	public function get_sql_list($mkstr,$gro,$nums,$type,$titlenums,$style){	
		$this   ->  titlenums = $titlenums;
		$this   ->  style     = $style;
		$this   ->  type      = $type;
		$this   ->  mkstr     = $mkstr;
		$wapid  =   WAPID;
		$where  =   $this -> where($gro);
		$order  =   $this -> order();
		$setSql =   "SELECT * FROM {$mkstr} WHERE {$where}{$order} LIMIT {$nums}";
		$array  =   $this->sql_array($setSql);
		if($array == NULL){
			return '暂无记录';
		}
		foreach($array as $value){
			$html[] = $this->liststyle($value);
		}
		if($this->style == 5||$this->style == 4){
		return implode('',$html);;
		}else{
		return implode('<br>',$html);
		}
	}
	public function sql_array($sql){
	$sql_sel = $sql;
	$sql_array = mysql_query($sql_sel);
	/*获取查询的字段*/
	for($i=0;$i<mysql_num_fields($sql_array);$i++){
        $meta = mysql_fetch_field($sql_array);
        $fields[$i] = $meta->name;
    }
	/*遍历每一条记录*/
	$i = 0;
	if($sql_array && mysql_num_rows($sql_array) > 0){
		while($array_value = mysql_fetch_row($sql_array)){
			/*遍历每一个值*/
			$n = 0;
			foreach($array_value as $value){
			$f = $fields[$n];
			/*把值对应值三维数据*/
			$array[$i][$f] = $value;
			$n++;
			}
			$i++;
		}
		return $array;
		}else{
		return FALSE;
		}
	}
    
	public function liststyle($array){
		$urlbs          =  $this->mkstr;
		$id             =  $array[$this->getarrid()];
		$content        =  $array['content'];
		$contentpic     =  $array['spic'];
		if($contentpic != NULL && $this->mkstr == 'article')
		{
			$pic = explode("|",$contentpic);
			$img = '<img src="'.str_replace("320_240","100_100",'http://'.$_SERVER['HTTP_HOST'].'/article/'.$pic[0]).'" alt="'.$title.'" >';
		}
		elseif($contentpic != NULL && $this->mkstr == 'download')
		{
			$pic = explode("|",$contentpic);
			$img = '<img src="/download/'.$pic[0].'" alt="'.$title.'" >';
		}
		else
		{
			$img = '<img src="http://'.$_SERVER['HTTP_HOST'].'/style/no.png" alt="'.$title.'" >';
		}
		if($this->titlenums == 0){
		$title = $arr['title'];
		}else{
		$title = mb_substr($arr['title'],0,$this->titlenums,"UTF-8");
		}	
		$title = $array['title'];
		$gro   = $array['gro'];
		$class   = $this->classname($gro);  
		$style = $this->style;
		$style = $style == null ? 0 : $style;
		switch($style){
		case '0':
			$list = '<a href="http://'.$_SERVER["HTTP_HOST"].'/'.$urlbs.'/content.php?cid='.$gro.'&id='.$id.'">'.$title.'</a>';
		break;
		case '2';
			$list = '<a href="http://'.$_SERVER["HTTP_HOST"].'/'.$urlbs.'/content.php?cid='.$gro.'&id='.$id.'">['.$class.']'.$title.'</a>';	
		break;
		case '3':
			$list = '[<a href="http://'.$_SERVER["HTTP_HOST"].'/index.php?cid='.$gro.'">'.$class.'</a>]<a href="http://'.$_SERVER["HTTP_HOST"].'/'.$urlbs.'/content.php?cid='.$gro.'&id='.$id.'">'.$title.'</a>';	 
		break;
		case '4':
			$list = '<li><a href="http://'.$_SERVER["HTTP_HOST"].'/'.$urlbs.'/content.php?cid='.$gro.'&id='.$id.'">'.$title.'</a></li>';	 
		break;
		case '5':
		if($this->mkstr == 'article')
		{
			$list = '<div class="list5"><a href="http://'.$_SERVER["HTTP_HOST"].'/'.$urlbs.'/content.php?cid='.$gro.'&id='.$id.'"><table  border="0" cellpadding="0" cellspacing="0"><tr><td class="list_pic">'.$img.'</td><td class="list5_r"><div class="list5_title">'.$title.'</div><div class="list5_content">'.date("Y/m/d H:i:s",$array['time']).'</div></td></tr></table></a></div>';
		}elseif($this->mkstr == 'download')
		{
			$list = '<div class="d_list5"><a href="http://'.$_SERVER["HTTP_HOST"].'/'.$urlbs.'/content.php?cid='.$gro.'&id='.$id.'"><table  border="0" cellpadding="0" cellspacing="0"><tr><td class="d_list_pic">'.$img.'</td><td class="d_list5_c"><div class="d_list5_title">'.$title.'</div><div class="d_list5_content">'.date("Y/m/d H:i:s",$array['file_time']).'</div></td><td class="d_list_r"><a href="http://'.$_SERVER['HTTP_HOST'].'/download/content.php?act=download&cid='.$gro.'&id='.$id.'">立即下载</a></td></tr></table></a></div>';
		}
			break;
	}
		return $list;	 
}
	public function getzd($mk)
	{
		switch($mk)
		{
			case 'chat':
			$f['title'] = 'content';
			$f[''];
			break;
			default:
			break;
		}
	
	}
	public function getarrid(){
	$mkstr = $this->mkstr;
	$result = mysql_query("select * from {$mkstr}");
    for($i=0;$i<1;$i++){
        $meta = mysql_fetch_field($result);
        return $meta->name;
		}
	}
	
	public function classname($gro){
		$sql="SELECT cn,cid FROM class WHERE cid='{$gro}'";
		$results = mysql_query($sql);
		list($cn,$cid) = mysql_fetch_row($results);
		return $cn;
	}
	
	public function where($cid){
	if($cid == 0){
			$where = " siteid='".WAPID."' ";
		}else{
			$arr =  explode('|',$cid);
			if(count($arr)>1){
			$in = implode(",",$arr);
			$where = " gro IN(".$in.") AND siteid='".WAPID."'";
			}else{
			$where = 'gro='.$cid.' AND siteid=\''.WAPID.'\'';
			}
		}
		return $where;
	}
	
	public function order(){
	$mk = $this->mkstr;
	$type = $this->type;
		if($mk == 'bbs'){
		if($type == 0){
		$type='rand()';
			}elseif($type == 1){
		$type = 'bbsid';
			}elseif($type == 2){
		$type = 'v';
			}elseif($type == 3){
		$type='rand()';
			}elseif($type == 4){
		$type='bbsid';
		$sqland = ' AND good=\'1\' ';
			}elseif($type == 7){
		$type = 'view_time';
			}
		}elseif($mk=='article'){
				if($type==0){
		$type='rand()';
			}elseif($type == 1){
		$type = 'articleid';
			}elseif($type == 2){
		$type = 'v';
			}elseif($type == 3){
		$type = 'rand()';
		}
		}elseif($mk=='sitelink'){
			if($type == 0){
		$type='rand()';
		}elseif($type == 1){
		$type = 'sitelinkid';
		}elseif($type == 2){
		$type = 'v';
		}elseif($type ==3){
		$type='rand()';
		}
		}elseif($mk == 'download'){
		if($type == 0){
		$type='rand()';
		}elseif($type == 1){
		$type = 'fileid';
			}
		}
		return " {$sqland} ORDER BY {$type} DESC ";
	}
}
?>