2017-12-10 12 views
0

こんにちは私は持っ は配列として型CI_DB_mysqli_resultのオブジェクトを使用することはできません

A PHPエラーが

重大度が発生した

この結果:エラー

メッセージ:配列

ようなタイプのCI_DB_mysqli_resultのオブジェクトを使用することはできません

ファイル名:controllers/Threads.php

行番号:48

バックトレース:

これは、エラーはこのコードから

$data['title'] = $data['item']['catname']; 
あるコントローラー

// category 
public function threadsview($slug) { 
    // get threads category 
    $data['item'] = $this->threads_model->getThreadsCategory($slug); 

    $data['title'] = $data['item']['catname']; 
    $data['desc'] = ''; 

    $this->load->view('themes/default/header', $data); 
    $this->load->view('threads/threadsview', $data); 
    $this->load->view('themes/default/footer'); 
} 

そして、私のモデルでコード

//get threads categories by slug 
    public function getThreadsCategory($slug) { 
     $this->db->order_by('id','asc'); 
     $this->db->where('slug',$slug); 
     $query = $this->db->get('threads_cat'); 
     return $query; 
    } 

の私のコードです

私は誰かがこのことに関して私に助けてくれることを願っています。

+0

'$データ[「項目」]'配列ではありません、この行を追加します。それが配列かどうかを確認してください。 – Wreigh

答えて

0

I`veは、あなたの場合に、その、今

public function threadsview($slug) { 
     // get threads category 
     $data['item'] = $this->threads_model->getThreadsCategory($slug); 
     if($data['item']->num_rows()<=0){ 
      $data['title'] = 'Opps!, 404 page not found'; 
      $this->load->view('themes/default/header', $data); 
      $this->load->view('errors/html/error_404'); 
      $this->load->view('themes/default/footer'); 
     }else 
     foreach ($data['item']->result() as $res) { 


     $data['title'] = $res->catname; 
     $data['desc'] = $res->description; 

     $this->load->view('themes/default/header', $data); 
     $this->load->view('threads/threadsview', $data); 
     $this->load->view('themes/default/footer'); 
     } 
    } 
0

$data['item']は実際に配列に結果をフェッチしなかったため、おそらく配列ではありません。

関数の中で、最初にクエリの結果を配列にフェッチします。

​​
+0

ありがとうございますが、このコードはまだ動作していないエラーは同じです。そして、私はすでにあなたの回答に対して非常に感謝しています。 –

1

あなたがする必要があるのは、結果または行を取得し、その中に何かがあるかどうかを確認することです。だから、あなたはあなたのクエリ実行するとき:

if($query->num_rows() > 0) 
{ 
    return $query->result_array(); 
} 

return FALSE; 

それとも、単に1つの行を期待している場合:

if($query->num_rows() == 1) 
{ 
    return $query->row_array(); 
} 

return FALSE; 

は、必ずあなたはこれを行う必要がある何かを返す前に

$query = $this->db->get('threads_cat'); 

をそれを使用する前に$data['item'] == FALSEを確認してください:

if($data['item'] !== FALSE && is_array($data['item'])) 
{ 
    // $data['item'] is an array and can be used as an array 
} 
+0

ありがとう、おい、私はすでにそれを把握し、今働いている –

+0

それを聞いてうれしい。クレジットが期限切れの場合、クレジットをいくつか与えてください。 –

+0

確かに..あなたの助けに感謝します。 –

0

の作業は、あなたのモデルにあなたがあなたのモーダル

$data['item'] = $this->threads_model->getThreadsCategory($slug);

に配列またはオブジェクトを返すことを確認し作られたご回答いただきありがとうございます使用

$this->db->from($table); $query = $this->db->get(); return $query->row(); //これはオブジェクト&ない配列として最初の行を返す

コントローラ

$data['title'] = $data['item']->catname;

アレイのあなたは、このコントローラでアレイ

として

$data['title'] = $data['item']['catname'];

を最初の行を返している。この

$query->first_row('array'); //のようなものを使用することができます

//静止画あなたは、コントローラで あなたのデータを印刷することができ、問題を持つことは、ちょうどあなたの荷重 - >ビュー() コメントと

print_r("<pre>"); print_r($data['item']); print_r("</pre>");

関連する問題