Error in passing value to View in codeigniter

Asked

Viewed 413 times

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:

inserir a descrição da imagem aqui

1 answer

4


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

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

In the view call variables by the Indice set in the controller:

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

Recommended reading:

CI Documentation - View

  • 1

    in addition, the documentation in English may help, https://cibr.github.io/User-Guide-CodeIgniter-PtBr/general/views.html#Adding-Dynamic-data-to-the-view

Browser other questions tagged

You are not signed in. Login or sign up in order to post.