2

I am extending form_helper that will populate data from an array in view.

E.g:

//Controller - user_controller.php
User_Controller extends CI_Controller{
    function edit(){
        $data['record'] = array('username'=>'robert','email'=>'[email protected]');
        $this->load->view('edit',$data);
    }
}


//View - edit.php
<?= $record['username']; ?> >> 'robert'
<?= simple_input('halo'); ?>

//Helper - MY_form_helper.php
function simple_input($name){
    var_dump($record); >> Undefined variable: record
    return "<input type='text'/>";
}

I thought helper should load up the variables from view. Didn't really understand how it works. How can I access the view variables from helper?

3 Answers 3

1

Try passing the variable in the function:

//...

//View - edit.php
<?= $record['username']; ?> >> 'robert'
<?= simple_input('halo', $record); ?>


//Helper - MY_form_helper.php
function simple_input($name, $record){
    var_dump($record);
    return "<input type='text'/>";
}
Sign up to request clarification or add additional context in comments.

Comments

0

helper is function, so you need to pass the var into the function to use it. (tttony wrote about this.)
I think you'd better make another view. in that case you don't need to pass the vars.

//View - edit.php
<?= $record['username']; ?> >> 'robert'
<?= $this->load->view('simple_input'); ?>

//View simple_input.php
var_dump($record);
echo "<input type='text'/>";

Comments

0

helper is function, so you need to pass the var into the function to use it. (tttony wrote about this.) I think you'd better make another view. in that case you don't need to pass the vars.

//View - edit.php
<?= $record['username']; ?> >> 'robert'
<?= $this->load->view('simple_input'); ?>

//View simple_input.php
var_dump($record);
echo "<input type='text'/>";

Comments

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.