Passing variables |
[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() { model: user_management.php Code: //FETCH ACTIVE USERS - START view: active_users.php Code: <div id="content"> Help over here..
[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.
[eluser]ninjayan[/eluser]
I do this in controller Code: echo $data['users']; and returns array
[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';"
[eluser]ninjayan[/eluser]
Can I do it like this? Code: $this->load->view(‘includes/template’, $data, $variable);
[eluser]ninjayan[/eluser]
Here's the controller: Code: public function __construct() { model Code: public function active() { view Code: <?php
[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"; 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
[eluser]beeLoop[/eluser]
[quote author="ninjayan" date="1350045825"]Here's the controller: Code: public function __construct() { model Code: public function active() { view Code: <?php Yes this will give you the exact what you want.
[eluser]ninjayan[/eluser]
Can't find the solution. This is the updated model Code: public function active() { View: shows this error message A PHP Error was encountered Severity: Notice Message: Undefined index: username Filename: views/active_users.php
[eluser]p. z.[/eluser]
change in the model Code: ... |
Welcome Guest, Not a member yet? Register Sign In |