开发者

CodeIgniter: Accessing view variables from helper

开发者 https://www.devze.com 2023-04-08 04:13 出处:网络
I am extending form_helper that will populate data from an array in view. E.g: //Controller - user_controller.php

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'=>'simplerobert@google.com');
        $this->load->view('edit',$data);
    }
}


//View - edit.php
<?= $record['username']; ?> >> 'robert'开发者_Python百科
<?= 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?


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'/>";
}


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'/>";


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'/>";
0

精彩评论

暂无评论...
验证码 换一张
取 消