1

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.

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Awesome.Thanks for setting me straight.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.