issue with controller and view |
[eluser]cupidleomanoj[/eluser]
hi, I am new to CI, I get a error message as "Undefined variable : data". i have pasted my codes, /* * * Controller ( welcome.php) * * * / <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Welcome extends CI_Controller { public function __construct() { parent::__construct(); } public function my_model() { $this->load->model('Firstmodel'); $data['query'] = $this->Firstmodel->tbl_data(); $this->load->view('firstview', $data); } } ?> /* * * Model (firstmodel.php) * * * / <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Firstmodel extends CI_Model { function __construct() { parent::__construct(); } function tbl_data() { $query = $this->db->get('data'); // data is my table name. return $query->result(); } } ?> /* * * View (firstview.php) * * * / <html> <head></head> <body> <?php echo $data; ?> </body> </html> ******************************************** A PHP Error was encountered Severity: Notice Message: Undefined variable: data Filename: views/firstview.php Line Number: 14 ******************************************** kindly please help me... thanks in advance.......
[eluser]danmontgomery[/eluser]
http://ellislab.com/codeigniter/user-gui...views.html Quote:
[eluser]TWP Marketing[/eluser]
cupid, sorry this a long response but I hope it helps you understand... Code: /* * * ******************************************** A PHP Error was encountered Severity: Notice Message: Undefined variable: data Filename: views/firstview.php Line Number: 14 ******************************************** First, please use the code tag to enclose your code, it makes it much easier to read. I have done so in the quote above. The loader class will extract elements of an array when that array is passed as the second parameter, From the User Guide: "The second optional parameter can take an associative array or an object as input, which it runs through the PHP extract function to convert to variables that can be used in your view files. Again, read the Views page to learn how this might be useful." This means that the array: $data will NOT be visible to the view, but the elements of that array will be visible. So your view should reference those elements: Code: /* * * Because you are returning the query result: Code: ... "This function returns the query result as an array of objects, or an empty array on failure. Typically you'll use this in a foreach loop, like this: Code: $query = $this->db->query("YOUR QUERY"); Your view must reference the query result using the correct syntax, so your view file might be something like this: Code: <html>
[eluser]cupidleomanoj[/eluser]
hi TWP, thanks for your reply. I tried with your code. but still I have a doubt. what you have given works even if i delete the php code from view file. ************************* <html> <head></head> <body> <?php foreach ($query as $row) { echo $row->title; echo $row->name; } ?> </body> </html> ************************ since you echo it in the controller file, the values from the DB gets executed in the query and gets printed. can you please explain how to load the view from controller to display the values from the model. please explain with this concept, $this->load->view('viewname'); thanks in advance........
[eluser]TWP Marketing[/eluser]
[quote author="cupidleomanoj" date="1303952841"]hi TWP, thanks for your reply. I tried with your code. since you echo it in the controller file, the values from the DB gets executed in the query and gets printed. can you please explain how to load the view from controller to display the values from the model. please explain with this concept, $this->load->view('viewname'); thanks in advance........[/quote] cupid, There should be no use of "echo" in the controller (did I understand your statement correctly?). when loading a view (from the controller) you want to pass data using the second parameter: Code: ... Code: ... Code: ... |
Welcome Guest, Not a member yet? Register Sign In |