offset is not working

offset is not working

RookieProgrammerRookieProgrammer Posts: 4Questions: 3Answers: 0

Dear Fellow Programmers,

I am trying to display records in Pagination using data table in CodeIgniter where my code is counting correctly total number of rows, model is also fetching all records from db even ajax response is showing correctly and limit of 10 records per page works fine as well however I am facing an issue of offset. My records are not changing with change of page.

Kindly suggest me suitable solution for the same

Best Regards,
Jay

Model

class UserDashboard extends CI_Model
{
    public function getRows(){
        $query2=$this->db->query('SELECT * FROM user_list');
        return $query2->num_rows();
    }
    public function userData(){
        $query = $this->db->get('user_list');
        $this->db->limit(10,10);
         return $query->result();
     }
}

Controller

public function ajax() {
    $user_data= $this->UserDashboard->userData();
    $totalRows=$this->UserDashboard->getRows();
    $data = array();
    foreach($user_data as $row){
        $data[] =array(
            $row->user_id,
            $row->user_name,
            $row->fname,
            $row->lname,
            $row->eml,
            $row->gnd,
            $row->ctry,
            $row->st,
            $row->cty,
            "<img height='70' width='70' src='../uploads/".$row->img."'>", 
            "<a href='edit.php?user_id=". $row->user_id ."' id='edit' class='btn btn-primary'>Edit </a>".
            "<a href='remove.php?user_id=". $row->user_id ."' id='delete' class='btn btn-danger'>Delete</a>"
        );
          
    }
    $output=array(
        'draw'=>$_POST['draw'],
        'recordsTotal' => $totalRows,
        'recordsFiltered' => $totalRows,
        'data'=>$data,
        );
    echo json_encode($output);  

}

View

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<!DOCTYPE html>
<html lang="">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Login</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src="https://cdn.datatables.net/1.10.24/js/jquery.dataTables.min.js"></script>
    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.24/css/jquery.dataTables.min.css"/>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous">
</head>
<body>
<!-- style="background:url(<?php echo base_url();?>/assets/css/Wireframe.png);" -->
<div class="container-fluid text-center">
    <h1>User Dashboard</h1>
    Welcome, <?php get_cookie('username')?>
    <a href="<?php echo site_url('home/logout');?>" class="btn btn-primary">Logout</a>
</div>
<div class="container-fluid ">
    <table id="tableData" class="table table-hover text-black">
        <thead>
            <tr>
                <th>User Id</th>
                <th>User Name</th>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Email</th>
                <th>Gender</th>
                <th>Country</th>
                <th>State</th>
                <th>City</th>
                <th>Profile Pic</th>
                <th>Action</th>
            </tr>
        </thead>
        <tbody>
        </tbody>
    </table>
    
</div>
<!-- <script src="<?php echo base_url();?>/assets/js/bootstrap.min.js"></script>  -->

<script>
$(document).ready(function() {
    $('#tableData').DataTable({
        //"processing": true,
        'serverSide': true,
        //"searching":true,
        //"order":[],
        'ajax':{
            'url':"<?php base_url();?>ajax",
            'type':"POST",
            // "columns": [
            //     {"name": "user_id"},
            //     {"name": "user_name"},
            //     {"name": "fname"},
            //     {"name": "lname"},
            //     {"name": "eml"},
            //     {"name": "gnd"},
            //     {"name": "ctry"},
            //     {"name": "st"},
            //     {"name": "cty"},
            //     {"name": "img"},
            //     {"name":"Action"}
            //  ],
        }
    });
    
});
</script>


</body>
</html>

Sign In or Register to comment.