【问题标题】:cant get pagination by when i use other method than count_all codeigniter当我使用 count_all codeigniter 以外的其他方法时无法分页
【发布时间】:2012-12-20 12:00:14
【问题描述】:

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

控制器代码

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

class 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);
}

【问题讨论】:

  • 任何帮助都会被应用。我想用 num_rows fun 代替 count_all
  • 你不能使用 num_rows 因为那总是限制的 100%,因此,没有分页
  • 感谢您的回复,是的哦,但这正是我面临的问题。
  • 但是当我们在 config['total_rows']='2' 中传递任何静态整数值时,它会生成分页,并且当我回显受影响行的结果时,它也会返回一个 int 那么问题出在哪里在分页中
  • 现在还不确定

标签: php codeigniter count pagination


【解决方案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-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-04
    • 1970-01-01
    • 2018-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多