Welcome Guest, Not a member yet? Register   Sign In
Passing data to/from models
#1

[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:
&lt;?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:
&lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;Test_view&lt;/title&gt;
    &lt;/head&gt;

    &lt;body&gt;
        View data:<br/>
        <pre>
            &lt;?php
            //echo data to see if it's the complete array
            print_r($data); ?&gt;
        </pre>

    &lt;/body&gt;
&lt;/html&gt;

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!
#2

[eluser]Tom Schlick[/eluser]
Code:
$data = $this->test_model->testify($data);

no quotes in parameters
and your model function has to be like so

Code:
function testify($data)
    {
        //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;
    }
notice the data variable in the parameters of the function

you have to assign the vairable data to the returned information from the model.
#3

[eluser]dgibsonky[/eluser]
trs, you are a scholar and a gentleman! Everything is perfect now. I did also realize that, in my view, I cannot call on $data directly:
Code:
//bad mojo:
print_r($data);

Because $data is exploded into its individual elements when entering the view, it no longer exists, and I have to call each element individually:
Code:
//good mojo:
echo $point1."<br>";
echo $point2."<br>";
echo $point3."<br>";

As I said, it's all happy now, and thanks again for your fast and excellent help!




Theme © iAndrew 2016 - Forum software by © MyBB