IF Session data exists, output in view

六眼飞鱼酱① 提交于 2019-12-23 15:39:34

问题


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...


回答1:


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)



来源:https://stackoverflow.com/questions/13478836/if-session-data-exists-output-in-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!