CodeIgniter Forums
How can I display a Codeigniter 3 pagination as JSON? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: How can I display a Codeigniter 3 pagination as JSON? (/thread-74234.html)



How can I display a Codeigniter 3 pagination as JSON? - Ajax30 - 08-31-2019

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?


RE: How can I display a Codeigniter 3 pagination as JSON? - milengardev1994 - 08-31-2019

I don't think this will work out.

Code:
$this->pagination->create_links()

This code as far as I know only generates HTML template. 

I had a similar issue and I have created custom library that can output it as JSON object.

To create the pagination you need the total pages count and amount of results per page.


Code:
$res['max_pages'] = ceil($totalImagesCount / MAX_RESULTS_PER_PAGE);

This is some example code I have used.

I hope I help you a bit!