2011-12-03 56 views
1

衝突的我有數據的頁面,從數據庫中抽取。我需要能夠刪除顯示所有數據行的頁面上的記錄。笨 - 分頁與刪除

所以客戶/指數顯示所有行。控制器:

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

如何'delete_customer()',而不必傳遞給它的一些工作? –

+0

對不起,我忽略在模型中發佈我的刪除功能。它只是使用URL的第三部分來獲取行的ID。所以如果是customers/delete/5,它會刪除數據庫中的第三行。它也被用來抵消表中顯示的記錄,這是問題所在。我不知道這通常是如何處理的。 – ohiock

+0

模型不應該是指除了數據庫和它給變量的任何其他。所以在Controller中你應該調用'$ this-> customers_model-> delete_customer($ id);',然後在模型中使用'delete_customer($ id)'作爲ID,而不是依賴模型中的URL。你可以使用Controller方法'function delete($ id)'而不是'function delete()'來獲取ID。 –

回答

0

我認爲最好的辦法是使重定向在數據庫中刪除行的索引頁後:

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

這似乎是做我所需要的。不知道該怎麼做。謝謝。 – ohiock

+0

這是簡單的方法。最好的辦法是重定向到同一個頁面,並檢查這個頁面是否存在,或者不存在,然後是否存在。我不知道該怎麼做。也許有人可以提出一個想法? – Viktors