如何使用codeigniter中的表类手动添加删除和更新等链接

时间:2015-01-30 05:32:16

标签: php codeigniter

我成功使用codeigniter表类创建表但不知道如何手动添加值,如链接,我成功显示从后端到我的表的所有值,但不知道如何限制特定的值视图以及如何添加删除upate到我的表的链接 代码1:这是我的控制器功能,当我点击管理面板中的查看用户链接

时,它会打开
    public function viewusers()
    {
        $this->load->model('insert_model');
         $im['pic']=$this->insert_model->image_fetc();


            // print_r($aaa); die();
            //  $data['aa'] = "sd"; 

            //$data['books'] = $this->abc();
            $data['books'] = $this->insert_model->pagination();
             //print "<pre>";print_r($data);die();
             $this->load->view('header_view',$im);
             $this->load->view('navside_view_admin');
             $this->load->view('admin_view', $data);

    } 

code2:这是我的模型(insertmodel),其中有分页函数,我在其中使用codeigniter的页面类创建表

    public function pagination(){

        //$this->load->model('insert_model');
         //$data ['query'] = $this->insert_model->view_data(); 
        $this->load->library('table');

        // Load Pagination
        $this->load->library('pagination');
        $total_row = $this->db->count_all("register");
        // Config setup
        $config['base_url'] = base_url().'index.php/welcome/viewusers';
        $config['total_rows'] = $total_row;
        $kk=$config['use_page_numbers'] = TRUE;
        $rpp =$config['per_page'] =15;
        $nop=ceil($total_row/$rpp);
        // I added this extra one to control the number of links to show up at each page.
         $config['num_links'] = $nop;


        // Initialize
        $this->pagination->initialize($config);

        // Query the database and get results
        // Here we add the limit and the offset
        // The second parameter is the limit, since we are showing
        // 10 per page, the limit should be 10

        if($this->uri->segment(3)){
            $page = ($this->uri->segment(3)) ;
            }
            else{
            $page = 1;
            }
        $offset = ($rpp*$page)-$rpp;
        $qry = $this->db->get('register',15, $offset);

        // Create custom headers
       $header = array('id','username','lastname','email' , 'password', 'image','status','operations');
        $tmpl = array ( 'table_open'  => '<table border="1" cellpadding="2" cellspacing="1" class="mytable">' );
       // foreach($qry as $rows)
       /* {
            $links  = anchor('controller/edit/'.$row->id ,'Edit');
        }*/

$this->table->set_template($tmpl);

 /*$this->table->add_row(
        $row->id,
        $row->fname,
        $row->lname,
        $row->email,
        $row->pass,
        $row->image,
           //add the links you created to the last row, corresponding to your 'Links' Header
    );*/
         //Set the headings
        $this->table->set_heading($header);
        // Load the view and send the results
       // print_r($offset);die();
     /*   if($offset == 0)
        {   

        return $data['books'];

        }
        else
        {
       //$data['books'] = $this->abc();
            $im['pic']=$this->insert_model->image_fetc();
        $this->load->view('header_view',$im);
         $this->load->view('navside_view');
           $this->load->view('admin_view', $data);
            }
       // print_r($data); die();

    }*/return $qry->result_array();

}}

code3:这是我的管理视图,其中我显示从表中的数据库获取的数据,这是通过使用codeigniter的表类生成的,我不知道如何在此表中添加手动链接,如删除或更新,以及如何显示表中数据库的限制值,但首先解决显示删除更新到表列等链接的问题...

 <?php


 if(isset($books)){
  echo $this->table->generate($books); ?>
    <?php echo $this->pagination->create_links(); }?>

1 个答案:

答案 0 :(得分:0)

我在我的控制器中做过这件事可能是你得到了一些暗示。

public function index() {
    $this->load->library('table');
    $data_rows = $this->my_model->get();
    $tmpl = array('table_open' => '<table id="sample_1" class="table table-striped table-bordered table-hover dataTable" aria-describedby="sample_1_info">');
    $this->table->set_template($tmpl);
    $this->table->set_heading('Name', 'Type','Reason', 'Status', 'Approval');
    foreach ($data_rows as $row) {
        $approvals = anchor('my_url' . $row->id, 'Approve', array('class' => 'btn green mini')) . " ";
        $approvals .= anchor('my_url' . $row->id, 'Refuse', array('class' => 'btn red mini'));

        //I have added all the columns with two extra columns named approvals links in it.
        $content = array($row->name, $row->type, $row->reason, $status, $approvals);
        $this->table->add_row($content);
    }
    $leaves_table = $this->table->generate();
    $this->load->view('layouts/header');
    $this->load->view('layouts/sidebar');
    $this->load->view('leaves/lists', array('leaves_table' => $leaves_table));
    $this->load->view('layouts/footer');
}