Я пытаюсь добавить данные в подключаемый модуль, используя codeigniter, но объем данных увеличивается.поэтому время загрузки моей страницы очень велико.поэтому я хочу получать постраничные данные из базы данных.
мой php view
<table class="table table-bordered table-striped" id="userTbl">
<thead>
<th>Sr no</th>
<th>UserId</th>
<th>Name</th>
<th>Mobile No.</th>
</thead>
</table>
<script>
fetch_data('no');
function fetch_data(is_date_search, start_date = '', end_date = '')
{
dataTable = $('#userTbl').DataTable({
language: {search: ""},
"processing": true,
"serverSide": true,
"order": [[0, "desc"]],
"ajax": {
url: "<?php echo site_url(); ?>/Admin/users_table",
type: "POST",
data: {
is_date_search: is_date_search, start_date: start_date, end_date: end_date
}
},
responsive: true,
"lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]], // page length options
"dom": 'Bfrtip',
"buttons": ['pageLength', $.extend(true, {}, {
extend: 'excelHtml5',
title: 'users'
}), ],
});
$('#userTbl_filter input[type="search"]').attr('placeholder', 'Search');
$("#userTbl_filter").addClass('col-sm-2 temp');
$("#userTbl_length").addClass('col-sm-2');
var div = document.createElement('div');
// set class name
div.className = 'col-sm-8 sf';
// set html contents
div.innerHTML = '<form name="form1" method="POST" action=""><div class="col-sm-4 no-padding padd"><input class="form-control" id="from_date" name="from_date" placeholder="Form Date" type="text" style="width:48%;display:inline;margin-right: 8px;" value="<?php echo set_value('from_date'); ?>" /><input class="form-control" id="to_date" name="to_date" placeholder="To Date" type="text" style="width:48%;display:inline;" value="<?php echo set_value('to_date'); ?>" /><span class="text-red"><?php echo validation_errors(); ?></span></div><div class="col-sm-2 no-padding padd"><button type="button" id="mybutton" class="form-control fa fa-search btn btn-default" style="width:auto;"> Search</button></div></form>';
// get .div2 element
var ele = document.querySelector('.temp');
ele.parentNode.insertBefore(div, ele);
$("#from_date").datepicker({format: 'dd-mm-yyyy'});
$("#to_date").datepicker({format: 'dd-mm-yyyy'});
$('#mybutton').click(function () {
var start_date = $('#from_date').val();
var end_date = $('#to_date').val();
if (start_date != '' || end_date != '')
{
$('#userTbl').DataTable().destroy();
fetch_data('yes', start_date, end_date);
} else
{
window.location.reload();
}
});
if (start_date != '') {
$('#from_date').val(start_date);
}
if (end_date != '') {
$('#to_date').val(end_date);
}
}
</script>
и мой контроллер
$requestData = $_REQUEST;
$columns = array(
0 => 'user_id',1 => 'user_id',2 => 'user_name',3 => 'user_mobile',
);
$sql = "SELECT * FROM `tbl_users`";
$query = $this->db->query($sql);
$totalData = $query->num_rows();
$totalFiltered = $totalData;
if (strtolower($requestData["search"]["value"]) == 'active') {
$search_term = 1;
} else if (strtolower($requestData["search"]["value"]) == 'deactive') {
$search_term = 0;
} else {
$search_term = $requestData["search"]["value"];
}
$sql = "SELECT * FROM `tbl_users` WHERE 1=1 ";
if (!empty($requestData['search']['value'])) {
$sql .= 'AND (user_id LIKE "%' . $requestData["search"]["value"] . '%"
OR user_name LIKE "%' . $requestData["search"]["value"] . '%"';}
$query = $this->db->query($sql);
$totalFiltered = $query->num_rows();
if ($requestData['start'] == 0 && $requestData['length'] == -1) {
$sql .= " ORDER BY user_id DESC ";
} else {
if (isset($requestData["order"])) {
$sql .= " ORDER BY " . $columns[$requestData['order'][0]['column']] . " " . $requestData['order'][0]['dir'] . " LIMIT " . $requestData['start'] . " ," . $requestData['length'] . " ";
} else {
$sql .= " ORDER BY user_id DESC LIMIT " . $requestData['start'] . " ," . $requestData['length'] . " ";
}
}
$query = $this->db->query($sql)->result_array();
$data = array();
$i = 1 + $requestData['start'];
foreach ($query as $row):
$nestedData = array();
$nestedData[] = $i;
$nestedData[] = $row['user_id'];
$nestedData[] = $row['user_name'];
$nestedData[] = $row["user_mobile"];
$data[] = $nestedData;
$i++;
endforeach;
$json_data = array(
"draw" => intval($requestData['draw']),
"recordsTotal" => intval($totalData),
"recordsFiltered" => intval($totalFiltered),
"data" => $data
);
echo json_encode($json_data);
Извините за длинный код.я новичок в codeigniter Framework я пытаюсь отправить стартовый индекс и предел, но это не работает.