Codeigniter的分页无法正常工作

时间:2014-04-19 02:58:03

标签: php codeigniter

我是CI的新手,

我从表中选择了一个数据库。现在想创建分页。 Pagination链接出现了。但是当我点击第2页时,错误消息是404 Page Not Found。我该如何解决这个问题?

这是剧本 - 控制器上的

function post()
 {

   $table_row_count = $this->db->count_all('post');

   $config['base_url'] = 'http://localhost/septiyo/index.php/admin/page/';
   $config['total_rows'] = $table_row_count;
   $config['per_page'] = 10;
   $this->pagination->initialize($config);

    $this->load->model('admin_model');  
    $data_admin['dataadmin']=$this->admin_model->view_post_model();
    $this->load->view('post_view',$data_admin);


 }//end of funciton
模型上的

public function view_post_model()
    {
        $hasil = $this->db->get('post');
        return $hasil->result();

    }//end funciton

在视图

echo "<table border='1' cellpadding='1' cellspacing='0' align='center' width='600'>
     <tr>
        <th>Judul</th>
        <th>Posting</th>
        <th>Ketegori</th>
        <th>Tanggal</th>
        <th>Aksi</th>
     </tr>";
foreach($dataadmin as $row)
{
    //$this->load->helper('text');
    $post = $row->post;
    $string = word_limiter($post,10);

    echo "<tr>";    
    echo "<td>$row->title</td>";
    //echo "<td>$row->post</td>";
    echo "<td>$string</td>";
    echo "<td>$row->kategori</td>";
    echo "<td>$row->tgl</td>";
    echo "<td>";
    echo anchor('company_controller/view_edit_employee/'.$row->id, 'Edit')." | ";
    echo anchor('company_controller/confirm_delete_employee/'.$row->id, 'Hapus');
    echo "</td>";
    echo "</tr>";
}    


echo "</table><br>";
echo $this->pagination->create_links();

任何人都可以帮我解决这个问题吗?

3 个答案:

答案 0 :(得分:0)

问题可能出在您设置的base_url中。如果分页显示正确,我打赌问题必须存在。如果您没有意识到正确的网址,请尝试在浏览器中手动更改网址。

答案 1 :(得分:0)

this may help you.

// On Controller Side

function post(){

   $limit = 10;
   $offset = $this->uri->segment(3);    

   $table_row_count = $this->db->count_all('post');

   $config['base_url'] = 'http://localhost/septiyo/index.php/admin/page/';

   $config['total_rows'] = $table_row_count;

   $config['per_page'] = $limit;
   $config['uri_segment'] = 3;  
   $this->pagination->initialize($config);

    $this->load->model('admin_model');  
    $data_admin['dataadmin']=$this->admin_model->view_post_model($limit,$offset);
    $this->load->view('post_view',$data_admin);


 }//end of funciton


// On Model Side
public function view_post_model($limit, $offset)
    {
    $hasil = $this->db->get('post', $limit , $offset);
        return $hasil->result();

    }//end funciton

what you are doing is something wrong, since to get the result is paginated form you need to  instruct the database to return the result in breaks, and this is what the limit and offset will do for you, please check this once.

I have not checked your view, that you can examine, and also ensure that you are getting the correct uri_segment and also you have ulr helper enabled in config.

答案 2 :(得分:0)

 function post()
 {
 $offset = $this->uri->segment(3);
    if(!$offset)
     $offset=0;

 $table_row_count = $this->db->count_all('post');

 $config['base_url'] = base_url().'admin/page';
希望它能帮到你......

相关问题