0
votes

I am trying to use pagination with codeigniter 3 and bootstrap twitter. When i click the link pagination it's always give me 404 not found.

I think it's my fault for not fully understanding codeigniter URI that's why i need help in my code

Here is my model :

class Mcrud extends CI_Model {

    public function record_count() {
        return $this->db->count_all("crud");
    }

    function view() {
    $ambil = $this->db->from('crud')->order_by('idcrud', 'DESC')->limit(5, 3)->get();
        if ($ambil->num_rows() > 0) {
            foreach ($ambil->result() as $data) {
                $hasil[] = $data;
            }
            $ambil->free_result();
            return $hasil;          
        }
    }

here is my controller :

class Chome extends CI_Controller {
function __construct() {
    parent::__construct();
    $this->load->model('Mcrud');

}

function index() {
    if($this->session->userdata('logged_in'))
    {
        $session_data = $this->session->userdata('logged_in');
        $data['nama'] = $session_data['fullname'];
        $data['username'] = $session_data['username'];
        $data['id'] = $session_data['idlogin'];

        $this->load->view('Header', $data);
        $this->suratkeluar();

    } else {
        redirect('welcome', 'refresh');
    }
}

function suratkeluar()
{
    $result_per_page = 2;  // the number of result per page

    $config['base_url'] = base_url() . '/Chome/index';
    $config['total_rows'] = $this->Mcrud->record_count();
    $config['per_page'] = $result_per_page;
    $config['use_page_numbers'] = TRUE;
    $config['num_links']    = 20;
    $config["uri_segment"] = 3;
    $this->pagination->initialize($config);

    $data['data_get'] = $this->Mcrud->view();
    $data['pagination'] = $this->pagination->create_links();

    $this->load->view('Vhome', $data);
    $this->load->view('Footer');
}

and this is my Vhome view :

<div class="table-responsive">
   <table class="table table-bordered table-hover table-striped">
      <caption>List Data</caption>
      <thead>
        <tr>
          <th width="80px">ID</th>
          <th>First Name</th>
          <th>Last Name</th>
          <th>Age</th>
          <th>Address</th>
          <th width="80px">Action</th>
        </tr>
      </thead>
      <tbody>
      <?php
        if ($data_get == NULL) {
        ?>
        <div class="alert alert-info" role="alert">Data masih kosong, tolong di isi!</div>
        <?php
        } else {
        foreach ($data_get as $row) {
        ?>
        <tr>
          <td><?php echo $row->idcrud; ?></td>
          <td><?php echo $row->firstname; ?></td>
          <td><?php echo $row->lastname; ?></td>
          <td><?php echo $row->age; ?></td>
          <td><?php echo $row->address; ?></td>
          <td>
            <a href="<?php echo site_url('Chome/edit/' . $row->idcrud); ?>" class="btn btn-warning btn-xs"><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span></a>
            <a href="<?php echo site_url('Chome/delete/' . $row->idcrud); ?>" class="btn btn-danger btn-xs"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span></a>
          </td>
            <?php
            }
        }
            ?>
        </tr>
      </tbody>
   </table>
   <?php echo $pagination; ?>
</div>

and last this is my base url in config.php :

$config['base_url'] = 'http://localhost/arsip/';

1
What is the URL you are getting the 404 on? Have you tried $config['base_url'] = site_url() instead of $config['base_url'] = base_url()?Craig
localhost/arsip/chome/index/2 always getting 404 error. And thanks site_url() is working :D, not getting 404 error anymore. I wonder what is the difference is. Now i am stuck why the result didnt change after i click that linkLa Croix
Because your query is not dynamic, look your limit statement.AkshayP
@Craig It is not correct, delete that comment. Because url function you post there like site_url() and base_url() are instantiated after config file. In matter of fact base_url() and site_url() use $config['base_url'] to be available. There for your suggestion will product Fatal error: Call to undefined function.Tpojka
@A.P but i has test it and site_url() is working. and base_url() always give me 404. I am sure 100% i already autoload 'url'La Croix

1 Answers

0
votes

try to amend this section:

Model:

function view($opset) {
    $ambil = $this->db->from('crud')->order_by('idcrud', 'DESC')->limit(5, $opset)->get();
    if ($ambil->num_rows() > 0) {
        foreach ($ambil->result() as $data) {
            $hasil[] = $data;
        }
        $ambil->free_result();
        return $hasil;          
    }
}

Controllers:

function suratkeluar($opset=NULL)
{
    $result_per_page = 2;  // the number of result per page

    $config['base_url'] = base_url('Chome/suratkeluar');
    $config['total_rows'] = $this->Mcrud->record_count();
    $config['per_page'] = $result_per_page;
    $config['use_page_numbers'] = TRUE;
    $config['num_links']    = 20;
    $config["uri_segment"] = 3;
    $this->pagination->initialize($config);

    $data['data_get'] = $this->Mcrud->view($opset);
    $data['pagination'] = $this->pagination->create_links();

    $this->load->view('Vhome', $data);
    $this->load->view('Footer');
}

work for me :)