Search code examples
phppaginationcodeigniter-2

CodeIgniter Pagination : Show Records from SQL Server


In codeigniter, I use sqlsrv and pagination library to show records from database, but after i click on page 2 on pagination link, the Records will show at the end of current page instead of showing in new page, any help would be greatly appreciated, here is the Controller:

public function example1() {
    $config = array();
    $config["base_url"] = base_url() . "index.php/posts/example1";
    $config["total_rows"] = $this->PostModels->record_count();
    $config["per_page"] = 10;
    $config["uri_segment"] = 3;
    $config["num_links"] = 5;

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

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

    $this->load->view("example1", $data);

Here is the Model:

public function record_count() {
return $this->db->count_all("BlawBlaw");
}

public function fetchdata($limit, $start) {
    $this->db->limit($limit, $start);
    $query = $this->db->get("BlawBlaw");

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

& here is the View:

        <table>
        <tr>
            <th><strong>Post Id</strong></th>
            <th><strong>Post Title</strong></th>
            <th><strong>Post Title</strong></th>
            <th><strong>Post Title</strong></th>
        </tr>
        <?php foreach($results as $data){?>
            <tr>
                <td><?php echo $data->a;?></td>
                <td><?php echo $data->b;?></td>
                <td><?php echo $data->c;?></td>
                <td><?php echo $data->d;?></td>
            </tr>
        <?php }?>
        <p><?php echo $links; ?></p>
    </table>

Solution

  • you can used this this model and view changes make in your code

    Here is the Model:

    function fetch_data($limit, $id) 
       {
           $this->db->select('*')->from('category ')->limit($limit, $id);
            //$this->db->limit($limit);
            //$this->db->where('Cid', $id);
            $query = $this->db->get();
            if ($query->num_rows() > 0) 
            {
            foreach ($query->result() as $row) 
            {
            $data[] = $row;
            }
    
            return $data;
            }
            return false;
    
    
    & here is the View:
    
                <table>
                <tr>
                    <th><strong>Post Id</strong></th>
                    <th><strong>Post Title</strong></th>
                    <th><strong>Post Title</strong></th>
                    <th><strong>Post Title</strong></th>
                </tr>
                <?php foreach($results as $data){?>
                    <tr>
                        <td><?php echo $data->a;?></td>
                        <td><?php echo $data->b;?></td>
                        <td><?php echo $data->c;?></td>
                        <td><?php echo $data->d;?></td>
                    </tr>
                <?php }?>
                <p><?php foreach ($links as $link) {
    echo $link; ?></p>
            </table>