IF会话数据存在,输出在视图中(IF Session data exists, output in view)

编程入门 行业动态 更新时间:2024-10-28 10:25:00
IF会话数据存在,输出在视图中(IF Session data exists, output in view)

所以我试图将一个会话的变量值输出到视图中(只有当它存在时)。 但我似乎无法让它工作。 没有错误。 没有。 我错过了什么? 以为我现在已经把这个打包下来了 - 不要猜测......

在控制器... FYI中,$ data被分配给视图(即$this -> load -> view('view', $data);

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

这是我在试图输出的视图中的php代码片段。 总之,如果会话存在,输出它。 如果没有,什么都不要做。

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

任何人?

编辑好吧,我应该提到,我试图输出到FORM值的会话值。 上面修改了视图代码。 表单提交就像价值在那里 - 甚至发送价值。 但是,它在文本输入中不可见...

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

最满意答案

你可以轻易地在你看来这样做:

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

那么如果你想让会话数据作为输入的值,可以这样做:

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

不需要控制会话userdata是否存在,如果它不存在就不会打印任何内容,导致userdata()方法返回false !

然后,您不需要通过$data[] array传递会话数据,因此可以从任何地方(模型/控制器/视图/钩子/库/帮助器等)检索会话数据。

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)

更多推荐

本文发布于:2023-07-07 15:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1065185.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   数据   Session   data   output

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!