0

我一直在尝试找到问题一段时间,但看不到任何问题,真的希望在找到问题方面得到一些帮助。

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

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

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

4

2 回答 2

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();
于 2013-02-28T16:38:05.350 回答
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);
于 2013-02-28T16:41:06.473 回答