PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, March 14, 2022

[FIXED] Pagination on Codeigniter always selected on page 1

 March 14, 2022     codeigniter, jquery, pagination, php     No comments   

Issue

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


Solution

Your codes wrong, you should use uri_segment instead of url_segment

$config['uri_segment']


Answered By - Bora
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing