How to pass a controller variable to view?

Asked

Viewed 1,102 times

1

I need to pass the variable $limit to my view and display it there, how could I do it?

public function index() {
    // ....
    $this->pagination->initialize($config);
    $data['pagination'] = $this->pagination->create_links();
    $limit = $config['per_page'];
    $offset = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0; 
    $where = "id IS NOT NULL";
    $servidores = $this->server->listarServidores($this->select, $where, $limit, $offset);
    $data['servidores'] = $servidores;
    $this->template->load('template_view', 'home/home_view', $data);

}
  • 1

    The data in the variable $data are no longer passed to the view? If yes, simply add the value of $limit in $data.

  • Yes, I thought the $data array was just for passing bank results, it worked, thank you!

  • I put the solution in response form, so I can flag the topic as solved.

1 answer

2


As defined in documentation of the framework, the load, to carry views and templates accepts a parameter of type array associative with values that will be present in the view/template.

Since in the presented code is already made use of this function through the variable $data, just add the value of $limit in this array, as subsequent:

$data["limit"] = $limit;

Thus, the value of $limit will also be accessible in the view.

  • That’s right haha, thanks anyway!

Browser other questions tagged

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