I am displaying data results from database with php, ajax and jquery.
I have used codeigniter 4 pager library for pagination. But when I click the next page of pagination the serial number reset and start again from 1-10, all I want is when i click the next page to continue counting a serial number from 11-20, 21-30 etc…
Help please. Thank you.
//html table
<table>
<thead>
<tr>
<th>No.</th>
<th>MyColumn</th>
</tr>
</thead>
<tbody class="result"></tbody>
</table>
//jquery
show();
function show(){
let ajax = $.ajax({
url : '/Controller/getAll',
method : 'post',
dataType: 'json',
cache : false
});
ajax.done(function(data){
let serialNumber = 1;
$.each(data.result, function(k, v){
html += `<tr>
<td>${serialNumber}</td>
<td>${v.myColums}</td>
</tr>`;
serialNumber++;
});
});
$('.result').html(html);
}
// php controller
public function getAll()
{
$result = $this->model->getData()->asObject()->paginate('10');
$pager = $this->model->pager;
foreach($result as $row){
$output[] = [
'id' => $row->id,
'myColumn' => $row->myColumn,
];
}
$jsonArray = [
'result' => $output,
'pager' => $pager->links(),
];
return $this->response->setJSON($jsonArray);
}
3
Answers
You are initializing
serialNumber
when you receive a ajax response. Do it on page load.Declare a variable outside of the function
Another option is to return the page number from PHP and calculate the start point of the serial
You can try this: