【发布时间】:2013-12-06 02:13:58
【问题描述】:
我正在尝试设置 jqgrid,但在构建生成 json 数据以填充网格的控制器时遇到了困难。我正在使用 codeigniter 2.0+,我不确定如何在 codeigniter 中构建 php 查询。
我在 jqgrid 的“加载数据 -> JSON 数据”下关注this guid。我还咨询了codeigniter docs关于数据选择的问题。问题是我不确定如何编写第二个查询以根据 jqgrid 参数进行排序和限制。这是我的控制器。
public function applicantdata(){
$page = $this->input->get('page');// get the requested page
$limit = $this->input->get('rows');// get how many rows we want to have into the grid
$sidx = $this->input->get('sidx');// get index row - i.e. user click to sort
$sord = $this->input->get('sord');// get the direction
if(!$sidx){ $sidx =1; }
$this->db->select('*');
$this->db->from('applicant');
$this->db->join('transaction', 'transaction.applicant_id = applicant.id');
$query = $this->db->get();
$count = $query->num_rows();
$limit = 10;
if( $count > 0 ) {
$total_pages = ceil($count/$limit);
} else {
$total_pages = 0;
}
if ($page > $total_pages){ $page=$total_pages; }
$start = $limit*$page - $limit; // do not put $limit*($page - 1)
//NOT SURE HOW TO DO THIS IN CODEIGNITER ENVIRONMENT
//$SQL = "SELECT a.id, a.invdate, b.name, a.amount,a.tax,a.total,a.note FROM invheader a, clients b WHERE a.client_id=b.client_id ORDER BY $sidx $sord LIMIT $start , $limit";
//$result = mysql_query( $SQL ) or die("Couldn t execute query.".mysql_error());
$result = $query->result_array();
$responce->page = $page;
$responce->total = $total_pages;
$responce->records = $count;
$i=0;
foreach ($result as $myrow){
$responce->rows[$i]['id']=$myrow['id'];
$responce->rows[$i]['cell']=array($myrow['id'],$myrow['firstname'],$myrow['lastname'],$myrow['amount'],$myrow['status']);
$i++;
}
echo json_encode($responce);
}
使用上面的代码,网格正在填充并且它的工作范围更大。唯一的问题是分页内容无法正常工作,因为当我移至第 2、3 页等时,第一页上的数据会显示。
【问题讨论】:
标签: php mysql sql codeigniter jqgrid