1

我正在使用以下代码来管理搜索结果中的分页:

if ($this->input->post('search-notes') && (is_string($this->input->post('search-notes')) || is_string($this->input->post('search-notes')))):
    $this->session->set_flashdata('search-notes', $_POST['search-notes']);
    $post['search-notes'] = $this->input->post('search-notes');
elseif ($this->session->flashdata('search-notes')):
    $this->session->set_flashdata('search-notes', $this->session->flashdata('search-notes'));
    $post['search-notes'] = $this->session->flashdata('search-notes');
endif;
if (isset($post['search-notes']) && is_string($post['search-notes']) && !empty($post['search-notes'])):
...

所有这些在我的开发计算机上都可以正常工作,但在实时网站上却卡住了;最后的if()陈述不评估为真。

但是,如果我在最终语句$post['search-notes']之前或之内回显变量,它就可以工作!if()

这太奇怪了,我以前从未遇到过这样的事情。

我正在使用 CodeIgniter 2.0

在旁注中,原始标题具有更多的特异性:“ set_flashdata()CodeIgniter 中的函数问题”。但是由于一些令人兴奋和过度的适度规则,我不得不把它淡化为一些不那么有意义的东西。

4

1 回答 1

3

您应该参加的第一件事是一旦您调用$this->session->flashdata('search-notes')方法,它就会'search-notes'从会话中取消设置项目。

所以,当你$this->session->flashdata('search-notes')第二次检查时,'search-notes'将不再存在。

如果您想将项目保持在会话中,请使用set_userdata()anduserdata()代替。

此外,您可以在第一次调用keep_flashdata('search-notes') 之后 set_flashdata() 之前flashdata()通过附加请求来保留 flashdata 变量。

附带一点:
没有必要检查isset()!empty()在一起。empty() 如果变量不存在,则不会生成警告并返回FALSE

CI 参考

还有一个关于 nettuts+的不错的教程可能会很有用。


就像一个演示:
不要复制,检查逻辑。

if ($_POST['search-notes'] AND is_string($_POST['search-notes']))
{
    $post['search-notes'] = $this->input->post('search-notes'/*, TRUE*/ /* Enable XSS filtering */);
    $this->session->set_flashdata('search-notes', $post['search-notes']);
}
elseif ($searchNotes = $this->session->flashdata('search-notes'))
{
    $post['search-notes'] = $searchNotes;
}

if (! empty($post['search-notes']) AND is_string($post['search-notes'])):
// ...

如果您需要将search-notes项目保持在会话中,请在第一条if语句中使用以下内容:

if ($_POST['search-notes'] AND is_string($_POST['search-notes']))
{
    $post['search-notes'] = $this->input->post('search-notes'/*, TRUE*/ /* Enable XSS filtering */);
    $this->session->set_flashdata('search-notes', $post['search-notes']);
    // Keep the flashdata through an additional request
    $this->session->keep_flashdata('search-notes');

} // ...
于 2013-06-29T11:58:37.687 回答