php - Codeigniter model shows Undefined property error on helper

547

Here is my helper function in a helper file misc_helper.php,

function  echoEditorsPickItems($projects = array('02134444', '0314020000')) {
  if (!is_null($projects)) {
    $CI = &get_instance();

    $CI->load->database();
    $CI->load->model('Project');

    $projects = $CI->Project->getAllProjects(); // this is the 28th line
                          // getAllProjects() returns
                          // an arrays of item

    for ($i=0; $i<EDITORS_PICK_ITEM_LIMIT; $i++) {
      // $CI->Project->getItem('0214010001');

      $out = '';
      $out .= '<li>';
      $out .= '<span>';
      $out .= '<a href="'. base_url() . 'portfolio/detail/' . $projects[$i]['pId'] .'" >';
      $out .= '<img src="'. base_url() . 'assets/img/blog/' . $projects[$i]['brandImageUrl'] . '" alt="' . $projects[$i]['title'] . '">';
      $out .= '<span ></span>';
      $out .= '</a>';
      $out .= '</span>';
      $out .= '<p >';
      $out .= '<a href="'. base_url() . 'portfolio/detail/' . $projects[$i]['pId'] .'" >' . $projects[$i]['title'] . '</a>';
      $out .= '<small >' . $CI->Project->title . '</small>';
      $out .= '</p>';
      $out .= '</li>';

      echo $out;
    }

  }
}

When I call it, I get the following error,

Severity: Notice
Message:  Undefined property: Project::$Project
Filename: helpers/misc_helper.php
Line Number: 28

Edit: Here is my model,

class   Project extends CI_Controller {
    private $tableName = DB_TABLE_NAME_PROJECT;

    public  $title;
    public  $description;
    public  $client;
    public  $skills;
    public  $preparedBy;
    public  $catagory;
    public  $date;
    public  $url;
    public  $images;
    public  $templateType;

    function __construct() {
        parent::__construct();
        $this->load->database();
    }

    public function getItem($projectId = NULL) {
        if (!is_null($projectId)) {
            $res = $this->db->get_where($this->tableName, array('pId' => $projectId));

            if ($res->num_rows) {
                $this->title = $res->row(1)->title;
                $this->description = $res->row(1)->description;
                $this->client    = $res->row(1)->client;
                $this->preparedBy       = $res->row(1)->preparedBy;
                $this->skills           = explode(TOKEN_DELIMINATOR, $res->row(1)->skills);
                $this->catagory         = $res->row(1)->catagory;
                $this->date             = $res->row(1)->date;
                $this->url              = $res->row(1)->url;
                $this->images           = explode(TOKEN_DELIMINATOR, $res->row(1)->images);
                $this->templateType     = ($res->row(1)->templateType=='1'? 1:0);

                return TRUE;
            }

            return FALSE;
        }
        return FALSE;
    }

    public function getAllProjects() {
        $res = $this->db->get($this->tableName);

        $projects = array();
        for ($i=0; $i<$res->num_rows(); $i++){
            $projects[$i] = array();
            $projects[$i]['title']          = $res->row($i)->title;
            $projects[$i]['catagory']       = explode(TOKEN_DELIMINATOR, $res->row($i)->catagory);
            $projects[$i]['brandImageUrl']  = $res->row($i)->brandImageUrl;
            $projects[$i]['pId']            = $res->row($i)->pId;
        }

        return $projects;
    }
}
732

Answer

Solution:

The CI Instance should created with reference.

$CI = &get_instance();

Edit:

You did mistake in your model.

class   Project extends CI_Model {  // you should extend model not controller
924

Answer

Solution:

What are you trying to acheive? By the looks of it you're trying to loop through all projects (or perhaps a certain number of them) and display them?

If that's the case then firstly (as kumar_v has said) you need to pass theget_instance(); function by reference as this will allow you to use the original CodeIgniter object rather than creating a copy of it. http://ellislab.com/codeigniter/user-guide/general/creating_libraries.html

Secondly, it looks like you're trying to access a property of each project (pId, brandImageUrl, title). If this is the case, you need to assign$CI->Project->getAllProjects(); to a variable and loop through these results. e.g.

function echoEditorsPickItems($projects = array('02134444', '0314020000')) {
    if (!is_null($projects)) {
      $CI =& get_instance();
      $CI->load->model('Project');

      $projects = $CI->Project->getAllProjects();

      foreach ($projects as $project) {
        $out = '';
        $out .= '<li>';
        $out .= '<span>';
        $out .= '<a href="' . base_url() . 'portfolio/detail/' . $project->pId . '" >';
        $out .= '<img src="' . base_url() . 'assets/img/blog/' . $project->brandImageUrl . '" alt="' . $project->title . '">';
        $out .= '<span ></span>';
        $out .= '</a>';
        $out .= '</span>';
        $out .= '<p >';
        $out .= '<a href="' . base_url() . 'portfolio/detail/' . $project->pId . '" >' . $project->title . '</a>';
        $out .= '<small >' . $project->title . '</small>';
        $out .= '</p>';
        $out .= '</li>';
        echo $out;
      }      
    }    
  }

Hope that helps.

People are also looking for solutions to the problem: Inventory Management Software - php MySQL

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.