0

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

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.