0

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.

3
  • Don't you need to get the result from your query like this: $user = $this->db->get('user')->result();? First, that code should really be in a model. Second, instead of getting the result as an object you could just get instead as an array which you could just pass to json_encode() and get rid of the foreach altogether Commented Mar 9, 2017 at 8:45
  • Pacio can gate fore details about. json_encode() Commented Mar 9, 2017 at 9:22
  • json_encode(['name' => $row->user, 'email' => $row->email]). Also you should not be sending the users password. It should be hashed and stored within your DB, never to be taken out Commented Mar 9, 2017 at 9:40

2 Answers 2

2

json_encode converts a standard php array to json. Like I said, your call to the db looks wrong to me, but it doesn't seem to give you an error, so I don't know what's going on there. But this is what it looks to me like how it should be:

 public function __construct()
    {
            parent::__construct();
            $this->load->helper('url_helper');
            $this->load->database();
    }
     public function index()
    {
        $query = $this->db->get('user');
        $user = $query->result_array();
        echo json_encode($user);
}

Aside from my question about how you get your data, if you can convert $user to a standard key=>value array the json_encode() function will do the rest.

Sign up to request clarification or add additional context in comments.

1 Comment

$user = $query->result_array(); Ok I fix it. thank you so much.
0

Put a comma before the object except the first one:

public function __construct()
{
        parent::__construct();
        $this->load->helper('url_helper');
        $this->load->database();
}
 public function index()
{
    $user = $this->db->get('user');
    $index = 0;
    echo '{"user": [';

    foreach ($user->result() as $row)
    {
        if (!($index++)) echo ",";
        echo "{";
            echo '"name" : "'. $row->user;
            echo '", "password" : "'. $row->password;
            echo '", "email" : "'. $row->email;
        echo '"}';
    }
    echo ']}';   
}

}

3 Comments

Donn't need in first last. Only for separation.
@DeenJustin have you actually tested this code? It should write something like {"user": [{"name" : "admin", "password" : "abcd", "email" : "[email protected]"},{"name" : "deenj", "password" : "dfgh", "email" : "[email protected]"}]}. The code will put a comma before all elements except the first.
f (($index++)) echo ","; Thanks to all . Pacio can gate fore details about. json_encode()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.