Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

code igniter question: problem with passing variable to a view

I am a newbie with CodeIgniter (2.03), and I have the following problem:

Here is my main template (view):

<?php $this->load->view('backOffice/bo_header_in'); ?>

<?php $this->load->view($bo_main_content); ?>

<?php $this->load->view('backOffice/bo_footer_in'); ?>

Here is my model:

<?php

class Back_office_users extends CI_Model 
{

  public function getAllUsers () 
  {
    $query = $this->db->query("SELECT * FROM users");

    if ($query->num_rows() > 0) {
      foreach ($query->result() as $rows) {
        $users[] = $rows;
      }
      return $users; 
    }
  }
}  

And here is my controler:

<?php

class Dashboard extends CI_Controller 
{

  public function __construct() 
  {
    parent::__construct();
    $this->is_logged_in();
  }

  public function index () 
  {   
    $this->load->model('back_office_users');
    $users['rows'] = $this->back_office_users->getAllUsers();

    $data['bo_main_content'] = "backOffice/dashboard";

    $this->load->view('backOffice/bo_template_in', $data, $users);

   // if I pass the variable like this it works just fine...
   //$this->load->view('backOffice/users', $users);
  }

  public function is_logged_in()
  {
    $is_logged_in = $this->session->userdata('is_logged_in');
    if (!isset($is_logged_in) || ($is_logged_in != true)) {
      $this->accessdenied();   
    }
  }

  public function accessdenied () 
  {    
    $data['bo_main_content'] = 'backOffice/accessdenied';
    $this->load->view('backOffice/bo_template', $data);
  }

  public function logout () 
  {    
    $this->session->sess_destroy();
    redirect('backOffice/index');
  }
}  

And the dashboard view is like this:

<?php
  print_r($users);
?> 

I am getting the following error:

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: users

Filename: backOffice/dashboard.php

Line Number: 9

Can anyone shed some light how can I resolve this? I create another view without use of the template, and it print the array.

like image 811
Zoran Avatar asked Dec 06 '25 06:12

Zoran


1 Answers

You're not passing the $users variable to the second (nested) view.

I'd suggest adding $users to the $data array, and then in the first view pass the $users array to the embedded view. So, in your controller:

public function index () {

  /* stuff... */

  $data['users']['rows'] = $this->back_office_users->getAllUsers();

  $data['bo_main_content'] = "backOffice/dashboard";

  /* stuff... */

  $this->load->view('backOffice/bo_template_in', $data);
}

Then in the main view:

<?php $this->load->view($bo_main_content, $users); ?>

Then in the dashboard view:

<?php
  print_r($rows);
?>

This is because in the main view, as you know, CodeIgniter transforms all elements of $data in to variables, so we'll end up with the $users variables. $users is an array containing rows, so when we pass $users to the second view, the second view transforms all elements of $users in to view variables, hence we now have access to $row.

like image 143
Alex Avatar answered Dec 08 '25 23:12

Alex