I am working on a blog application with Codeigniter 3.1.8.
Currently, its admin area is well separated from the frontend.
In the frontend, I have managed to replace "classic" Codeigniter views with JSONS. (The JSONS are displayed and "handled" with AngularJS):
Code:
private function _initPagination($path, $totalRows, $query_string_segment = 'page') {
//load and configure pagination
$this->load->library('pagination');
$config['base_url'] = base_url($path);
$config['query_string_segment'] = $query_string_segment;
$config['enable_query_strings'] =TRUE;
$config['reuse_query_string'] =TRUE;
$config['total_rows'] = $totalRows;
$config['per_page'] = 12;
if (!isset($_GET[$config['query_string_segment']]) || $_GET[$config['query_string_segment']] < 1) {
$_GET[$config['query_string_segment']] = 1;
}
$this->pagination->initialize($config);
$limit = $config['per_page'];
$offset = ($this->input->get($config['query_string_segment']) - 1) * $limit;
return ['limit' => $limit, 'offset' => $offset];
}
public function index() {
//call initialization method
$config = $this->_initPagination("/", $this->Posts_model->get_num_rows());
$data = $this->Static_model->get_static_data();
$data['pages'] = $this->Pages_model->get_pages();
$data['categories'] = $this->Categories_model->get_categories();
//use limit and offset returned by _initPaginator method
$data['posts'] = $this->Posts_model->get_posts($config['limit'], $config['offset']);
// All posts
$this->output->set_content_type('application/json')->set_output(json_encode($data,JSON_PRETTY_PRINT));
}
What I have not been able to do (despite tormenting my brain), is display the pagination of the posts as JSON too.
My pagination view:
Code:
<div class="pagination-container text-center">
<?php echo $this->pagination->create_links(); ?>
</div>
I have tried
Code:
echo json_encode(['pagination' => $this->pagination->create_links()]);
It does not work. What shall I do?