CodeIgniter表类显示重复的行

时间:2013-02-28 16:27:20

标签: codeigniter duplicates

我一直试图找到问题一段时间,但看不到任何问题,真的希望找到问题有所帮助。

在我的控制器中,我正在构建一个表格并将其回显到另一个页面,但是当我看到结果时,它们会被写入两次。

        $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);

非常感谢一些帮助,我认为我返回结果的方式存在问题,但不完全确定它是什么。我尝试了几种方法,但对此我很陌生。提前致谢

2 个答案:

答案 0 :(得分: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);
    }    

<强>更新

更改此行

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

到此

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

答案 1 :(得分: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);