Pagination with database

In your controller make method like below:
note: Here i am creating a pagination with a date between query, so keep clam and read.


public function fn_name(){
        $fdate=$this->uri->segment(3); //getting date from url
        $tdate=$this->uri->segment(4);
        if($fdate!=”” && $tdate!=””){
            $config[“base_url”]=base_url().’controller/method/’.$fdate.’/’.$tdate;
            $config[“total_rows”]=$this->Model->total_row_fn_name($fdate,$tdate);
            $config[“per_page”] = 6;
            $config[“uri_segment”] = 5;
            $choice = $config[“total_rows”] / $config[“per_page”];
            $config[“num_links”] = round($choice);
            $this->pagination->initialize($config);
            $page = ($this->uri->segment(5)) ? $this->uri->segment(5) : 0;
            $result[“data”] = $this->Model->method_Bydate($config[“per_page”],$page,$fdate,$tdate);
            $result[‘pagination_link’]=$this->pagination->create_links();
        }
        $this->load->view(‘pages/slip_report’,$result);

    }

In your model make a method:


public function total_row_fn_name($fdate,$tdate){
        $sql=$this->db->select(‘put your database field to select here’)
            ->join(‘tbl name’,’table.field=tbl.field’,’left’)
            ->join(‘tbl’,’tbl.field=tbl.field’,’left’)
            ->where(‘DATE(chalan_date) >=’,date(‘Y-m-d’,strtotime($fdate)))
            ->where(‘DATE(chalan_date) <=’,date(‘Y-m-d’,strtotime($tdate)))
            ->order_by(‘tbl.field’)
            ->get(‘tbl’);
        return $sql->num_rows();
    }

public function method_Bydate($limit,$start,$fdate,$tdate){
        $sql=$this->db->select(‘ your fields goes here’)
            ->join(‘tbl’,’tbl.field=tbl.field’,’left’)
            ->join(‘tbl’,’tbl.field=tbl.field’,’left’)
            ->where(‘DATE(date_field) >=’,date(‘Y-m-d’,strtotime($fdate)))
            ->where(‘DATE(date_field) <=’,date(‘Y-m-d’,strtotime($tdate)))
            ->order_by(‘field’)
            ->limit($limit,$start)
            ->get(‘dailycirculation’);
        //echo $this->db->last_query();
        return $sql->result_array();
    }

In your view invoke this pagination like below:


<?php echo $pagination_link;?>

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>