我的控制器上有一个函数get()
。我用这个函数从数据库中获取数据,然后将其保存到$data
变量。
可以在另一个函数上调用get()
函数,然后将$data
变量发送到视图中吗?我想制作两个视图,一个是经典视图,另一个是现代视图。
这是我的代码:
public function get()
{
$data['all'] = $this->genbamodel->getAll();
$data['delay'] = $this->genbamodel->getDelay();
$data['ontime'] = $this->genbamodel->getOntime();
$data['ahead'] = $this->genbamodel->getAhead();
$data['unloading'] = $this->genbamodel->getUnloading();
$data['topdelay'] = $this->genbamodel->getTopDelay();
$data['topahead'] = $this->genbamodel->getTopAhead();
$data['jumlah'] = count($this->genbamodel->getID())+1;
for ($j=1; $j < $data['jumlah']; $j++) {
$datas['lalax'.$j] = $this->genbamodel->getRecord($j);
$data['lala'.$j] = array_reverse($datas['lalax'.$j]);
}
$data['time'] = $this->genbamodel->getTime();
}
public function home_classic()
{
$this->get();
$this->load->view('home_classic',$data);
}
public function home_modern()
{
$this->get();
$this->load->view('home_modern',$data);
}
答案 0 :(得分:1)
希望这会对你有所帮助:
您的get()
方法应为return
$ data
public function get()
{
$data['all'] = $this->genbamodel->getAll();
$data['delay'] = $this->genbamodel->getDelay();
$data['ontime'] = $this->genbamodel->getOntime();
$data['ahead'] = $this->genbamodel->getAhead();
$data['unloading'] = $this->genbamodel->getUnloading();
$data['topdelay'] = $this->genbamodel->getTopDelay();
$data['topahead'] = $this->genbamodel->getTopAhead();
$data['jumlah'] = count($this->genbamodel->getID())+1;
for ($j=1; $j < $data['jumlah']; $j++) {
$datas['lalax'.$j] = $this->genbamodel->getRecord($j);
$data['lala'.$j] = array_reverse($datas['lalax'.$j]);
}
$data['time'] = $this->genbamodel->getTime();
return $data;
}
<{1>}中的就像这样做
home_classic()
public function home_classic()
{
$data = $this->get();
$this->load->view('home_classic',$data);
}
中的执行相同的
home_modern()
了解更多:https://www.codeigniter.com/user_guide/general/views.html#adding-dynamic-data-to-the-view