CodeIgniter - 分页与删除冲突

时间:2011-12-03 16:56:06

标签: php mysql codeigniter pagination

我有从数据库中提取的数据页面。我需要能够删除页面上显示所有数据行的记录。

所以customers / index显示所有行。控制器:

public function index($page = 'customer_list', $title = 'Customer List') {      
    $this->exists($page);

    $data['customer'] = $this->customers_model->get_customers();
    $data['title'] = $title;
    $data['content'] = 'content/'.$page.'.php';

    $this->load->view('frame/grab', $data);
}

public function delete() {  
    $this->customers_model->delete_customer();

    $this->index();
}

它使用这个模型函数:

public function get_customers() {
    $this->load->library('pagination');

    //Config for pagination
    $config['base_url'] = '**cant post links**/customers/index';
    $config['total_rows'] = $this->db->get('customers')->num_rows();
    //Rows per page
    $config['per_page'] = 10;
    //Number of links shown to navigate
    $config['num_links'] = 20;

    //Initializes the pagination
    $this->pagination->initialize($config);

    $query = $this->db->get('customers', $config['per_page'], $this->uri->segment(3));
    return $query->result();
}

以下是我的观点:

    <div class="main-content">
        <div class="main-content-header">
            <h2 class="floatl"><?php echo $title; ?></h2>
            <a class="add-new floatr" href="<?php echo base_url(); ?>customers/add"><h4>Add New Customer &raquo;</h4></a>

            <div class="clear"></div>
        </div>

        <div class="sort-container">
            Sort by:
            <ul class="sort">
                <li><a href="#">ID Ascending</a></li>
                <li><a href="#">ID Descending</a></li>
                <li><a href="#">First Name</a></li>
                <li><a href="#">Last Name</a></li>
            </ul>
        </div>

        <?php
        //Creates the template for which the table is built to match
        $tmpl = array ( 'table_open'          => '<table class="data-table">
                                                    <tr class=\'tr-bgcolor table-header\'>
                                                        <td>#</td>
                                                        <td>First Name</td>
                                                        <td>Last Name</td>
                                                        <td>Edit</td>
                                                        <td>Delete</td>
                                                    </tr>',

                        'heading_row_start'   => '<tr>',
                        'heading_row_end'     => '</tr>',
                        'heading_cell_start'  => '<th>',
                        'heading_cell_end'    => '</th>',

                        'row_start'           => '<tr class="tr-bgcolor">',
                        'row_end'             => '</tr>',
                        'cell_start'          => '<td>',
                        'cell_end'            => '</td>',

                        'row_alt_start'       => '<tr>',
                        'row_alt_end'         => '</tr>',
                        'cell_alt_start'      => '<td>',
                        'cell_alt_end'        => '</td>',

                        'table_close'         => '</table>'
                      );

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

        //Creates table rows from the rows of data in the db which were created as the customer array
        foreach ($customer as $row) :
            $this->table->add_row(
                $number = array('data' => $row->rec_num, 'class' => 'center'),
                $row->last_name,
                $row->first_name,
                $edit = array('data' => '[ <a href="#">Edit</a> ]'),
                $edit = array('data' => '[ <a href="'.base_url().'customers/delete/'.$row->rec_num.'">Delete</a> ]')
            );
        endforeach;

        //Creates the table based on the rows determined above
        echo $this->table->generate();

        //Creates page links
         echo $this->pagination->create_links();
        ?>
    </div>

所以我认为分页会妨碍,因为当我点击删除链接时,它会将其发送给customers / delete /#。因此它会删除记录,但是显示的结果会有所不同,因为我认为分页是关闭的,用URL中的数字设置显示的行(实际上只是行的ID)。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

我认为最好的方法是在将db中的行删除到索引页面后进行重定向:

public function delete() {  
    $this->customers_model->delete_customer();
    redirect('customers/index');
}