CodeIgniter Forums

Full Version: Notice for session! if($this->session->userdata['logged_data']['data_error_message']==TRUE)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]behnampmdg3[/eluser]
Hi;

How can I make this code smaller without getting notices?

Thanks

Code:
if(isset($this->session->userdata['logged_data']['data_error_message']))
    {
    if($this->session->userdata['logged_data']['data_error_message']==TRUE)
               {
                   $session_data = $this->session->userdata('logged_data');
                   $data['details_error'] = "<div class = 'alert alert-error'>Please fill the form with correct information and re submit</div>";
                   $session_data['data_error_message'] = FALSE;
                   $this->session->set_userdata('logged_data', $session_data);
               }
             else
              {
               $data['details_error'] = "";
              }        
             }  
            else
             {
              $data['details_error'] = "";
             }

El Forum

[eluser]kamikaz[/eluser]
Use
Code:
$this->session->userdata('logged_data') != FALSE

El Forum

[eluser]stevezissou[/eluser]
[quote author="kamikaz" date="1394784787"]Use
Code:
$this->session->userdata('logged_data') != FALSE
[/quote]

No, first off it would be shorter to write:

Code:
!$this->session->userdata('logged_data');

But that is still wrong. If the value stored in $this->session->userdata('logged_data') is 0, that will still evaluate as false. 0 may be a perfectly valid return value. What you want is:

Code:
$this->session->userdata('logged_data') !== false

Second, lots of if/else statements are code smells. They are usually quite easy to clean up though. Just keep working at it.

El Forum

[eluser]boltsabre[/eluser]
Didn't actually look at your code to see if it is valid, but this is one way of reducing those nested if/else statements and making that code block about 50% smaller:

Code:
$data['details_error'] = "";
if( isset($this->session->userdata['logged_data']['data_error_message']) &&
   ($this->session->userdata['logged_data']['data_error_message']==TRUE )
{
   $session_data = $this->session->userdata('logged_data');
   $data['details_error'] = "<div class = 'alert alert-error'>Please fill the form with correct information and re submit</div>";
   $session_data['data_error_message'] = FALSE;
   $this->session->set_userdata('logged_data', $session_data);
}

Oh wait, I just had a look at your code, can't test this, I'm at work without a CI install, but this should work! Just assign your "data_error_message" straight in, no need to extract userdata('logged_data') first, manipulate it and then assign it back in.
Code:
$data['details_error'] = "";
if( isset($this->session->userdata['logged_data']['data_error_message']) &&
   ($this->session->userdata['logged_data']['data_error_message']==TRUE )
{
   $data['details_error'] = "<div class = 'alert alert-error'>Please fill the form with correct information and re submit</div>";
   $this->session->set_userdata("logged_data['data_error_message']", false);
}
7 lines of code instead of 18, much much better :-)