简体   繁体   中英

Undefined property,Call to a member codeigniter model function in php helper function PHP Codeigniter i

Below is my helper function(defined in autoload as db)

<?php

function get_categories_h(){
    $CI =  get_instance();
    $categories = $CI->Product_model->get_categories();
    return $categories;
}

function in model is

public function get_categories(){

    $this->db->select('*');
    $this->db->from('categories');
    $query = $this->db->get();
    return $query->result();
}

code in the view is

<?php foreach(get_categories_h() as $category) : ?>
                            <li class="list-group-item"><a href="#"><?php echo $category->name; ?></a></li>
                            <?php endforeach; ?>

error I am getting is

Message: Undefined property: Products::$Product_model

Filename: helpers/db_helper.php

Fatal error: Call to a member function get_categories() on a non-object in C:\\xampp\\htdocs\\gamingplace_done\\application\\helpers\\db_helper.php

You will need to load the model in your helper for it to be accessible. Something like:

<?php

function get_categories_h(){
    $CI =  get_instance();
    $CI->load->model('Product_model');
    $categories = $CI->Product_model->get_categories();
    return $categories;
}

You can read more about how to load it at: http://www.codeigniter.com/userguide2/general/models.html#loading

or

http://www.codeigniter.com/user_guide/general/models.html#loading-a-model

Depending on what CI version you are using.

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