2011-08-14 20 views
1

jqueryで使用するために配列をjsonにエンコードしようとしていますか? これは私のモデルから機能Codeigniter結果配列が1行だけを返します

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

であり、これは私がブラウザでコードを実行するときには、データベースから1行のみを返す私のコントローラから

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

です。 助けてください

答えて

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配列は、現在{ \t \t \t \t $データ[] [ 'USER_ID'] = $行 – MrFoh

+0

あなたのforeachのコードを貼り付け – shikhar

+0

foreachの($行として$ rows-> result_array())を台無しにされています['ユーザー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

これは良いですが、あなたの構文は、アレイ

関連する問題