I make a simple code for JSON api like that:
<?php
class Json extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper('url_helper');
$this->load->database();
}
public function index()
{
$user = $this->db->get('user');
echo '{"user": [';
foreach ($user->result() as $row)
{
echo "{";
echo '"name" : "'. $row->user;
echo '", "password" : "'. $row->password;
echo '", "email" : "'. $row->email;
echo '"},';
}
echo ']}';
}
}
Result is
{"user": [{"name" : "admin", "password" : "abcd", "email" : "[email protected]"},{"name" : "deenj", "password" : "dfgh", "email" : "[email protected]"},]}
The issue is the the comma repeating. Please help to fix the bug.