2011-08-14 2 views
1

jquery와 함께 사용할 json으로 배열을 인코딩하려고합니다. 이 내 모델Codeigniter 결과 배열이 하나의 행만 반환합니다.

function get_latest_pheeds() { 
     $this->load->helper('date'); 
     $time = time(); 
     $q = $this->db->select("user_id,pheed_id,pheed,datetime,COUNT(pheed_comments.comment_id) as comments") 
         ->from('pheeds') 
         ->join('pheed_comments','pheed_comments.P_id=pheeds.pheed_id','left') 
         ->group_by('pheed_id') 
         ->order_by('datetime','desc') 
         ->limit(30); 
     $rows = $q->get(); 
      foreach($rows->result_array() as $row) { 
       $data['user_id'] = $row['user_id']; 
       $data['pheed_id'] = $row['pheed_id']; 
       $data['pheed'] = $row['pheed']; 
       $data['comments'] = $row['comments']; 
       $data['datetime'] = timespan($row['datetime'],$time); 
      } 
      return $data; 
    } 

에서 기능입니다 그리고 이것은 내 컨트롤러

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

내가 브라우저에서 코드를 실행할 때 데이터베이스에서 단 1 행을 반환에서이다. 제발 도와주세요

답변

2

모든 반복에서 데이터를 덮어 쓰는 중입니다!

$data[] = array(
    'user_id' => $row['user_id']; 
    'pheed_id' => $row['pheed_id']; 
    'pheed' => $row['pheed']; 
    'comments' => $row['comments']; 
    'datetime' => timespan($row['datetime'],$time); 
    ) ; 
+0

배열의 각 요소에 대해 'USER_ID'=> $ 행 [ 'user_id를'] ,해야한다 그러나 JSON 배열 해주기 – MrFoh

+0

은 ($ 행은 $ rows-> result_array()) 귀하의 foreach 코드 – shikhar

+0

의 foreach { \t \t \t \t $ 데이터 [] 'USER_ID'] = $ 행 붙여 엉망 [ 'user_id']; \t \t \t \t $ data [] [ 'pheed_id'] = $ row [ 'pheed_id']; \t \t \t \t $ data [] [ 'pheed'] = $ row [ 'pheed']; \t \t \t \t $ data [] [ 'comments'] = $ row [ 'comments']; \t \t \t \t $ data [] [ 'datetime'] = timespan ($ row [ 'datetime'], $ time); \t \t \t} – MrFoh

0

같은

사용 일이 좋다하지만 구문은 작동

관련 문제