Reputation: 1720
I'm using the following code to manage pagination in search results:
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'])):
...
All of which works fine on my development computer, but chokes on the live website; the final if()
statement does not evaluate to true.
However, if I echo out the $post['search-notes']
variable either before or within the final if()
statement, it works!
It's utterly bizarre, and I've never encountered anything like it before.
I'm using CodeIgniter 2.0
On a side note, the original title had much more specificity: "Problem with set_flashdata()
function in CodeIgniter". But due to some excitable and excessive moderation rules, I've had to water it down to something less meaningful.
Upvotes: 1
Views: 6915
Reputation: 99484
The first thing you should attend is once you call $this->session->flashdata('search-notes')
method, it unsets the 'search-notes'
item from session.
So, when you check the $this->session->flashdata('search-notes')
at the second time, 'search-notes'
will no longer exists.
If you want to keep the item in session, use set_userdata()
and userdata()
instead.
Also, you can use keep_flashdata('search-notes')
after set_flashdata()
or before the first call of flashdata()
to preserve the flashdata variable through an additional request.
As a side point:
There's no need to check isset()
and !empty()
together. empty()
does NOT generate a warning if the variable does not exist, and returns FALSE
.
There is also a nice tutorial on nettuts+ could be useful.
Jus as a Demo:
do NOT copy, check the logic.
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'])):
// ...
If you need to keep the search-notes
item in session, use the following at the first if
statement:
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');
} // ...
Upvotes: 3