[eluser]dgibsonky[/eluser]
Hi folks--please forgive another noob for being dense, but I've read numerous posts here and elsewhere regarding the act of passing data to & from models and still can't make it work. It seems pretty simple, but I'm undoubtedly missing some detail somewhere.
Here's a sample controller (built on the 'welcome' controller for speed's sake):
Code:
<?php
class Welcome extends Controller {
function Welcome()
{
parent::Controller();
}
function index()
{
$this->load->view('welcome_message');
}
function test ()
{
$data['point1'] = "green eggs and ham";
$data['point2'] = "sausage biscuits";
$data['point3'] = "maple & brown sugar oatmeal";
//load model and pass data to it
$this->load->model('test_model');
$this->test_model->testify('$data');
//echo the data returned from the model--point1 should
//have changed
echo "controller data:".'<pre>';
print_r($data);
echo '</pre>';
//pass data to view
$this->load->view('test_view', $data);
}
}
And the model:
Code:
<?php
class Test_model extends Model {
function Test_model()
{
parent::Model();
}
function testify()
{
//change point1 of data
$data['point1'] = "I do not like green eggs and ham!";
//echo data to see if it has the complete array or just
//the new point1
echo "model data:".'<pre>';
print_r($data);
echo '</pre>';
return $data;
}
}
And finally, the view:
Code:
<html>
<head>
<title>Test_view</title>
</head>
<body>
View data:<br/>
<pre>
<?php
//echo data to see if it's the complete array
print_r($data); ?>
</pre>
</body>
</html>
When I load welcome/test in the browser, I get the following:
Quote:model data:
Array
(
[point1] => I do not like green eggs and ham!
)
controller data:
Array
(
[point1] => green eggs and ham
[point2] => sausage biscuits
[point3] => maple & brown sugar oatmeal
)
View data:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: data
Filename: views/test_view.php
Line Number: 9
Obviously, in a REAL model I'd be working with the database. What I'm trying to accomplish in the sample is to a) establish the $data array; b) pass $data to the model, where it is modified by calls to the db (or updates the db), c)pass the modified $data back to the controller and on to the view. Unfortunately, $data appears to be unique in each file, rather than being handed back and forth.
All of that said, what painfully obvious thing am I missing? Thanks in advance!