2014-02-17 99 views
1

這裏是一個幫助文件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'] .'" class="post-img">'; 
      $out .= '<img src="'. base_url() . 'assets/img/blog/' . $projects[$i]['brandImageUrl'] . '" alt="' . $projects[$i]['title'] . '">'; 
      $out .= '<span class="overlay"></span>'; 
      $out .= '</a>'; 
      $out .= '</span>'; 
      $out .= '<p class="wrap">'; 
      $out .= '<a href="'. base_url() . 'portfolio/detail/' . $projects[$i]['pId'] .'" class="rp_title">' . $projects[$i]['title'] . '</a>'; 
      $out .= '<small class="rp_date">' . $CI->Project->title . '</small>'; 
      $out .= '</p>'; 
      $out .= '</li>'; 

      echo $out; 
     } 

    } 
} 

當我打電話吧,我得到以下錯誤,

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

編輯:這是我的模型,

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; 
    } 
} 

回答

1

CI實例應該參考創建。

$CI = &get_instance(); 

編輯:

你在你的模型做了錯誤。

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

試過了,沒有工作! – Dewsworld

+0

實際上第28行是什麼? –

+0

剛剛更新了我的原始代碼,第28行 – Dewsworld

1

你想達到什麼目的?通過它的外觀,你試圖循​​環所有項目(或者其中的一定數量)並顯示它們?

如果是這種情況,那麼首先(如kumar_v所說的),您需要通過引用傳遞get_instance();函數,因爲這將允許您使用原始CodeIgniter對象而不是創建它的副本。 http://ellislab.com/codeigniter/user-guide/general/creating_libraries.html

其次,它看起來像試圖訪問每個項目(pId,brandImageUrl,title)的屬性。如果是這種情況,則需要將$CI->Project->getAllProjects();分配給一個變量並循環這些結果。例如

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 . '" class="post-img">'; 
       $out .= '<img src="' . base_url() . 'assets/img/blog/' . $project->brandImageUrl . '" alt="' . $project->title . '">'; 
       $out .= '<span class="overlay"></span>'; 
       $out .= '</a>'; 
       $out .= '</span>'; 
       $out .= '<p class="wrap">'; 
       $out .= '<a href="' . base_url() . 'portfolio/detail/' . $project->pId . '" class="rp_title">' . $project->title . '</a>'; 
       $out .= '<small class="rp_date">' . $project->title . '</small>'; 
       $out .= '</p>'; 
       $out .= '</li>'; 
       echo $out; 
      }    
     }   
    } 

希望有幫助。

+0

嘗試添加** get_instance()**,但沒有得到運氣!我發現$ CI無法訪問模型。 – Dewsworld

+0

你有沒有通過參考? &get_instance(); (我錯過了我的原始答案) –

+0

是的,我做到了。 (雖然沒有寫在我原來的帖子後,幾次失敗的嘗試) – Dewsworld

相關問題