简体   繁体   中英

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.

Message: Undefined variable: id Filename: admin/upload_form.php Line Number: 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. In other words I need to persist the ID throughout.

Any and all assistance is greatly appreciated.

You're passing the view data incorrectly:

Your code passes 3 parameters:

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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