Mike Barwick
Mike Barwick

Reputation: 5367

IF Session data exists, output in view

So I'm trying to output a session's variable value into a view (only if it exists). But I can't seem to get it to work. No error. Nothing. What am I missing? Thought I had this down packed by now - guess not...

In controller...FYI, $data is assigned to the view (i.e. $this -> load -> view('view', $data);

$data['campaign_name'] = $this -> session -> userdata('campaign_name');

Here's my php snippet in the view that I'm trying to output. So in short, if the session exists, output it. If not, do nothing.

<input type="text" name="campaign_name" class="wizardInput nameField" value="<? if (isset($campaign_name)) ;?> ">

Anyone?

EDIT Okay, I should have mentioned that i'm trying to output the session value into a FORM value. Modified view code above. The form submits as though the value is there - and even sends the value along. However, it's not visible in the text input...

Upvotes: 3

Views: 9848

Answers (1)

Filippo oretti
Filippo oretti

Reputation: 49813

you can easly do this in your view:

if($this->session->userdata('campaign_name')){
 // do somenthing cause it exist
}

then if you want to make session data as the value of an input do this:

<input type="text" name="campaign_name" class="wizardInput nameField" value="<?php echo $this->session->userdata('campaign_name') ?>">

you don't need to control if session userdata exist cause if it not exist it doesn't prints anything, cause userdata() method returns false !

Then you don't need to pass session data trough the $data[] array, cause session data can be retrieved from anywhere (model/controllers/views/hooks/libraries/helpers and so on)

Upvotes: 8

Related Questions