Error in passing value to View in codeigniter

1

In a simple application. A value is passed to view. However codeigniter reports an error:

PHP Code:     

class User extends CI_Controller {

    public function __construct(){

            parent::__construct();
                $this->load->helper('url');
                $this->load->model('user_model');
                $this->load->library('session');
    }


    public function index()
    {

    }


    public function alterarUsuario(){

      $teste ="2";
      $this->load->view('teste.php', $teste);

    }
}

?>

View Code:

<?php

    $teste = $_REQUEST['teste'];
    echo "Resposta: ".$id;

?>

Error Message:

    
asked by anonymous 09.10.2017 / 22:44

1 answer

3

It is common to pass more than one value to the view, in this case you must register the name that the variable will be accessible in the view its value, this is done through an array or object. That is passed as the second argument to the view() method.

public function alterarUsuario(){
    $params['id'] = 2;
    $params['teste'] = 'algum valor';
    $this->load->view('teste.php', $params);
}

In the view call the variables by the index defined in the controller:

<?php echo "id: ". $id ." valor: ". $teste; ?>

Recommended reading:

CI Documentation - View

    
09.10.2017 / 22:47