neknek mouh
neknek mouh

Reputation: 1802

CodeIgniter Pagination not showing the correct rows per page

hello my pagination is not working.

This is my controller class. customer_controller.php

     public function __construct()
    {
        parent::__construct();
        $this->load->helper('url');
        $this->load->model('customer_model');
        $this->load->library('pagination');


    }
    public function index()
    {


        $data['title']= 'Customer';
        $data['records'] = $this->customer_model->getAll();
        $data['groups'] = $this->customer_model->getAllCustomerGroups();
        $data['groupcodes'] = $this->customer_model->getAllCustomerGroups();

        $config['base_url'] = 'customers';
        $config['total_rows'] = $this->customer_model->record_count();
        $config['per_page'] = 1;

        $config["uri_segment"] = 3;

        $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        $data["customers"] = $this->customer_model->fetch_customers($config["per_page"], $page);
        $data["links"] = $this->pagination->create_links();


        //$this->pagination->initialize($config);
        //echo $this->pagination->create_links();

        $this->load->view('include/header',$data);
        $this->load->view('include/navbar',$data);
        $this->load->view('customer_view', $data);
        $this->load->view('include/sidebar',$data);
        $this->load->view('include/footer',$data);

    }

Customer_model.php

   public function fetch_customers($limit, $start) {
            $this->db->limit($limit, $start);
            $query = $this->db->query('SELECT customercode, customername, customergroup, customertype, customeraddress, website FROM customercard');

            if ($query->num_rows() > 0) {
                foreach ($query->result() as $row) {
                    $data[] = $row;
                }
                return $data;
            }
            return false;
        }

customer_view.php

       <?php

            foreach ($customers as $key => $value)
            {
                    echo '<p>'. $value->customercode . '</p>';
                    echo '<p>'. $value->customername . '</p>';
            }

            ?>

My problem is, it's just displaying all the records and not showing only 1 record at a time. and also my pagination links does not display. What am I doing wrong in here? Help is pretty much appreciated. Thanks.

Upvotes: 1

Views: 1896

Answers (3)

Nikhil
Nikhil

Reputation: 99

Try this one

Model:

public function fetch_customers($limit, $offset) {
    $this->db->select('customercode, customername, customergroup, customertype, customeraddress, website');
    $query=$this->db->get('t_candidate', $limit, $offset);

    if ($query->num_rows() > 0) {
        return $query->result_array();
    }else{
        return false;
    }
}

View: In view paste following link wherever you want to show your links

 <?php 
 foreach ($customers as $key => $value)
        {
                echo '<p>'. $value['customercode'] . '</p>';
                echo '<p>'. $value['customername'] . '</p>';
        }
  ?>

 <?php echo $this->pagination->create_links(); ?>

Controller: uncomment following line

  $this->pagination->initialize($config);

Also i would rather suggest that you try and use codeigniter active records as it protects your db from sql injections

Upvotes: 0

Harpreet Bhatia
Harpreet Bhatia

Reputation: 186

your pagination links would display with

 <?php

        foreach ($customers as $key => $value)
        {
                echo '<p>'. $value->customercode . '</p>';
                echo '<p>'. $value->customername . '</p>';
        }
        echo $links;

        ?>

also uncomment

//$this->pagination->initialize($config);

also change your query to only

//    $this->db->limit($limit, $start);// <-- remove this

        $query = $this->db->query("SELECT customercode, customername, customergroup, customertype, customeraddress, website FROM customercard limit $start, $limit");

hope this helps

Upvotes: 1

fr05t1k
fr05t1k

Reputation: 401

Try to replace

$this->db->limit($limit, $start);
$query = $this->db->query('SELECT customercode, customername, customergroup, customertype, customeraddress, website
 FROM customercard');

to

$this->db->select('customercode, customername, customergroup, customertype, customeraddress, website')->from('customercard')->limit($limit, $start)->get();

Upvotes: 0

Related Questions