简体   繁体   English

CI:未定义变量

[英]CI: Undefined Variable

I know similiar questions have been asked, but I've still been unable to resolve my issue. 我知道有人问过类似的问题,但仍然无法解决我的问题。 When my view loads, I get the error below, and I know there is a value in $id. 加载视图时,出现以下错误,并且我知道$ id中有一个值。

Message: Undefined variable: id Filename: admin/upload_form.php Line Number: 17 消息:未定义的变量:id文件名:admin / upload_form.php行号:17

Controller: 控制器:

public function getEventNameById( $id ) {
            $q = $this->event_model->getEventNameById( $id );            
            echo "Event Name: ".$q."</p>";
            $data['id'] = $id;            
            $this->load->view('admin/upload_form',array('error' => ' ' ), $data);
}

View: 视图:

<body>
<div>
<?php echo $error;?>
<p>Event Image:</p>
<?php echo form_open_multipart('admin_upload/do_upload');?>

<input type="file" name="userfile" size="20" />
<br /><br />
<input type="hidden" id="iEventID" name="iEventID" value="<?php echo $id;?>" />
<input type="submit" value="upload event image" /> <input type="button" value="close" 
onclick="window.close()">

</form>
</div>
</body>

What I need to be able to do, is query information based on an ID, then pass that ID to my view, which will then pass the ID back to a different controller function for use in updating a record in my database. 我需要做的是基于ID的查询信息,然后将该ID传递给我的视图,然后将其传递回另一个控制器功能,以用于更新数据库中的记录。 In other words I need to persist the ID throughout. 换句话说,我需要始终保留该ID。

Any and all assistance is greatly appreciated. 任何帮助都将不胜感激。

You're passing the view data incorrectly: 您错误地传递了视图数据:

Your code passes 3 parameters: 您的代码传递了3个参数:

$this->load->view('admin/upload_form', array('error' => ' ' ), $data);
//                |        1         |            2          |   3   |

You should pass all data to the second parameter: 您应该将所有数据传递给第二个参数:

$this->load->view('admin/upload_form', array('error' => ' ' ) + $data);
//                |        1         |                  2             |

The third param is supposed to be a boolean, whether or not to print the data directly (default false ) or store it in a variable ( true ). 第三个参数应该是布尔值,是直接打印数据(默认为false )还是将其存储在变量中( true )。

I just used the + operator to combine the arrays, but it probably would be cleaner to use this: 我只是用+运算符组合了数组,但是使用它可能会更干净:

$data['error'] = ''; // Not sure why this is needed, but I assume it is
$data['id'] = $id;   
$this->load->view('admin/upload_form', $data);

What happened was your $data array wasn't getting passed to the view at all, hence the undefined variable. 发生的是您的$data数组根本没有传递给视图,因此未定义变量。

Reference: http://codeigniter.com/user_guide/libraries/loader.html 参考: http : //codeigniter.com/user_guide/libraries/loader.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM