Christ Samuel
Christ Samuel

Reputation: 109

Pagination on Codeigniter always selected on page 1

I've a problem with pagination. It show the data on database by its limit,but it always select page 1 even i click the second page.

I'm using sorting and pagination in 1 function This is my controller

    function index($sort_by = 'title', $sort_order= 'asc', $offset = 0)
    {
        $limit = 2;

        $data['fields'] = array(
            'nama' => 'Nama',
            'jabatan' => 'Jabatan',
            'jampulang' => 'Jam Pulang',
            'alasan' => 'Alasan',
            'pendingtask' => 'Pending Task',
            'status' => 'Status'
        );

        $result = $this->karyawan_pulang->search($limit,$offset, $sort_by, $sort_order);

        $data['pulang'] = $result['rows'];
        $data['num_results'] = $result['num_rows'];
        $data['main_content'] = 'karyawanpulangarea';

        // pagination
        $config = array();
        $config['base_url'] = site_url("izinpulang/index/$sort_by/$sort_order");
        $config['total_rows'] = $data['num_results'];
        $config['per_page'] = $limit;
        $config['url_segment'] = 4;
        $config['num_links'] = 3;
        $config['use_page_numbers'] = TRUE; 
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();

        $data['sort_by'] = $sort_by;
        $data['sort_order'] = $sort_order;

        $this->load->view('includes/template', $data);

    }

And this is my model

function search($limit, $offset, $sort_by, $sort_order)
{
    $sort_order = ($sort_order == 'desc') ? 'desc' : 'asc';
    $sort_columns = array('nama', 'jabatan', 'jampulang', 'alasan', 'pendingtask', 'status');
    $sort_by = (in_array($sort_by, $sort_columns)) ? $sort_by : 'nama';

    // result query
    $q = $this->db->select('pid, nama, jabatan, jampulang, alasan, pendingtask, status')
        ->from('izin_pulang')
        ->limit($limit, $offset)
        ->order_by($sort_by, $sort_order);

    $ret['rows'] = $q->get()->result();

    // count query
    $q = $this->db->select('COUNT(*) as count', FALSE)
        ->from('izin_pulang');

    $tmp = $q->get()->result();
    $ret['num_rows'] = $tmp[0] -> count;


    return $ret;
}

This is my view

<h2><a href="<?php echo base_url();?>site/form_area"><img src='<?php echo base_url();?>assets/img/backs.png'/>Back</a></h2>
<h3>
<?php

if (empty($pulang)) {

echo "There's No Data!";


}

else 
{

?>
</h3>
<div id="content" class="container_center">
<h3> Daftar Karyawan Izin Pulang</h3>
<div id="count">
    Jumlah Data : <?php echo $num_results;?>
</div>
<div class="table">
<table>
<thead>
    <tr align="center">
        <?php foreach ($fields as $field_name => $field_display):?>
        <th <?php if($sort_by == $field_name) echo "class=\"sort_$sort_order\""?>>
            <?php echo anchor("izinpulang/index/$field_name/" .
                (($sort_order == 'asc' && $sort_by == $field_name) ? 'desc' : 'asc') ,
            $field_display); ?>
        </th>
        <?php endforeach;?>
        <th>Edit</th>
        <th>Download</th>
    </tr>
</thead>
<?php

foreach ($pulang as $data):

?>
<tbody>
    <tr align="center">
        <?php foreach ($fields as $field_name => $field_display):?>
        <td>
            <?php echo $data->$field_name; ?>
        </td>
        <?php endforeach;?>
         <td> <a href="<?php echo base_url();?>izinpulang/baca/<?php echo $data->pid; ?> "><img src='<?php echo base_url();?>assets/img/edit.png'/></a></td>
         <td> <a href="<?php echo base_url();?>izinpulang/download/<?php echo $data->pid; ?>"><img src='<?php echo base_url();?>assets/img/downloads.png'/></a> </td>         
    </tr>
</tbody>
<?php

endforeach;

?>

</table>
<?php if (strlen($pagination)):?>
<div id="count">
    Pages : <?php echo $pagination;?>
</div>
<?php endif; ?>
</div>
<?php

}
?>
<br>
</div>

I try to change the url_segment just like google and people recommend it. But however its still not working. But if i only do some pagination and not do sorting

$config['base_url'] = site_url("izinpulang/index/");

The pagination is working but doesn't load the second data on page 2. It show the same data as page 1.

Any advice ? Thank you

Upvotes: 2

Views: 2484

Answers (6)

Engr Talha
Engr Talha

Reputation: 404

    $config["base_url"] = base_url();
    $config["total_rows"] = $this->authors_model->get_count();
    $config["per_page"] = 10;
    $config["uri_segment"] = 2;

    $this->pagination->initialize($config);

Upvotes: 1

Soumik Mukhopadhyay
Soumik Mukhopadhyay

Reputation: 11

$config['use_page_numbers'] = TRUE;

Just use this inside your $config & boom! It will be fixed.

Upvotes: 0

Ankur Chandravanshi
Ankur Chandravanshi

Reputation: 1

you should use change this

$config['uri_segment'] = 5

Upvotes: -1

Filippo oretti
Filippo oretti

Reputation: 49843

i guess you really need

$config['uri_segment'] = 5

but be sure that $sort_by/$sort_order are always present or your pagination uri segment will change

you should do better:

$config['base_url'] = site_url("izinpulang/index");
$config['uri_segment'] = 3;
$config['suffix'] = "/$sort_by/$sort_order";

and your urls will looks like for example : site.com/izinpulang/index/10/sort_by/sort_order

Upvotes: 1

umefarooq
umefarooq

Reputation: 4574

hi make changes its uri_segment not url_segment, and for base_url use bese_url helper

$config['base_url'] = base_url("izinpulang/index/");

Upvotes: 0

Bora
Bora

Reputation: 10717

Your codes wrong, you should use uri_segment instead of url_segment

$config['uri_segment']

Upvotes: 2

Related Questions