Cyb*_*kie 14 php session codeigniter
我了解了flashdata并决定使用它来显示基于数据库交互的消息.
例如,如果没有行受影响,我想显示 Post id is invalid or does not exist!
在我的模型中
function delete_post($post_id)
{
$this->db->where('user_id', $user_id);
$this->db->where('post_id', $post_id);
$this->db->delete('posts');
if ($this->db->affected_rows() == 0)
{
$this->session->set_flashdata('result', 'Post id is invalid or does not exist!');
redirect('/posts/management');
return FALSE;
}
else
{
redirect('/posts/management');
return TRUE;
}
}
Run Code Online (Sandbox Code Playgroud)
在我看来
if ($this->session->flashdata('result') != ''):
echo $this->session->flashdata('result');
endif;
Run Code Online (Sandbox Code Playgroud)
这似乎工作正常,但CI文档中没有如何在MVC之间传递flashdata的示例.我很担心..我这样做了吗?
编辑:我似乎从之前的尝试中保留了FALSE和TRUE.我可能不需要那个.
Wes*_*rch 15
会话数据随时可在您的应用程序中的任何位置使用.直接从视图文件中调用它是正确的,因此它在Controller中进行设置.
有没有必要在所有把它作为数据$this->load->view()-它是多余的.为什么在这种情况下首先将它分配给flashdata?
你这样做的方式是正确的.
编辑:我刚刚看到你在模型而不是控制器中设置它 - 这是非常有争议的.我建议从Model调用中返回一个值,然后在Controller中根据它设置消息.
| 归档时间: |
|
| 查看次数: |
38167 次 |
| 最近记录: |