2

I am trying to pass a variable from a controller to a view. I have some code but in order to understand what is the problem I made it simple. Here is my controller:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    class Welcome extends CI_Controller {

        $p=2;

        public function index()
        {
            $this->load->view('welcome_message',$p);
        }
    }

?>

Variable p is declared in the view.

<div id="container">
    <h1>Welcome to CodeIgniter!</h1>
    <?php echo $p?>
</div>

When I try to display the $p value, I obtained the error:

ERROR

Parse error: syntax error, unexpected '$p' (T_VARIABLE), expecting function (T_FUNCTION) in C:\wamp\www\..\application\controllers\welcome.php on line 20

What's wrong?

Thanks.

2 Answers 2

3

First of variables need to be passed as an array (check out the docs).

$data = array(
               'title' => 'My Title',
               'heading' => 'My Heading',
               'message' => 'My Message'
          );

$this->load->view('welcome_message', $data);

$p has been declared out of the scope of the function, so either;

public function index() {
   $p = 2;
   $this->load->view('welcome_message',array('p' => $p));
}

or

class Welcome extends CI_Controller {

public $p=2;

public function index()
{
    $this->load->view('welcome_message',array('p' => $this->p));
}
}
Sign up to request clarification or add additional context in comments.

6 Comments

Yes. it works now. But when I changed in my real code I have another error: A PHP Error was encountered Severity: Notice Message: Undefined property: CI_Loader::$arrPermisos Filename: marketing/campanas.php Line Number: 6 The controller code: public function index() { $this->load->view("marketing/campanas",$this->arrPermisos); } The view code: <ul class="menuArea"> <?php if ($this->arrPermisos["campanas_leer"]) { ?> <li class="current"><a href="/comercial/campanas/">Campañas</a></li> <?php } ?> </ul> What's wrong?
@axmug is $this->arrPermisos a hash array?
Yes. It is an array where roles users are saved.
when referencing in the view code you need to use the key of the array element as the variable. E.g in controller array('foo' => 1) then when passed into your view access it by $foo
Yes. I tried it but it doesn't work. And I don't know why. If I type: <?php if ($arrPermisos["campanas_leer"]) { ?> <li class="current"><a href="/comercial/campanas/">Campañas</a></li> <?php } the error is undefined variable arrPermisos.
|
0

You should declare $pin your controller's constructor:

class Welcome extends CI_Controller {

    function __construct() {
    parent::__construct();
        $this->p = 2;
    }

    public function index()
    {
        $data['p'] = $this->p;
        $this->load->view('welcome_message',$data);
    }
}

?>

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.