Welcome Guest, Not a member yet? Register   Sign In
Passing variables
#1

[eluser]ninjayan[/eluser]
Hi everyone! I have a controller, model and view and want to echo all active users fetch from the model. My problem is how to properly pass the data/variables from model->controller then controller->view.
Here's the code.

controller: users.php
Code:
public function __construct() {
  parent:: __construct();
  $this->load->model('user_management'); //GETTING ACTIVE, PENDING, DISABLED USERS
  $this->logged_in();
  $this->clear_cache();
}

public function active_users() {
  if ($this->session->userdata('is_logged_in')) {
   $data['users'] = $this->user_management->active();
   $data['bb'] = "abc";

   //VIEW PAGE
   $data['main_content'] = 'active_users';
   $this->load->view('includes/template', $data);
  }else {
   redirect(base_url());
  }
}

model: user_management.php
Code:
//FETCH ACTIVE USERS - START
public function active() {
  $this->db->where('status', 'Pending');
  $query = $this->db->get('users');

  if ($query) {
   foreach($query as $row) {
    $active_users[] = $row;
   }
  }
  $query->free_result();
  $data = $active_users;
  return $data;
}
//FETCH ACTIVE USERS - END

view: active_users.php
Code:
<div id="content">
    &lt;?php
    var_dump($users);
    ?&gt;
   </div>

Help over here..
#2

[eluser]beeLoop[/eluser]
Lets Debug your problem.
In your controller, First check the value of $data['users'].
If you are getting the correct result, then next step.

In views includes/template, it is assumed that you have included the main_content variable.
As in the controller you are passing the value as $data['main_content'] = 'active_users';, so echo the $main_content variable in includes/template view.

That's it for now.
Let me know what value of $data['users'] you are getting in controller.


#3

[eluser]ninjayan[/eluser]
I do this in controller
Code:
echo $data['users'];

and returns array
#4

[eluser]beeLoop[/eluser]
If it is returning array, then your model is returning some data.
For more you can var_dump the data getting from model.

I guess your problem lies in piece of code.
//VIEW PAGE
$data['main_content'] = 'active_users';
$this->load->view('includes/template', $data);

Why don't you include "active_users.php" in your "includes/template" view.

and don't include it in controller like
"$data['main_content'] = 'active_users';"



#5

[eluser]ninjayan[/eluser]
Can I do it like this?

Code:
$this->load->view(‘includes/template’, $data, $variable);
#6

[eluser]ninjayan[/eluser]
Here's the controller:
Code:
public function __construct() {
  parent:: __construct();
  $this->load->model('user_management'); //GETTING ACTIVE, PENDING, DISABLED USERS
  $this->logged_in();
  $this->clear_cache();
}

public function active_users() {
  if ($this->session->userdata('is_logged_in')) {
   $data['users'] = $this->user_management->active();
   //VIEW PAGE
   $this->load->view('active_users', $data);
  }else {
   redirect(base_url());
  }
}

model
Code:
public function active() {
  $this->db->where('status', 'Pending');
  $query = $this->db->get('users');

  $active_users = array();

  if ($query) {
   foreach($query as $row) {
    $active_users[] = $row;
   }
  }
  $query->free_result();
  $data = $active_users;
  return $data;
}

view
Code:
&lt;?php
    foreach ($users as $user) {
     echo $user['username'];
    }
    ?&gt;
#7

[eluser]Unknown[/eluser]
No you can't. The third variable must be a boolean, and if it is set to true, the function call will not print the view to the output, but instead will return it as data (=string).

The second parameter is "data", an associative array. Each member of this array can be used as PHP variables in your view.

So, for example

In your controller:

Code:
$data["CoolString"] = "This is a very cool string";
$this->load->view('my_view',$data);

In your view:

Code:
echo $CoolString; //it is case sensitive!

Don't forget to review the official documentation on loading views:

http://ellislab.com/codeigniter/user-gui...views.html

#8

[eluser]beeLoop[/eluser]
[quote author="ninjayan" date="1350045825"]Here's the controller:
Code:
public function __construct() {
  parent:: __construct();
  $this->load->model('user_management'); //GETTING ACTIVE, PENDING, DISABLED USERS
  $this->logged_in();
  $this->clear_cache();
}

public function active_users() {
  if ($this->session->userdata('is_logged_in')) {
   $data['users'] = $this->user_management->active();
   //VIEW PAGE
   $this->load->view('active_users', $data);
  }else {
   redirect(base_url());
  }
}

model
Code:
public function active() {
  $this->db->where('status', 'Pending');
  $query = $this->db->get('users');

  $active_users = array();

  if ($query) {
   foreach($query as $row) {
    $active_users[] = $row;
   }
  }
  $query->free_result();
  $data = $active_users;
  return $data;
}

view
Code:
&lt;?php
    foreach ($users as $user) {
     echo $user['username'];
    }
    ?&gt;
[/quote]


Yes this will give you the exact what you want.
Smile
#9

[eluser]ninjayan[/eluser]
Can't find the solution.

This is the updated model
Code:
public function active() {
  $this->db->select('username', 'salutation', 'first_name', 'middle_initial', 'last_name', 'office', 'email', 'privilege', 'status', 'datetime_registered');
  $this->db->where('status', 'Pending');
  $query = $this->db->get('users');

  $active_users = array();

  if ($query) {
   foreach($query as $row) {
    $active_users[] = $row;
   }
  }
  $query->free_result();
  $data = $active_users;
  return $data;
}

View: shows this error message
A PHP Error was encountered

Severity: Notice

Message: Undefined index: username

Filename: views/active_users.php

#10

[eluser]p. z.[/eluser]
change in the model
Code:
...

if ($query->num_rows()) {
   foreach($query->result_array() as $row) {
    $active_users[] = $row;
   }
...




Theme © iAndrew 2016 - Forum software by © MyBB