2013-02-28 53 views
0

我一直在試圖找出問題一段時間,但看不到任何,真的很想找到一些幫助找到問題。CodeIgniter表類顯示重複的行

在我的控制器中,我構建了一個表並將它回顯到另一個頁面上,但是當我看到結果時,它們會被寫入兩次。

 $results = $this->main_model->search(); 

     $table_row = array(); 
     foreach ($results->result() as $product) 
     { 
      $table_row = NULL; 
      $table_row[] = $product->product_id; 
      $table_row[] = $product->title; 
      $table_row[] = $product->description; 
      $table_row[] = $product->price; 
      $table_row[] = $product->stock; 
      $table_row[] = $product->cat_name; 
      $table_row[] = $product->subcat_name; 
      $table_row[] = anchor('admin/edit/' . $product->product_id, 'edit'); 

      $this->table->add_row($table_row); 
     }  

     $table = $this->table->generate($results); 

     $data['table'] = $table; 

     $this->load->view('search',$data); 

會真的很感謝一些幫助,我認爲我返回結果的方式有問題,但不完全確定它是什麼。我嘗試了幾種方法,但對此很新穎。在此先感謝

回答

1

我認爲你有重複東陽你手動與此代碼

foreach ($results->result() as $product) 
    { 
     $table_row = NULL; 
     $table_row[] = $product->product_id; 
     $table_row[] = $product->title; 
     $table_row[] = $product->description; 
     $table_row[] = $product->price; 
     $table_row[] = $product->stock; 
     $table_row[] = $product->cat_name; 
     $table_row[] = $product->subcat_name; 
     $table_row[] = anchor('admin/edit/' . $product->product_id, 'edit'); 

     $this->table->add_row($table_row); 
    }  

UPDATE

更改這一行添加行

$results_table = $this->table->generate($results); 

至此

$results_table = $this->table->generate(); 
+0

對不起,我一直在試圖弄清楚,但如果我刪除你指出的最後一行代碼,我不能返回任何東西視圖。它給我一個未定義的變量錯誤爲$ results_table在我的控制器功能 – a7omiton 2013-02-28 17:26:35

+0

對不起,請參閱更新的答案 – 2013-02-28 17:38:00

+0

我想我以前嘗試過,當我肆虐一些解決方案,但它的作品。感謝您幫助我 – a7omiton 2013-02-28 17:43:03

0

$this->table->generate();它返回一個包含生成表的字符串。如果您沒有手動添加行,則接受可選參數,該參數可以是數組或數據庫結果對象。

但在你的情況,你已經通過

$this->table->add_row($table_row); 

,然後再你在

$results_table = $this->table->generate($results); 

將他們添加的行,而不是添加註釋行的行

$table_row = array(); 
     foreach ($results->result() as $product) 
     { 
      $table_row = NULL; 
      $table_row[] = $product->product_id; 
      $table_row[] = $product->title; 
      $table_row[] = $product->description; 
      $table_row[] = $product->price; 
      $table_row[] = $product->stock; 
      $table_row[] = $product->cat_name; 
      $table_row[] = $product->subcat_name; 
      $table_row[] = anchor('admin/edit/' . $product->product_id, 'edit'); 

//   $this->table->add_row($table_row); comment this line 
     }  

     $results_table = $this->table->generate($results); 
+0

我試過評論,但像法比奧說,這對我來說也沒有意義 – a7omiton 2013-02-28 17:35:51