if then else to display 2 views on one function called

喜夏-厌秋 提交于 2019-12-11 12:59:50

问题


i have got a little problem with redirect function, I have a controller function named "someview" and I also created a view file of the same name (someview.ctp) The controller function will do some stuff(query data from the model). it can be simply described as follows

function someview()
{
    $result=$this->User->getdatafrommodel();
    if(null!=$result)
    {
        //do something
    }
    else
    {
        $this->redirect('usernotexist');
    }
}

function usernotexist()
{
    $this->loadSkin();
}

I also created a page named "usernotexist.ctp" which I would like to display some information as to when the specified user doesn't exist in the database system. However, my previous function (someview) always execute "if" and "else" both after it is called. If I eliminate the "else" part in that function, it then works correctly for me; the page named "someview.ctp" is displayed. The $result value returned from the getdatafrommodel function is correct. Thank you for any help.


回答1:


try

if(!empty($result)){
    //do something
}else{
    $this->redirect(array('action'=>'usernotexist'));
}

hope this helps



来源:https://stackoverflow.com/questions/9628018/if-then-else-to-display-2-views-on-one-function-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!