简体   繁体   中英

CodeIgniter fatal error: Call to a member function display_clients() on a non-object

I am writing a program that basically does stuff with a database, I am having trouble with some of the functionality though, this is what it is supposed to do

public function clist() {
$this->load->model('list_model');
$fields = $this->list_model->listcli();
if ($fields === $this->list_model->listcli()) {
    $fieldl = $fields;
    $this->load->view('clientlist');
    $this->clientlist->display_clients($fieldl);
}

}

This loads the model which looks like this:

public function listcli()
{
 $this->db->list_fields('clients');
}

}

Then runs the model function listcli so that it will list all the fields in the clients database and puts the value into $fields I then call it fieldl and load a view that will show the data, the view looks like this:

<html>
<body>
<p> sup </p>
<?php

 function display_clients($fieldl)
 {

        ?>
        <html>
            <body>
            <p> sup2 </p>
                <ul>
                <?php
                foreach ($fieldl as $l) {
                ?>
                <li> 
                    <?php echo $l;?>
                </li>
                <?php
                }
    }

Then calls the function inside the view and passed the data from $fieldl into it.

but I am getting the error " Call to a member function display_clients() on a non-object in /codeigniter/src/application/controllers/clientlist.php on line 40"

line 40 is

$this->clientlist->display_clients($fieldl);

Can you help? Please and thank you.

(I know this kind of question has been asked before but they are always very specific to the code at hand so doesn't help me, I am really new to CodeIgniter so if you can keep any answer relatively simple I will be grateful).

Why can't you prepare the list in the controller and pass it to view via $data array?

$my_list = '<ul>';
foreach ($fieldl as $l) {
$my_list .= '<li>'.$l.'</li>';
}
$my_list .= '</ul>';

$data['my_list'] = $my_list;

then you can access it in view as $my_list . Just echo it where you need it.

EDIT: AS per your own answer, yes - exactly. Using example above you would pass the $data to view as the second parameter:

$this->load->view('my_view',$data);

Then in the View, you access $data['my_list'] as $my_list , $data['foo'] as $foo etc.

我需要使用第二个参数将数据传递到视图中,而不是自己的临时解决方案,CI接受一个对象作为CI_Loader :: view()的第二个参数

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM