0

我想知道这两种形式中哪一种在 Codeigniter 中更有效。在选项 1 中,数据数组被传递给所有视图。在选项 2 中,每个数据数组都有不同的名称,以免忘记每个视图的所有信息。我想假设选项 2 是正确的,我的意思是重命名每个数组,而不是在每个视图上花费不必要的信息。我对么?Codeigniter 是否重要?感谢您阅读帖子问候

选项1

public function alguna_funcion(){


       $data['datos1'] = $this->modelo->function1();
       $data['widget1'] = $this->load->view('widget1',$data,TRUE);
       $data['datos2'] = $this->modelo->function2();
       $data['widget2'] = $this->load->view('widget2',$data,TRUE);
       $data['datos3'] = $this->modelo->function3();
       $data['widget3'] = $this->load->view('widget3',$data,TRUE);
       $data['datos4'] = $this->modelo->function4();
       $data['widget4'] = $this->load->view('widget4',$data,TRUE);

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


   }

选项 2

public function alguna_funcion(){


       $data1['datos1'] = $this->modelo->function1();
       $data['widget1'] = $this->load->view('widget1',$data1,TRUE);
       $data2['datos2'] = $this->modelo->function2();
       $data['widget2'] = $this->load->view('widget2',$data2,TRUE);
       $data3['datos3'] = $this->modelo->function3();
       $data['widget3'] = $this->load->view('widget3',$data3,TRUE);
       $data4['datos4'] = $this->modelo->function4();
       $data['widget4'] = $this->load->view('widget4',$data4,TRUE);

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


   }
4

2 回答 2

1

那么你可以这样做

public function alguna_funcion()
{
    $data['datos'] = $this->modelo->function1();
    $data['widget1'] = $this->load->view('widget1',$data,TRUE);
    unset($data['datos']);

    $data['datos'] = $this->modelo->function2();
    $data['widget2'] = $this->load->view('widget2',$data,TRUE);
    unset($data['datos']);

    $data['datos'] = $this->modelo->function3();
    $data['widget3'] = $this->load->view('widget3',$data,TRUE);
    unset($data['datos']);

    $data['datos'] = $this->modelo->function4();
    $data['widget4'] = $this->load->view('widget4',$data,TRUE);
    unset($data['datos']);

    // Now you have only widgets pass them to view
    $this->load->view('home_view',$data);
}
于 2013-02-04T17:48:08.113 回答
0

这不是 CodeIgniter 的错。只要 data_i 都相同,就可以通过所示的两种方式之一来访问它们的元素,然后后一种情况似乎是多余的。-

于 2013-02-04T14:41:24.690 回答