1

Trying to fill options in dropdown by fetching data from database into a view. However I get the error:

Message: Undefined property: CI_Loader::$AdminDataHelper

Filename: forms/user.php

Fatal error: Call to a member function get_colleges() on a non-object in /ci/app/views/includes/forms/user.php on line 54

    <select id="college" name="college-selector" >   
         <?php 
               foreach ($this->AdminDataHelper->get_colleges() as $colleges)
               {
                 echo "<option value='".$colleges['id']."'>".$colleges['description']."</option>";
               }
        ?>                   
     </select>

My Library class:-

class AdminDataHelper {

public function __construct()
{
    $this->CI =& get_instance();
    $this->CI->load->model('admin_m');      
}

public function get_colleges(){
    return $this->CI->admin_m->get_colleges();
}

My model:-

class Admin_M extends CI_Model {

function __construct()
{
    // Call the Model constructor
    parent::__construct();
    $this->global_db = $this->load->database('global', TRUE); 
}

public function get_colleges()
{
    $this->global_db->select('id, description');
    $this->global_db->from('College');
    $result = $this->global_db->get();
    $data=$result->result_array();
    return $data;
}

In my autoload.php file I added the library.

$autoload['libraries'] = array('database', 'form_validation', 'AdminDataHelper');
3
  • 1
    I think you need to carefully read the docs. You cannot use a name like that. Only the first letter of the file name is capitalized and then use all lower-case when referencing it. Admindatahelper.php and then $this->admindatahelper->. Follow the examples and see the "naming conventions" section here: codeigniter.com/user_guide/general/… Commented Jan 19, 2017 at 23:45
  • I also don't think you can call a Library from within a View. You need to call it from your Controller and only send the data into your View. Commented Jan 19, 2017 at 23:48
  • You are right. The issue was with the naming convention. Thanks Commented Jan 19, 2017 at 23:59

2 Answers 2

2

Carefully read the docs. You cannot use a name like that. Only the first letter of the file name is capitalized and then use all lower-case when referencing it.

Follow the examples and see the "naming conventions" section:

  • File names must be capitalized. For example: Myclass.php
  • Class declarations must be capitalized. For example: class Myclass
  • Class names and file names must match.

Admindatahelper.php and $this->admindatahelper

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

Comments

1

You should to use Model in this case instead of using Library, however this will work:

$this->adminDataHelper->get_colleges();

the first character must start with lowercase letter.

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.