2011-08-16 88 views
0

我正在codeigniter中創建一個web應用程序,它根據用戶添加到關鍵字列表中的關鍵字從用戶數據庫中檢索帖子。 我試圖檢索所有與用戶的關鍵字的職位,但是,而也有複式在帖子查詢只返回一行數據

這是關鍵字相匹配的是從我的模型

此函數檢索它的函數只返回一個職位用戶關鍵字

function user_keywords($user_id) { 
     //Get all the user keywords 
     $q = $this->db->select('keywords') 
         ->from('keywords') 
         ->where('U_id',$user_id) 
         ->get(); 
     $words = $q->result_array(); 
     return $words; 
    } 

該函數獲取職位

function get_latest_pheeds() { 
     $this->load->helper('date'); 
     $time = time(); 
     $keyword = $this->user_keywords($this->ion_auth->user_id); 
     foreach($keyword as $word) { 
     $q = $this->db->select('user_id,pheed_id,datetime,pheed,COUNT(pheed_comments.comment_id) as comments') 
     ->from('pheeds') 
     ->join('pheed_comments','pheed_comments.P_id=pheeds.pheed_id','left') 
     ->like('pheed',$word['keywords']) 
     ->order_by('datetime','desc') 
     ->get(); 
     } 
     $rows = $q->result_array(); 
     return $rows; 
    } 

我的控制器編碼它的JSON

function latest_pheeds() { 
      if($this->isLogged() == true) { 
      $this->load->model('pheed_model'); 
      $data = $this->pheed_model->get_latest_pheeds(); 
      echo json_encode($data); 
      } 
      return false; 
    } 

我真的很感激幫助

+0

你確定這個問題是不是在你的選擇()或result_array()方法? – Mike

+0

'get_latest_pheeds()'方法中的'$ q'將有最後一個查詢。在提出更好的方法之前,'$ word ['keywords']'包含了什麼?一個關鍵字? 「關鍵字」表中的「pheeds」表(在「pheed」字段中)也完全一樣嗎? – ifaour

+0

$ word ['keywords']包含從user_keywords()方法返回的關鍵字 – MrFoh

回答

1

我認爲(但很難未經測試)的問題來形成功能get_last_feeds:

您查詢是建立在每個循環的迭代,但是你在之後獲取結果。我想你只會得到最後一個查詢的結果(如果你沒有在每個循環中獲取結果,下一次迭代會覆蓋原始請求而不會獲取結果)。

我會做這樣的事情:

<?php 
function get_latest_pheeds() { 
    $rows = array(); // The final result, an array of rows 
    $this->load->helper('date'); 
    $time = time(); 
    $keyword = $this->user_keywords($this->ion_auth->user_id); 
    foreach($keyword as $word) { 
    $q = $this->db->select('user_id,pheed_id,datetime,pheed,COUNT(pheed_comments.comment_id) as comments') 
    ->from('pheeds') 
    ->join('pheed_comments','pheed_comments.P_id=pheeds.pheed_id','left') 
    ->like('pheed',$word['keywords']) 
    ->order_by('datetime','desc') 
    ->get(); 
    $rows[] = $q->result_array(); 
    } 

    return $rows; 
} 
+0

我將修改該函數並嘗試使用 – MrFoh

+0

它的工作,但有一些打嗝,我可以處理 – MrFoh