由于查询,Jqgrid分页无法正常工作

时间:2013-12-06 02:13:59

标签: php mysql sql codeigniter jqgrid

我正在尝试设置一个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页等时。

1 个答案:

答案 0 :(得分:0)

她是最后的工作。对于任何可能需要它的人。

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('firstname');
     $this->db->from('applicant');
     $this->db->join('transaction', 'transaction.applicant_id = applicant.id');
     $query = $this->db->get();    

     $count = $query->num_rows();

        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)


        $this->db->select('*');
        $this->db->from('applicant');
        $this->db->join('transaction', 'transaction.applicant_id = applicant.id');
        $this->db->order_by("applicant.id", $sord);
        $this->db->limit($limit, $start);
        $query2 = $this->db->get();               
        $result = $query2->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);
}