codeigniter JSON
$forum_list->result()
returns an array of results.
If you only want 1 row, use $forum_list->row()
, otherwise in the javascript, you'll need to loop through all the rows.
$.each(data, function(i,v){ alert(v.overskrift);});
EDIT: When outputting JSON, do not print anything before or after. You need to remove $this->load->view('includes/footer', $data);
after the json_encode
. Also, controllers don't return anything.
EDIT 2: Replace if ($data['forum_list'] === true)
with if ($data['forum_list'] !== false)
. The ===
compares type, and an array is not a boolean.
Model:
function forumList(){ $this->db->select('overskrift', 'indhold', 'brugernavn', 'dato'); $this->db->order_by('id', 'desc'); $forum_list = $this->db->get('forum_traad'); if($forum_list->num_rows() > 0) { return $forum_list->result_array(); } else { return false; }}
Controller:
function forumThreads() { $this->load->model('ajax_model'); $data['forum_list'] = $this->ajax_model->forumList(); if ($data['forum_list'] !== false) { echo json_encode($data['forum_list']); }}
Try this:
//works only with php 5.3echo json_encode($data['forum_list'], JSON_FORCE_OBJECT);