Abin Jacob Abin Jacob - 1 year ago 86
JSON Question

Codeigniter, How to get database table data displayed in json format?

I have to display the result in json format.
my code looks like:

$query = $this->db->get();

foreach($query->result() as $row)
echo json_encode($row);

and the result looks like:


I have to get this json in [] and separated by comma (,) . that is I want the result looks like:


what all changes should I done in the code.

Answer Source

Retrieve the record in array and then encode it

$result = $this->db->get('country')->result_array();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download