如果会话数据存在,则在视图中输出 [英] 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...
在控制器中...仅供参考,$ data已分配给视图(即 $ this->加载-> view('view',$ data);
In controller...FYI, $data is assigned to the view (i.e. $this -> load -> view('view', $data);
$data['campaign_name'] = $this -> session -> userdata('campaign_name');
以下是我要输出的php代码段。因此,简而言之,如果该会话存在,则将其输出。否则,不执行任何操作。
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)) ;?> ">
有人吗?
编辑好吧,我应该提到我正在尝试将会话值输出为FORM值。上面修改了视图代码。表单提交的好像是值存在-甚至将值一起发送。它在文本输入中不可见...
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...
推荐答案
您可以轻松地执行此操作在您的视图中:
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') ?>">
您无需控制会话用户数据是否存在,原因是不存在,它不打印任何内容,导致 userdata()
方法返回 false
!
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
!
然后,您无需通过 $ data []数组
传递会话数据,因为可以从任何地方检索会话数据(模型/控制器/视图/挂钩/库/帮助器等)
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)
这篇关于如果会话数据存在,则在视图中输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!