我需要对多个结果集进行分页,到目前为止我的查询是这样的:
// setting offset for pagination
$offset=is_numeric($this->uri->segment($page_segment))?
$this->uri->segment($page_segment):
0;
$q = "
SELECT
Notices.Id,
NoticeLoc.Notices_Id,
Loc.Id as Loc_Id,
CAST(Notices.Text AS TEXT) as Text,
CAST(Notices.Title AS TEXT) as Title,
Notices.CDate as RDate
FROM NoticeLoc
JOIN Notices ON NoticeLoc.Notices_Id=Notices.Id JOIN Loc ON NLoc.Loc_Id=Loc.Id WHERE Loc_Id IN (1)
UNION
SELECT
Notices.Id,
'',
'',
CAST(Notices.Text AS TEXT) as Text,
CAST(Notices.Title AS TEXT) as Title,
Notices.CDate as RDate
FROM NoticeC
JOIN Notices ON NoticeC.Notices_Id=Notices.Id WHERE C_Id=110 AND CDate BETWEEN '10/01/2011' AND '07/14/2025' ORDER BY RDate desc ";
$query = $this->db->query($q);
//result array
$Notices = $query->result_array();
//count total number of records in query
$recCnt = count($Notices);
//pagination
//slice array, so that you only get the amount defined in the limit
$config['total_rows'] = array_slice($query, $page_segment , $limit);
$config['per_page'] = $limit;
$this->pagination->initialize($config);
//pass onto the controller
return array("paging"=>$this->pagination->create_links(),"notices"=>$notices);
我似乎无法让最后一部分工作,页面成功加载所有通知,但分页失败。页面最终显示所有通知,而不是在切片后显示特定的通知。
知道为什么吗?
欢呼声。
修正如下:
$config['total_rows'] = $recCnt;
然后在最后写道:
$notices = array_slice($notices, $offset , $limit);
换句话说,原始查询获取完整的结果集并将其放入数组中,然后根据页面的偏移量对其进行切片。