3
votes

Can any one tell me wahat went wrong here. I am writing a code for pagination using codeigniter pagination library and every thing works perfect except the page links donot take any where. they just add additional uri paths like http://localhost:8081/web/index.php/Public/Main/index/1/1/1 when clicked. Help please.

application/core/ My Controller

public function PAGINATE($total, $per_page) {
        $this->load->library('pagination');
        $config["base_url"] =   $_SERVER['PHP_SELF'];
        $config["total_rows"] = $total;
        $config["per_page"] = $per_page;
        $config["uri_segment"] = 3;
        $config["num_links"] = 3;
        $config['prev_link'] = 'Previous';
        $config['next_link'] = 'Next';
        $config['last_link'] = 'Last';
        die(print_r($config));
        $this->pagination->initialize($config);
        $this->data["links"] = $this->pagination->create_links();
    }

Controllers/Public/Main/Index

public function index()
    {
        parent::PAGINATE(sizeof($this->data['homes'] = $this->AppModel->get_all(HOME_TABLE, 200, 0)), 1);
        parent::RENDER('Public/Site/Home');
    }

Model

 public function get_all($table, $limit, $offset){
        $this->db->select('*');
        $this->db->limit($limit, $offset);
        $this->db->from($table);
        $query = $this->db->get();
        return $query->result_array(); // fetch data

    }

View

<div class="page-nav">
                <ul>
                    <li><?php echo $links; ?></li>
                </ul>
            </div>
1
how many records in the table?ImBhavin95
Try with hard coded value for base url similar to how is pointed in docs ( http://example.com/index.php/test/page/ ). Don't forget ending slash.Tpojka
ImBS 10 records are in the table.Melaku Minas Kasaye
@Tpojka i treied that and nothing is changed.Melaku Minas Kasaye
Problem is that relative links are created and those are appending existing URL. Can you post what is there in $links variable when page is rendered?Tpojka

1 Answers

1
votes
$config["base_url"] =   $_SERVER['PHP_SELF'];

This line should be replace with following

$config["base_url"] =   site_url('public/main/index');
//$config["base_url"] =   'http://localhost:8081/web/index.php/Public/Main/index';//you can use this too

Make sure you loaded URL helper to use site_url function.

If you want to see page number instead of record number in link you can use

$config['use_page_numbers'] = true;

To get total records in database you should use.

return $this->db->count_all_results($table);

For more details please refer doc.