1
我試圖將數組編碼爲json以便與jQuery一起使用。 這是函數從我的模型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行。 請幫我出來
它的工作陣列中的每個元素但JSON陣列現在弄亂 – MrFoh
粘貼的foreach代碼 – shikhar
的foreach($ rows-> result_array()作爲$行){ \t \t \t \t $數據[] [ '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