Welcome Guest, Not a member yet? Register   Sign In
(CLOSED) - Use of flashdata
#1

[eluser]gryzzly[/eluser]
Hy there.

I'm new to CodeIgniter (2 days ago), and im' getting some issues with the session->flashdata() behavior. It never displays any of my messages, even using the basic configuration as described here.

What i actually coded is :

In config.php , autoload of session library ;

After a Db successful insert, in the controller :
Code:
// Set a flash message
$msg = "Item created.";
$this->session->set_flashdata('flashmsg', $this->load->view('flashmsg/success', $msg));

I created the /application/view/flashmsg/success.php file , that contains :
Code:
<p class="flashmsg success">&lt;?php echo $msg; ?&gt;</p>

And finally, in my theme header, i'm calling :
Code:
&lt;?php $this->session->flashdata('flashmsg'); ?&gt;

What is wrong with my code ?

Why, even with the simple configuration, i do not get my flashes ?

Thanks for help.
#2

[eluser]jedd[/eluser]
Howdi, and welcome to the CI forums.

I think you have an easy problem. Instead of:

Code:
&lt;?php $this->session->flashdata('flashmsg'); ?&gt;

Try:
Code:
&lt;?php echo $this->session->flashdata('flashmsg'); ?&gt;


EDIT: Though you are more likely to want to pull the data out ( $bob = $this->session->flashda...) in your controller, and hand $bob out to your view(s).
#3

[eluser]gryzzly[/eluser]
You're right. i should "echo". Good to have an outside eye ... stupid mistakes.
But, it doesn't change anything. It does not flash at all.

I probably forgot an important information :

Just after db insert, and flashmessage definition, i'm redirecting to an other controller/action pair ... Could it be the thing ?
#4

[eluser]gryzzly[/eluser]
Code correction :
Code:
// Set a flash message
$msg['result'] = "Item created.";
$this->session->set_flashdata('flashmsg', $this->load->view('flashmsg/success', $msg));

// Redirects to add (current action)
// redirect('categories/add');
If i DO NOT redirect, it flashes ...
Code:
&lt;?php echo $this->session->flashdata('flashmsg'); ?&gt;

Code:
<p class="flashmsg success">&lt;?php echo $result; ?&gt;</p>

But :
Code:
// Set a flash message
$msg['result'] = "Item created.";
$this->session->set_flashdata('flashmsg', $this->load->view('flashmsg/success', $msg));

// Redirects to add (current action)
redirect('categories/add');
Now, i'm pretty sure that the thing.
No flash ... Flashdata is destroyed when redirecting.

Can anyone confirm this idea ?
If i'm right, what the way to go ?

EDIT : tryed keep_flashdata() without success ... something wrong with me ? :-s
#5

[eluser]gryzzly[/eluser]
Ok, i was right, flashdata() is destroyed on/before (?) redirection.

The way to go is keep_flashdata(), but i do not really understand how to make use of it. Is there any example of it's use somewhere ?
#6

[eluser]jedd[/eluser]
Well, apart from the CI User Manual, specifically the Flashdata section in the [url="http://ellislab.com/codeigniter/user-guide/libraries/sessions.html"]Session Class[/url] page.

So, yes, you're right - you can use the $this->session->keep_flashdata('item'); thing - and it works just like how you set the data in the first place, and is effectively just setting itself for the next 'url jump' based on the data that's currently in it.

So you'd just do something like this on the page where you're about to redirect from:
Code:
$this->session->keep_flashdata('flashmsg');

You might be better off using real session data though. It's not that scary - I find it less troublesome to track than keeping flashdata alive during redirects etc.
#7

[eluser]gryzzly[/eluser]
Ok, thanks for advices. But for this project, i want/need to use core functionnalities when they exist, as most as possible.

At least, strange thing, i'll keep the following simple, and remove all my "complicated" view calls :

In my controller :
Code:
// Set a flash message
//$msg['result'] = "Item created.";
//$this->session->set_flashdata('flashmsg', $this->load->view('flashmsg/success', $msg));
$this->session->set_flashdata('flashmsg', '<p style="color:green;">Item created.</p>');

// Redirects to index
redirect('categories');

In my template file :
Code:
&lt;?php echo $this->session->flashdata('flashmsg'); ?&gt;

Finally, this works fine. It flashes even with redirection... and in the place i want to. I think i do not complety understand the flash functionnality behavior.

The problem seems then to come from the view call within the set_flashdata ... That blocks the flashdata() , and that destroys my page design (the flash outpouts before the doctype declaration).

I'm going to have a look, step by step.
#8

[eluser]gryzzly[/eluser]
Code:
function set_flashdata($newdata = array(), $newval = '')
    {
        if (is_string($newdata))
        {
            $newdata = array($newdata => $newval);
        }

        if (count($newdata) > 0)
        {
            foreach ($newdata as $key => $val)
            {
                $flashdata_key = $this->flashdata_key.':new:'.$key;
                $this->set_userdata($flashdata_key, $val);
            }
        }
    }

$newval should be a string :'(
Could not be a method() call .... here is it.

3 hours lost on that stupid thing.

At least, i now know that i can not use flashdata as i wanted to :'( BAD !
#9

[eluser]jedd[/eluser]
Howdi. Not sure what the exact nature of the problem you had in that last message - perhaps you could elucidate on what you were trying to do, functionality-wise as well as the details on how you were trying to do it.

(Speculating wildly - you might want to look at the standard PHP functions - serialize and unserialize (?) - as ways of converting arrays into strings, and back again. I'm not sure how much space you get with flashdata - if it's subject to the 4KB limit - but I'm digressing now.)


Note that:

Quote:Ok, thanks for advices. But for this project, i want/need to use core functionnalities when they exist, as most as possible.

I was referring to CI Session data - see elsewhere on the [url="http://ellislab.com/codeigniter/user-guide/libraries/sessions.html"]Session[/url] page - which might be more useful for what you're trying to do.


Quote:In my template file :
Code:
&lt;?php echo $this->session->flashdata('flashmsg'); ?&gt;

With flashdata, you might be better off assigning it to a variable in your controller, and passing that variable to your view.
#10

[eluser]gryzzly[/eluser]
Quote:With flashdata, you might be better off assigning it to a variable in your controller, and passing that variable to your view.

I don't think it is a good solution, since assigning it to a variable, would force me to check in my template if the variable is assigned or not. A very small loop --&gt; if(isset($bob)){echo $bob;} <-- but unnecessary, since the flashdata key does not require to really exist (if the key:flashmsg is not assigned/present in userdata session, flashdata just not shows it, and does not deletes it from session). If it exists in session, it flashes and deletes. If the value is not assigned, it just waits we pass data to it to play it's role.

I'm not sure to be easyly understandable (english is not my mother tong, and i do not have always the right words) ...
---
At least, I get it working:

http://ellislab.com/codeigniter/user-gui...views.html
Quote:Returning views as data

There is a third optional parameter lets you change the behavior of the function so that it returns data as a string rather than sending it to your browser. This can be useful if you want to process the data in some way. If you set the parameter to true (boolean) it will return data.

Then in controller :
Code:
// Set a flash message
$flashmsg['result'] = "Item created.";
$this->session->set_flashdata('flashmsg', $this->load->view('flashmsg/success', $flashmsg, TRUE)); // TRUE is the thing !! it makes my view singular datas :o)

// Redirects to index
redirect('categories');

In my template view :
Code:
&lt;?php echo $this->session->flashdata('flashmsg'); ?&gt;

Finally, in the /view/flashmsg/success.php file :
Code:
<p class="flashmsg success">&lt;?php echo $result; ?&gt;</p>

Finally i'll probably make a small function to pass automatically the good message to the choosen message-type view.

The interest of this method, is that i can send notices, errors, success, etc. with any message i want and in a custom design :o) Perfect for informations (update succes, failure, etc....) to give to the user after he mades any action !


PS : is there a way to "close"/"mark resolved" any message on this forum ?




Theme © iAndrew 2016 - Forum software by © MyBB