如何在codeigniter中的模板内添加分页

时间:2015-08-05 02:59:58

标签: php codeigniter pagination

我正在做我朋友的一个项目,这是使用codeigniter构建的,它运行的是模板。所以我的问题是,当我使用分页时它正确查看,但当我点击第二页按钮时,它会在主页面上加载相同的数据。

这是我的代码..

加载主模板的控制器。

public function pg(){
        $this->template->attach($this->resours);
        $this->template->draw('student/view_pg',true);

          $this->load->view('student/view_pg');

    }

用于加载分页视图的控制器。

function pg_index($offset=0,$order_column='st_id',$order_type='asc')
    {
        $this->load->helper('url');

        //Check for valid column
        if(empty($offset)) $offset=0;
        if(empty($order_column)) $order_column='st_id';
        if(empty($order_type)) $order_type='asc';

        //load data
        $Students=$this->Student_model->get_paged_list($this->limit,$offset,$order_column,$order_type)->result();
        //print_r($Students);
        //genarate pagination
        $this->load->library('pagination');
        //$config['base_url']=site_url('student/pg_index/');
        $config['base_url']=site_url('student/pg/');
        $config['total_rows']=$this->Student_model->count_all();
        $config['per_page']=$this->limit;
        $config['uri_segment']=3;
                $this->pagination->initialize($config);
                $data['pagination']=$this->pagination->create_links();


                //genarate table data
                $this->load->library('table');
                $this->table->set_empty(" ");
                $new_order=($order_type=='asc' ? 'desc' : 'asc');
                $this->table->set_heading(
                    'St_id',                                                 'name',
                    'address',
                    'number',
                    'email',

                    'Actions'
                    );
                $i=0+$offset;
                foreach($Students as $Student)
                {

                    $this->table->add_row(++$i,
                                          $Student->name,
                                          $Student->address,
                                          $Student->number,
                                          $Student->email,

                   anchor('get_detail_st(<?php echo $value->st_id;?>);'.$Student->id,'update',array('class'=>'Delete')),
                        anchor('Student/delete/'.$Student->id,'delete',array('class'=>'Delete'))

                        );

                }

               $data['table'] = $this->table->generate();


           $this->load->view('student/Student_pg_crud',$data);




            }

分页视图student_pg_crud

<body>
    <div class="content">

        <div class="data"><?php echo $table; ?></div>
        <div class="paging"><?php echo $pagination; ?></div>

        <br />
        <?php echo anchor('Student/add/','Add new students',array('class'=>'add')); ?>
    </div>
</body>
</html>

绘制主视图。 / view_pg

$_instance = get_instance()
?>


<table width="100%" border="0" cellpadding="10">
    <tr class="ContentTableTitleRow" width="100%">


        <td style="vertical-align: top" width="100%">View Student</td>
    </tr>
    <tr>

        <td style="vertical-align: top"><?php $_instance->pg_index(); ?></td>


    </tr>

</table>

这里有什么问题..我是codeigniter的开销者,在codeigniter中使用模板有什么方法吗?

1 个答案:

答案 0 :(得分:0)

尝试使用$config['base_url']=base_url('student/path/to/your/pagination/controller');

我认为您的控制器是pg_index。 echo每个变量的值并检查总记录数和uri_segment是什么?