CodeIgniter - разбиение на страницы конфликтует с удалением - PullRequest
2 голосов
/ 03 декабря 2011

У меня есть страницы данных, извлеченные из базы данных. Мне нужно иметь возможность удалить запись на странице, которая отображает все строки данных.

Таким образом, клиенты / индекс отображает все строки. Контроллер:

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>

Так что я думаю, что нумерация страниц мешает, потому что, когда я нажимаю ссылку удаления, она отправляет ее клиентам / delete / #. Таким образом, он удаляет запись, как и должно быть, но отображаемые результаты различаются, потому что я считаю, что нумерация страниц отключена путем установки отображаемых строк по номеру в URL (который на самом деле является просто идентификатором строки).

Любая помощь очень ценится.

1 Ответ

0 голосов
/ 06 декабря 2011

Я думаю, что лучший способ - это сделать перенаправление после удаления строки в БД на страницу индекса:

public function delete() {  
    $this->customers_model->delete_customer();
    redirect('customers/index');
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...