Global variable - codeigniter

Asked

Viewed 1,710 times

0

Hello!

I want to display a die($variable) on a footer.

The way it is below, it works and the variable $operador is displayed correctly on all options/screens I click.

<?php $operador = $this->session->userdata('nome'); ?>

<div id="footer" class="span12"> <?php echo date('Y'); ?> | Versão: <?php echo $operador; ?>

But I don’t want to show a $variável of the Second.

Then I created a controller/modal/view called SYSTEM.

In this controller I have the idSistema, nome, data_registro, date_expiracao, licenca, versao.

So I want to show on the footer version.

So I tried it this way:

<div id="footer" class="span12"> <?php echo date('Y'); ?> | Versão: <?php foreach ($results as $r) { echo $r->versao; }?></div>

But this only works if I’m on the option SYSTEM, if I click another option, where I should display the system version, the message is displayed that the Results variable was not defined.

How can I set this variable to global??

Below, part of the function code in the system controller.

$this->data['results'] = $this->sistemas_model->get('sistemas','idSistemas,nome, dt_registro,dt_expiracao,licenca,versao','',$config['per_page'],$this->uri->segment(3));
$this->data['view'] = 'sistemas/sistemas'; $this->load->view('tema/topo',$this->data);

inserir a descrição da imagem aqui

  • I wanted to help you but I didn’t understand it right. I didn’t understand what you mean by global variable. Who has $this->data['Results']? Need to do the foreach? It just doesn’t solve: <div id="footer" class="span12"> <?php echo date('Y'); ?> | Versão: <?php echo $results->versao ?></div>

  • Does not work.. version is coming from array.. For this reason I put the foreach.

  • I want to transform the values of Results on global variables like Session, which I can get the values on any page.

  • what version of codeigniter?

  • The application is in version 2

1 answer

5


You can use the codeigniter native function vars()

You basically need to define which variables you want to make global using

$this->load->vars();

Place this function in the codeigniter "base" class CI_Controller as in the example below;

class CI_Controller {

    public function __construct()
    {
        ...
        $this->load->vars(['name' => 'variabel global']);
        // Dentro da função vars você pode chamar 
        // uma outra função que retorna um array com os dados do sistema
    }

Now just use the variable in the view $name.

  • This would be the easiest way? What would it be like if I created a helper and loaded it into the view?

  • I changed the answer.

Browser other questions tagged

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