开发者

How do I display adodb data in Codeigniter in my view?

开发者 https://www.devze.com 2023-01-16 17:30 出处:网络
I have this code in my model in codeigniter: <?php class User_model extends Model { function User_model()

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',开发者_如何学Python$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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消