Welcome Guest, Not a member yet? Register   Sign In
Helping me with first MVC attemp
#1

[eluser]puddles[/eluser]
Hi,

Somewhere, I'm losing this...I have read the documentation around Active Record, Controllers, Models, and Views and result generation. Still, I'm having a hard time getting the data retrieved from the database (through the model) to appear in my view. Here is my code:

Model
Code:
<?php

class Standings extends Model {
    
    var $owner;
    var $points;
    var $datetime;
    
    function Standings()
    {
        parent::Model();
    }
    
    function get_standings()
    {
        $query = $this->db->get('owner_overall');
        return $query->result();
    
    }
}

Controller
Code:
<?php

class Welcome extends Controller {

    function Welcome()
    {
        parent::Controller();    
    }
    
    function index()
    {    
        // debugging
        $this->output->enable_profiler(TRUE);
        
        // loading table library
        $this->load->library('table');
                
        // load the header and navigation
        $this->load->view('header');
        $this->load->view('nav');
        
        // load the Standings model and call function get_standings
        $this->load->model('Standings');
        $standings = $this->Standings->get_standings();
        
        // load the welcome view and pass it data from Standings model
        $this->load->view('welcome', $standings);
        
        // load the footer
        $this->load->view('footer');
    }

}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */

and now the snippet from the view.....
Code:
<div class="span-6">
    &lt;!-- this is where I want to view the data returned from db --&gt;
    &lt;?php
    
    foreach ($standings as $row)
    {
       echo $row->Owner;
       echo $row->Points;
       echo $row->Date_Updt;
    }
    
    ?&gt;

</div>

Obviously I'm doing this incorrectly as I get both an undefined variable "standings error as well as an "invalid argument supplied foreach()". Can someone point out where I've gone wrong?

Thanks....
#2

[eluser]CroNiX[/eluser]
You're almost there Smile

These lines:
Code:
$standings = $this->Standings->get_standings();
        
// load the welcome view and pass it data from Standings model
$this->load->view('welcome', $standings);

should be:
Code:
$data = array();
$data['standings'] =  $this->Standings->get_standings();
// load the welcome view and pass it data from Standings model
$this->load->view('welcome', $data);

Then, $standings will be available to your view. Your view is fine just the way it is (well, except its gonna put the output all on one line with no spaces separating the values). Good luck!
#3

[eluser]puddles[/eluser]
Thanks so much for replying.

Quick question - does putting:
Code:
$data = array();

prior to getting the data through the model mean that the data being passed to the view will be in array format as opposed to object format? Therefore, I would need to access the data by:
Code:
echo $row['Owner'];

as opposed to

Code:
echo $row->Owner;
#4

[eluser]CroNiX[/eluser]
No. CI automatically explodes the $data array in the view. This allows for passing multiple variables if needed. If the data type you assign to $data['something'] is an object, you would still access it as an object (or string, array, whatever) in the view, but leave off the $data part (just use $something->Owner).

Like in your model you can do:
Code:
$data['header'] = 'hello';  //string
$data['some_object'] = $this->some_model->get_people();  //assume you are returning db object
$data['some_array'] = array('one' => 'first');  //array

In the view:
Code:
echo $header; //echos 'hello'

foreach($some_object as $people)
{
    echo $people->fname . '<br />';  //echos first name of person from object
}

echo $some_array['one'];  //echos 'first', or if a multidimensional array you can traverse it like the object example.




Theme © iAndrew 2016 - Forum software by © MyBB