php - how to check session flashdata in helper in CI

686

To check and display a session, I usually do this:

    $alert = $this->session->flashdata('alert');

    if($alert)
    {
      ?>
      <div role="alert">
        <button type="button" data-dismiss="alert"><span aria-hidden="true">&times;</span><span >Close</span></button>
        <?php echo $var; ?>
      </div> <?php
    }

But the result is sloppy. I want to make a "ECHO" of a function and that function contains everything you need to check and display the session. So I try to do this in form helper:

function errors_session()
{
   $ci =& get_instance();

   if( $this->ci->session->flashdata() == TRUE )
   {
      return '<div class="error">'. $this->ci->session->flashdata() .'</div>';
   }

   return '';
}

but this not work

129

Answer

Solution:

in Controller file:

$this->session->set_flashdata('Success', 'You are Successfully Logged.'); 

in View file:

 if($this->session->flashdata('Success')){
  //your content
}

or

echo $this->session->flashdata('Success');
771

Answer

Solution:

Ah should have spotted that. Its as the error says- you can't use $this you need to change to use the $ci object you have created like this

$ci->session->flashdata()

People are also looking for solutions to the problem: Create non-existing array by accessing it in PHP?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.