Welcome Guest, Not a member yet? Register   Sign In
Flashdata boolean gets converted between requests
#1

[eluser]mawe[/eluser]
Hi all!

The topic pretty much says it all. I have this in a controller:
Code:
function delete($news_id)
{
    if ($this->News_Model->delete($news_id) === TRUE)
    {
        $this->session->set_flashdata('deleted', TRUE);
    }
    else
    {
        $this->session->set_flashdata('deleted', FALSE);
    }

    redirect(base_url().'admin/news/archive', 'location');
}

In the archive view I had this:
Code:
<?php if ($this->session->flashdata('deleted')): ?>

<?php if ($this->session->flashdata('deleted') === TRUE): ?>
<p class="success">News gelöscht!</p>
&lt;?php else: ?&gt;
<p class="error">News konnte nicht gelöscht werden!</p>
&lt;?php endif; ?&gt;

&lt;?php endif; ?&gt;

The inner if (the one that checks for TRUE) doesn't work. I examined the session and the flashdata item is actually '1', a string, not boolean TRUE.

Is this expected behaviour?
#2

[eluser]Dam1an[/eluser]
I think this is expected
When the session data is stored, its serialized, so it all becomes a long String
When it gets deserialized at the other end, I'm asuming it was stored as '1', so it probably stays as that
#3

[eluser]mawe[/eluser]
Thanks for the answer, but this is only correct for the first part.
Just tested this on my machine. Though it really gets serialized to a string, unserialize handles data types properly:
Code:
C:\Users\MatiPati\Desktop>type test.php
&lt;?php

$a = serialize(TRUE);
var_dump($a);
var_dump(unserialize($a));

?&gt;

C:\Users\MatiPati\Desktop>php -f test.php
string(4) "b:1;"
bool(true)
#4

[eluser]benoa[/eluser]
What if you do it this way?

Code:
function delete($news_id)
{
    if ($this->News_Model->delete($news_id) === TRUE)
    {
        $this->session->set_flashdata('deleted', 1);
    }
    else
    {
        $this->session->set_flashdata('deleted', 0);
    }

    redirect(base_url().'admin/news/archive', 'location');
}

And in the view:

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

&lt;?php if ($this->session->flashdata('deleted')): ?&gt;
<p class="success">News gelöscht!</p>
&lt;?php else: ?&gt;
<p class="error">News konnte nicht gelöscht werden!</p>
&lt;?php endif; ?&gt;

&lt;?php endif; ?&gt;

Maybe the synthax is broken somewhere (no code highlighting in the reply textarea)... But I hope this can help!




Theme © iAndrew 2016 - Forum software by © MyBB