php access odbc分页的实现

php+access用odbc实现分页主要用的是

int odbc_fetch_into ( resource $result_id , array &$result_array [, int $rownumber ] )

这个方法,但是就是这个方法让我纠结了很久,注意这个方法的参数,很多资料的中rownumber参数都是在第二个,所以分页结果返回的都是0,所以,还是看最新的文档比较保险。下面是代码:

public function getListByPage($table, $page){

	$constr = 'DRIVER={Microsoft Access Driver (*.mdb)};DBQ='.realpath($this->db_path).';';
	$this->link = odbc_connect($constr, $this->db_user, $this->db_password, SQL_CUR_USE_ODBC);

	if(!$this->link){
		echo "connection failed!";
		return;
	}

	$pagesize = 2;

	$hasNext = false;
	$hasPre = false;
	$totalCount = 0;
	$totalPage = 0;

	$sql = "select count(*) from " . $table;
	$result = $this->odbc_exec($this->link, $sql);;

	//取得记录总数
	$recordcount = odbc_result($result, 1);
	$totalCount = $recordcount;

	odbc_free_result($result);	//释放资源

	//获取总页数
	$pagecount = bcdiv($recordcount + $pagesize - 1, $pagesize, 0);
	$totalPage = $pagecount;

	if(!isset($page)){
		$page = 1; //如果没有指定显示页码,缺省为显示第一页
	}
	if($page < 1){
		$page = 1;	 //如果页码比1小,则显示第一页
	}
	if($page > $pagecount){
		$page = $pagecount; //如果页码比总页数大,则显示最后一页
	}

	// 判断上一页/下一页
	if($page > 1){
		$hasPre = true;
	}
	if($page < $totalPage){
		$hasNext = true;
	}

	$sql = "select * from " . $table;
	$result = $this->odbc_exec($this->link, $sql);

	$rownum = (($page - 1) * $pagesize) + 1;

	$recordlist = array();
   	for($i = 0; $i < $pagesize; $i++){
	    if($rownum <= $recordcount){
    		$row = &$row;
	        odbc_fetch_into($result, $row, $rownum);
	        array_push($recordlist, $row);
	        $rownum = $rownum + 1;
      	}
   	}

   	$data = array();
   	$data['data'] = $recordlist;
   	$data['hasPre'] = $hasPre;
   	$data['hasNext'] = $hasNext;
   	$data['totalCount'] = $totalCount;
   	$data['totalPage'] = $totalPage;

   	return $data;
}

 

发表评论