Codeigniter上的分页总是在第1页上选择

时间:2013-08-29 06:10:50

标签: php jquery codeigniter pagination

我的分页问题。 它按数据限制显示数据库中的数据,但即使点击第二页,它也总是选择第1页。

我在1个函数中使用了排序和分页 这是我的控制器

    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);

    }

这是我的模特

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;
}

这是我的观点

<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>

我尝试像谷歌一样更改url_segment,人们推荐它。 但是它仍然无法正常工作。但如果我只做一些分页而不进行排序

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

分页正在运行,但不会在第2页上加载第二个数据。它显示与第1页相同的数据。

有什么建议吗?谢谢

4 个答案:

答案 0 :(得分:2)

您的代码有误,您应该使用uri_segment代替url_segment

$config['uri_segment']

答案 1 :(得分:0)

嗨更改其uri_segment而不是url_segment,并且对于base_url使用bese_url helper

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

答案 2 :(得分:0)

我猜你真的需要

$config['uri_segment'] = 5

但请确保$sort_by/$sort_order始终存在或您的分页uri片段将更改

你应该做得更好:

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

并且您的网址例如如下:site.com/izinpulang/index/10/sort_by/sort_order

答案 3 :(得分:-1)

你应该改变这个

$config['uri_segment'] = 5