我的分页有问题。它按限制显示数据库中的数据,但即使我单击第二页,它也总是选择第 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 页相同的数据。
有什么建议吗?谢谢