views:

50

answers:

1

I have this code in my model in codeigniter:

<?php
class User_model extends Model {

    function User_model()
    {
        parent::Model();
    }
    function get_data()
    {
        $pages = false;

        // Get the pages from the database using adodb if needed
        $this->adodb->connect();

        $recordSet = $this->adodb->execute('SELECT id, genreID, artist, albumName FROM album' );
        if ( ! $recordSet )
        {
            log_message( 'error', 'Error connecting to the database' );
            log_message( 'debug', $this->adodb->getErrorMsg());
        }
        else
        {
            unset( $this->pages );
            while (!$recordSet->EOF)
            {
                $pages[] = array(
                    'id'    => $recordSet->fields[0],
                    'genreID'    => $recordSet->fields[1],
                    'artist'    => $recordSet->fields[2],
                    'albumName'    => $recordSet->fields[3]
                );

                $recordSet->MoveNext();
            }
            $this->pages = $pages;
        }

        $this->adodb->disconnect();

    } 
}
?>

I have this in my controller:

<?php

    class Welcome extends Controller {

        function Welcome()
        {
            parent::Controller();   
        }

        function index()
        {
            //
            $this->load->model('User_model');
            $data['query'] = $this->User_model->get_data();
            $this->load->view('welcome_message',$data);
        }
    }

What I cannot do is get my model results into my view. There is no result() object because I used adodb so this:

<?php foreach($query->result() as $row): ?>
<p>
    <?=$row->albumName;?>
</p>
<?php endforeach; ?>

gets me an error.

What do I put in my view to get the query results of my model. I know the model works because I can echo $recordSet->fields[3]; from the model and see the album name.

Thank you for any help.

edit: I don't understand why my get_data() call in my view returns nothing.

A: 

For one you are not returning anything from get_data so that will cause $query to be null. Also you shouldn't be executing the query in the view because view content should not be handling data layer operations, also you've already disconnected from the db.

Ken Struys
I was copying the guy at the bottom of here http://codeigniter.com/forums/viewthread/65672/ I thought I was following the active record pattern calling the query from the controller but I don't know why get_data returns nothing.
johnny
I put in a return $recordSet but get_data returns nothing to the view. The data is only present in the model.
johnny
You shouldn't need to use $query->results(), did you try something similar to the iteration done in the model over recordSet? note that the reason the model doesn't return a value is because the implementer set the value on a property of the model ($this->pages), you might be able to access it $this->User_model->pages but usually I don't add properties to model.
Ken Struys