<?php
function list_style_fun($style,$class,$gro,$id,$title,$urlbs,$spc){
//$class 栏目名称
//$gro   所属ID
//$id    资源ID
//$title 标题
//$urlbs 标示名称
if($spc == 0){
$br = '';
}else{
$br = '<br>';
}
	 if($style == NULL || $style == 0){
		$list = $br.'<a href="http://'.$_SERVER["HTTP_HOST"].'/'.$urlbs.'/content.php?cid='.$gro.'&id='.$id.'">'.$title.'</a>';
			}elseif($style==2){
		$list = $br.'<a href="http://'.$_SERVER["HTTP_HOST"].'/'.$urlbs.'/content.php?cid='.$gro.'&id='.$id.'">['.$class.']'.$title.'</a>';	 
			}elseif($style==3){
		$list = $br.'[<a href="http://'.$_SERVER["HTTP_HOST"].'/index.php?cid='.$cid.'">'.$class.'</a>]<a href="http://'.$_SERVER["HTTP_HOST"].'/'.$urlbs.'/content.php?cid='.$gro.'&id='.$id.'">'.$title.'</a>';	 
			}elseif($style==4){
		$list = '<li><a href="http://'.$_SERVER["HTTP_HOST"].'/'.$urlbs.'/content.php?cid='.$gro.'&id='.$id.'">'.$title.'</a></li>';	 
			}
		return $list;	 
}

function maxlist($where,$from,$order){
	$result = mysql_query("SELECT {$order} FROM {$from} WHERE {$where} ORDER BY {$order} ASC LIMIT 20");      
		if($result && mysql_num_rows($result) > 0){
	$i = 1;
		while(list($lbbsid) = mysql_fetch_row($result)){
		$limit_id = $lbbsid;
		$i++;
		}
	}else{
	//die('ERROR');
	}
	return $limit_id;
}


function list_article($wapid,$type,$where,$listnum,$start,$titlenum,$style){
	$max_limit = " AND articleid<='".maxlist($where,'article','articleid')."'";
			if($type==0){
		$type='rand()';
			}elseif($type == 1){
		$type = 'articleid';
			}elseif($type == 2){
		$type = 'v';
			}elseif($type == 3){
		$type = 'rand()';
	}
	$sql="SELECT articleid,gro,title,v,content,time FROM article WHERE {$where}{$max_list} ORDER BY {$type} DESC LIMIT {$start},{$listnum}"; 
	$result = mysql_query($sql);      
				if($result && mysql_num_rows($result) > 0){
			$i = 0;
			while(list($articleid,$gro,$title,$v,$content,$time) = mysql_fetch_row($result)){
				$sql="SELECT cn,class FROM class WHERE cid='{$gro}'";
				$results = mysql_query($sql);
				list($cn,$class) = mysql_fetch_row($results);
			if($titlenum == 0){
		$title = $title;
			}else{
		$title = mb_substr($title,0,$titlenum,"UTF-8");
			}
		$list.=list_style_fun($style,$cn,$gro,$articleid,$title,'article',$i);
		$i++;
		}
		mysql_free_result($result);
		}else{
	$list = '暂时没有记录';
}

return $list;
}

//友情链接UBB调用函数
function list_sitelink($wapid,$type,$where,$listnum,$start,$titlenum,$style){
	$max_limit = 'AND sitelinkid<='.maxlist($where,'sitelink','sitelinkid');
		if($type == 0){
	$type='rand()';
	$max_limit=NULL;
		}elseif($type == 1){
	$type = 'sitelinkid';
	$max_limit = NULL;
		}elseif($type == 2){
	$type = 'v';
	$max_limit = NULL;
		}elseif($type ==3){
	$type='rand()';
	}
	$sql = "SELECT sitelinkid,gro,title,v,content,time,list,siteid,url FROM sitelink WHERE {$where} ORDER BY {$type} DESC LIMIT {$start},{$listnum}";
	/*执行数据查询语句*/  
	$result = mysql_query($sql);
	if($result && mysql_num_rows($result) > 0){
	$i=0;
	while(list($sitelinkid,$gro,$title,$v,$content,$time,$list,$siteid,$url) = mysql_fetch_row($result)){
		$sql="SELECT cn,class FROM class WHERE cid='{$gro}'";
		$results = mysql_query($sql);
		list($cn,$class) = mysql_fetch_row($results);
		if($titlenum == 0){
			$title = $title;
		}else{
			$title = mb_substr($title,0,$titlenum,"UTF-8");
		}
		$list2.=list_style_fun($style,$cn,$gro,$sitlinkid,$title,'sitelink',$i);
		$i++;
	}
mysql_free_result($result);
}else{
		$list2 = NULL;
	}

//返回数据
return $list2; 
} 



function list_bbs($wapid,$type,$where,$listnum,$start,$titlenum,$style){
	$max_limit = 'AND bbsid<='.maxlist($where,'bbs','bbsid');
			if($type == 0){
		$type='rand()';
		$max_limit = NULL;
			}elseif($type == 1){
		$type = 'bbsid';
		$max_limit = NULL;
			}elseif($type == 2){
		$type = 'v';
		$max_limit = NULL;
			}elseif($type == 3){
		$type='rand()';
			}elseif($type == 7){
		$type = 'view_time';
		$max_limit = NULL;
	}

	$sql="SELECT bbsid,gro,title,v,content,time,view_time FROM bbs WHERE {$where}{$max_limit} ORDER BY {$type} DESC LIMIT {$start},{$listnum}"; 
		$result = mysql_query($sql);      
		if($result && mysql_num_rows($result) > 0){
		$i = 0;
			while(list($bbsid,$gro,$title,$v,$content,$time,$view_time) = mysql_fetch_row($result)){
				$sql="SELECT cn,cid FROM class WHERE cid='{$gro}'";
				$results = mysql_query($sql);
				@list($cn,$cid) = mysql_fetch_row($results);
				//文章列表标题字数限制
					if($titlenum == 0){
				$title = $title;
					}else{
				$title = mb_substr($title,0,$titlenum,"UTF-8");
					}
			$list.=list_style_fun($style,$cn,$gro,$bbsid,$title,'bbs',$i);
			$i++;
			}
		mysql_free_result($result);
		}else{
			$list = NULL;
		}
		//返回数据
		return $list;
	}



function list_download($wapid,$type,$where,$listnum,$start,$titlenum,$style){
	$sql="SELECT fileid,siteid,gro,file_name,file_path,file_ver,file_time,file_end,file_content,file_v,file_down,file_pic,file_auter,file_lan,file_pt FROM download WHERE {$where}";
		$result = mysql_query($sql);      
			if($result && mysql_num_rows($result) > 0){
			$i = 0;
			while(list($fileid,$siteid,$gro,$file_name,$file_path,$file_ver,$file_time,$file_end,$file_content,$file_v,$file_down,$file_pic,$file_auter,$file_lan,$file_pt) = mysql_fetch_row($result)){
				$sql="SELECT cn,cid FROM class WHERE cid='{$gro}'";
				$results = mysql_query($sql);
				list($cn,$cid) = mysql_fetch_row($results);
			if($titlenum == 0){
				$title = $file_name;
			}else{
				$title = mb_substr($file_name,0,$titlenum,"UTF-8");
			}
				$list .= list_style_fun($style,$cn,$gro,$fileid,$title,'download',$i);
				$i++;
			}
		mysql_free_result($result);
	}else{
	$list = NULL;
}

return $list;
}


function main_list_fun($wapid,$mk,$cid,$listnum,$type,$titlenum,$style,$start){
if($listnum > limit_nums ){
	$listnum = limit_nums;
}

	if($start==NULL||$start>0||$start==''){
			$start   = 0;
		}else{
			$start   = $start;
		}
		if($cid == 0){
			$where = "siteid='".$wapid."'";
		}else{
			$arr =  explode('|',$cid);
			if(count($arr)>1){
		$i = 1;
		foreach($arr as $value){
			if($i == 1){
		$s = NULL;
			}else{
		$s = ',';
			}
			$in .= $s.$value;
		$i++;
	}
	$where = "gro IN(".$in.") AND siteid=".$wapid;
		}else{
	$where = 'gro='.$cid.' AND siteid='.$wapid;
		}
	}
		if($mk == 'article'){
		
		$echo = list_article($wapid,$type,$where,$listnum,$start,$titlenum,$style);
			
			}elseif($mk == 'bbs'){
			
		$echo = list_bbs($wapid,$type,$where,$listnum,$start,$titlenum,$style);
			
			}elseif($mk == 'sitelink'){
		
		$echo = list_sitelink($wapid,$type,$where,$listnum,$start,$titlenum,$style);
			
			}elseif($mk == 'download'){
		
		$echo = list_download($wapid,$type,$where,$listnum,$start,$titlenum,$style);
		
		}
return $echo;
	}
	
	

?>