3

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.

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Alex. Not only the code works but from your answer, I realize my mistake. Thanks for your time and for sharing your valuable knowledge. Regards, Zoreli

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.