Welcome Guest, Not a member yet? Register   Sign In
A little help with a function to display a message popup.
#1

[eluser]Samuurai[/eluser]
Hi,

I'm getting to know codeigniter fairly well, but I've just come to my first requirement to create a function which will be available globally.

I have a placeholder in my header which displays a message if the $message variable is set. Messages will be things like "Successfully logged in" etc.

I am using redirects so I'm going to use the flashdata in some cases, but I want to also use normal $data->message = "blah" then load the view, passing $data.

So far my code is
Code:
$messages = array();
if(isset($message))
{
array_push($messages, $message);
}
if($this->session->flashdata('message'))
{
array_push($messages, $this->session->flashdata('message'))
}

I then do a foreach in the header view and display these messages.

Can anyone see a more optimised way of doing this? And where can I put this code so I can just call $this->messages from the view? (Haven't fully got my head around this class structure yet.

Thanks!

B
#2

[eluser]jedd[/eluser]
[quote author="Samuurai" date="1253546318"]
I am using redirects so I'm going to use the flashdata in some cases, but I want to also use normal $data->message = "blah" then load the view, passing $data.
[/quote]

If you're using redirects, you have to sustain your flashdata through the jumps. Why not just use normal session data?

I put my 'make a little one of four-coloured alert/info boxes' function in my helper (and I autoload my helper). This calls returns a string of HTML, and does that via a view partial.

I'm not entirely convinced it's the best way of doing it.

I also suggest you explore the wiki and the forum and the user guide for the string MY_Controller - it'll throw up a gazillion (the UK, not the US figure - that'd just be silly) results and should give you some good pointers - you could conceivably locate this functionality entirely in that construct.

It'd certainly go some way to satisfying your ultimate requirement - though you don't want to call '$this->anything' from your view, but rather your controller.
#3

[eluser]BrianDHall[/eluser]
I would suggest building your message functions into a library which will itself keep track of messages using sessions. I generally dislike flash data, as I believe it is better to set it normally and then check for its existence and if unneeded kill it, or if used then use it and then kill it.

Then you load your message library, or auto-load it, and refer to it like:

Code:
$this->Message->add_message('login', "Login successful!", 'optional boolean flag - if set will be unset after printing');
//$this->Message->echo_message('login);
$this->Message->echo_messages();
#4

[eluser]Samuurai[/eluser]
[quote author="BrianDHall" date="1253560591"]I generally dislike flash data, as I believe it is better to set it normally and then check for its existence and if unneeded kill it, or if used then use it and then kill it.[/quote]

Thanks for the reply...

How else can you carry the data over if you're redirecting?
#5

[eluser]BrianDHall[/eluser]
[quote author="Samuurai" date="1253560861"][quote author="BrianDHall" date="1253560591"]I generally dislike flash data, as I believe it is better to set it normally and then check for its existence and if unneeded kill it, or if used then use it and then kill it.[/quote]

Thanks for the reply...

How else can you carry the data over if you're redirecting?[/quote]

Oh, just regular sessions - 'flash' data is only available on the next access of a page, so as jedd mentions it will not be available on the page the person gets redirected to
#6

[eluser]Samuurai[/eluser]
Oh.. I thought flashdata was just a function within CI which made it easy to set and unset some userdata? What's the difference between manually setting some userdata, getting the data, then unsetting it and just using flashdata?
#7

[eluser]BrianDHall[/eluser]
Flashdata is a function in CI where data set is only available on the next page a user goes to, so in CI if a flash variable was set to start with on a page it is killed once all processing is done.

The difference is what if you want to redirect a user, or maybe have a user do something first (like change their password, or agree to changed licensing/user agreement, etc) before continuing on to their previously requested page?

Manually killing when you are done with it ensures the data lives until you don't want it - flashdata requires that info ONLY be useful on the next access of the page.
#8

[eluser]kurucu[/eluser]
CI drops the flash data at the end of the next request for you, it keeps user data until the end of the session.
#9

[eluser]Samuurai[/eluser]
[quote author="kurucu" date="1253565281"]CI drops the flash data at the end of the next request for you, it keeps user data until the end of the session.[/quote]

Flashdata sounds perfect then ! I don't need the messages like "Thanks for registering" to clogging up my userdata.
#10

[eluser]jedd[/eluser]
I feel we are going in circles here, and that perhaps you're not aware that there's a user manual for CodeIgniter (handy for explaining differences between flash and non-flash session data, f.e.)

The reason you might not use flash (as I mentioned way back when) was because you need to refresh it on every pass-through a redirect page. This can be tiresome.

If you are committed to catching the data and displaying a pop-up based on the contents of same, then normal session data makes as much sense.

If you tend to write bodgy code which would result in you not catching these 'notes to yourself' that you store in session date, then you would indeed be better off using flash-data, as it'll save you from yourself (or, as you put it, not clog up your userdata).




Theme © iAndrew 2016 - Forum software by © MyBB