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.
Best Answer
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()
anduserdata()
instead.Also, you can use
keep_flashdata('search-notes')
afterset_flashdata()
or before the first call offlashdata()
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 returnsFALSE
.CI reference
There is also a nice tutorial on nettuts+ could be useful.
Jus as a Demo:
do NOT copy, check the logic.
If you need to keep the
search-notes
item in session, use the following at the firstif
statement: