0

I have this in a codeigniter controller:

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

class Test extends CI_Controller {

    public $idioma;

    public function index() {

        parent::__construct();

            // get the browser language.
        $this->idioma = strtolower(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2));

        $data["idioma"] = $this->idioma;
        $this->load->view('inicio', $data);

    }

    public function hello(){
        $data["idioma"] = $this->idioma;
        $this->load->hello('inicio', $data);
    }
}

Inicio view:

 <a href="test/hello">INICIO <?php echo $idioma ?></a>

hello view:

Hello <?php echo $idioma ?>

The inicio view works great, but when the hello view is loaded there's nothing displayed. Any idea why this is not working?

1
  • hello() doesn't have $this->idioma set to anything. Commented Sep 26, 2013 at 19:24

1 Answer 1

3

If you wish to set a class property automatically you would do it in the constructor, not in index(). index() does not run before other methods if they are called directly. In your case I assume you're calling hello via the url as test/hello

class Test extends CI_Controller {

    public $idioma;

    public function __construct(){
        parent::__construct();

            // get the browser language.
        $this->idioma = strtolower(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2));
    }

    public function index() {

        $data["idioma"] = $this->idioma;
        $this->load->view('inicio', $data);

    }

    public function hello(){
        $data["idioma"] = $this->idioma;
        $this->load->hello('inicio', $data);
    }
}
Sign up to request clarification or add additional context in comments.

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.