渲染视图后的动作(codeigniter)

时间:2015-09-25 21:00:39

标签: php codeigniter

我想知道在使用codeigniter呈现视图后是否可以进行不同的操作,例如:

public function my_function()
{
    $data['var1'] = $this->make_one_action();
    $data['var2'] = $this->make_other_action();
    $this->load->view('a_view', $data);
    //and now... more actions??
    $this->make_dataBase_action();
}

我想要做的是处理数据库的一些数据,但不要等待用户进行此处理(当新视图不会在数据库上向用户显示此操作时)。

还有一点......(一个不同的问题,但相关),我可以做这样的事情:

public function my_function()
{
    $data['var1'] = $this->make_one_action();
    $data['var2'] = $this->make_other_action();
    redirect(base_url()); //here is the difference
    //and now... more actions??
    $this->make_dataBase_action();
}

在这种情况下,我想将用户重定向到新页面,然后在数据库上进行处理。

谢谢。

1 个答案:

答案 0 :(得分:1)

重定向将阻止PHP脚本执行,如果您希望可以加载页面并发送ajax请求以触发查询
假设您使用的是jQuery:在Controller上添加方法
function executeDbOnpageLoad(){$this->make_dataBase_action();} //add _get to the method name if CI Version 2

并将此行添加到“a_view.php”

$(document).ready(function(){
   $.ajax("index.php?/ControllerName/executeDbOnpageLoad()")
})

(你需要让它更加安全,并确保用户不会多次触发此功能)