Search code examples
codeigniterpaginationstacked

Stacked in Pagination in Codeigniter


i'm new with CI and i tried a lot posibilities to make this run, but still it ain't working. Could you please tell me what i'm doing wrong?

So, Controller:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class News extends CI_Controller {

public function __construct()
{
    parent::__construct();
    $this->load->model('model_add','',TRUE);
}

public function _remap($a){
    if (isset($a) && !empty($a)):
        switch ($a) {
            case 'index':
                $this->index();
                break;

            default:
                $this->one_news($a);
                break;
        }
    endif;
}

public function index()
{
    $this->load->library('pagination');
    $home=$this->uri->segment(2);

    $limit=2;
    $offset=0;

    $query=$this->model_add->count_news($limit,$offset);

    $config['base_url'] = base_url().'/news/';
    $config['total_rows'] = $this->db->count_all('tdt_news');
    $config['per_page'] = 2;
    $config['uri_segment'] = 2;

    $this->pagination->initialize($config);
    $data = array('query' => $query,'page'=>$home);

    $data['news']           = $query;

    $this->load->view('main/header');
    $this->load->view('main/news', $data);
    $this->load->view('main/footer');
}
}

And the Model:

    function count_news()
{
    $query=$this->db->query("SELECT * FROM `tdt_news` ORDER BY `id` DESC LIMIT $limit, $offset;");
    return $query->result();
}

I'll be very thankful for your help, thank you!


Solution

  • You have a series of errors

    • your count function in your model is missing parameters
    • you should reformat the SQL string
    • alternate and standard syntax in your view
    • renamed model class to empty string

    heres a fixed version,

    Controller:

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    class News extends CI_Controller {
    
        public function __construct()
        {
            parent::__construct();
            $this->load->model('model_add');
        }
    
        public function _remap($a){
            if (isset($a) && !empty($a)) {
                switch ($a) {
                    case 'index':
                    $this->index();
                    break;
    
                    default:
                    $this->one_news($a);
                    break;
                }
            }
        }
    
        public function index()
        {
            $this->load->library('pagination');
            $home = $this->uri->segment(2);
    
            $limit = 2;
            $offset = 0;
    
            $query=$this->model_add->count_news($limit,$offset);
    
            $config['base_url'] = base_url().'/news/';
            $config['total_rows'] = $this->db->count_all('tdt_news');
            $config['per_page'] = 2;
            $config['uri_segment'] = 2;
    
            $this->pagination->initialize($config);
    
            $data = array(  'query' => $query,
                            'page'=>$home,
                            'news' => $query);
    
            $this->load->view('main/header');
            $this->load->view('main/news', $data);
            $this->load->view('main/footer');
        }
    }
    

    Model:

    // with default parameters just in case
    function count_news($limit = 50, $offset = 0)
    {
        // and there is a nicer way to write your query
        // it works with multiple dbs thanks to active record
        $this->db->order_by('id','desc');
        $query = $this->db->get('tdt_news', $limit, $offset);
        return $query->result();
    }