2011-08-16 94 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