<?php
/*-------------------------------------
	版权所有：南京牛昆网络科技有限公司（TEL-15896098989）
	程序版本：1.0
	更新日期：2015-02-09 
-------------------------------------*/

class datalist
{
	public $db;
	public $pagestring;
	public $number;
	public $totalcount;
	
	function __construct()
	{
		global $db;
		$this->db=$db;
	}
	
	function getlist($table,$where,$orderby,$page=1,$pagesize=15,$totalnumber=0,$align='right',$atttable='',$mainindex='id',$attindex='catid',$morefields='*')
	{
		$array=array();
		$where=empty($where)?'':'WHERE '.$where;
		$orderby=empty($orderby)?'':'ORDER BY '.$orderby;
		if(!$totalnumber)
		{
			if(!empty($atttable))
			{
				/*
					读取多表级联 , 一般不建议使用!
				*/
				$number=get_cache_counts("SELECT COUNT(*) AS count FROM `$table` a LEFT JOIN `$atttable` b ON a.`$mainindex`=b.`$attindex` $where"); 
			}
			else
			{
				$number=get_cache_counts("SELECT COUNT(*) AS count FROM `$table` $where"); 
			}
		}
		else
		{
			$number=intval($totalnumber);
		}
		$this->totalcount=$number;
		$pagenum=ceil($number/$pagesize);
		$page=max(1,min(max(intval($page),1),$pagenum));
		$start=($page-1)*$pagesize;
		$limit="LIMIT $start,$pagesize";
		$this->pagestring=getpages($number,$page,$pagesize,0,$align);
		if(!empty($atttable))
		{
			/*
				读取多表级联 , 一般不建议使用!
			*/
			$array=$this->db->fetch_all("SELECT a.*,b.`$attindex` FROM `$table` a LEFT JOIN `$atttable` b ON a.`$mainindex`=b.`$attindex` $where $orderby $limit");
		}
		else
		{
			$array=$this->db->fetch_all("SELECT * FROM `$table` $where $orderby $limit");
		}
		return $array;
	}
}
?>
