2

我使用连接查询来获取数据,当从连接的表中删除一些数据时,它会产生分页问题

控制器代码

function index($msg='',$offset = 0)
{
        $data = array('title'=>'Towns','message'=>'', 'link_add'=>site_url('manage/town/add'), 'edit_link'=>site_url('manage/town/edit'), 'tbl'=>'towns' );
        $uri_segment = 4;
        $offset = $this->uri->segment($uri_segment);
        // load data
        $value=('towns.Id,towns.Name as TownName,city.Name as CityName,city.Status,towns.Status,towns.TaxAmount');
        $data['list_records'] = $this->admin_model->get_joinlist($data['tbl'],$value,'city','city.Id = towns.cityId','left outer','towns.Id','asc',array('towns.Status !='=>'Delete','city.Status'=>'Enable'),$this->limit, $offset)->result();
        if($msg=='m')$data['message'] = 'New Town has been added successfully!';
        // generate pagination
        $this->load->library('pagination');
        $config['base_url'] = site_url('manage/town/index/');
        $this->total = $this->admin_model->**count_all**($data['tbl'],array('Status !='=>'Delete'));
        $config['total_rows'] = $this->total;
        $config['per_page'] = $this->limit;
        $config['uri_segment'] = $uri_segment;
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
        $data['j'] = 0 + $offset;
        $data['total_rows']= $this->total;
        // load view
        $this->load->view('manage/includes/header', $data);
        $this->load->view('manage/town', $data);
        $this->load->view('manage/includes/footer');
}

         <?php

类 Admin_model 扩展 CI_Model {

//listing with join
public function get_joinlist($table,$value,$table2,$condi,$join_type,$order_by,$order,$where,$limit, $offset)
{
    $this->db->select($value);
    $this->db->join($table2,$condi,$join_type);
    $this->db->order_by($order_by,$order);
    $this->db->where($where);
    return $query= $this->db->get($table, $limit, $offset);
}

//For pagination
function count_all($table,$where)
{   
   return $this->db->where($where)
   ->count_all_results($table);
}
function num_rows($table)
{   
   return $this->db->affected_rows($table);
}
4

1 回答 1

1

做这样的事情:

   public function get_joinlist($table,$value,$table2,$condi,$join_type,$order_by,$order,$where,$limit, $offset)
{
    $this->db->start_cache();
    $this->db->select($value);
    $this->db->join($table2,$condi,$join_type);
    $this->db->order_by($order_by,$order);
    $this->db->where($where);
    $this->db->stop_cache();

    $query['num_rows']=$this->db->get($table)->num_rows();
    $query['results']=$this->db->get($table, $limit, $offset);
    $this->db->flush_cache();
    return $query;
}

这样,您的模型将返回一个数组,其中包含连接的总行数以及受偏移量和限制行数限制的结果

于 2013-02-15T00:43:56.777 回答