I have been searching for the solution on how to create JSON array in JSON object from Mysql query. Need help from any of you.
My source data in mysql:
Table: parent
--------------------------------------
| id | firstname | lastname | rating |
--------------------------------------
| 1 | John | Doe | 9.3 |
| 2 | Marry | Jane | 8.5 |
| 3 | Paijo | Masni | 9.8 |
--------------------------------------
Table: children
------------------------------
| id | idparent | name |
------------------------------
| 1 | 1 | John A |
| 2 | 1 | John B |
| 3 | 1 | John C |
| 4 | 2 | Jane A |
| 5 | 2 | Jane B |
| 6 | 3 | Bang Jo |
| 7 | 3 | Kak Jo |
------------------------------
My MySQL Query:
Select p.firstname, p.lastname, p.rating, c.name as children
from parent p join children c on p.id = c.idparent
Output:
-------------------------------------------------
| id | firstname | lastname | rating | children |
-------------------------------------------------
| 1 | John | Doe | 9.3 | John A |
| 1 | John | Doe | 9.3 | John B |
| 1 | John | Doe | 9.3 | John C |
| 2 | Marry | Jane | 8.5 | Jane A |
| 2 | Marry | Jane | 8.5 | Jane B |
| 3 | Paijo | Masni | 9.8 | Bang Jo |
| 3 | Paijo | Masni | 9.8 | Kak Jo |
-------------------------------------------------
Here is my output of JSON format that I wanted:
var profile = [
{
"firstname": "John",
"lastname": "Doe",
"rating": 9.3,
"children": [
"John A",
"John B",
"John C"
],
"id": 1
},
{
"firstname": "Marry",
"lastname": "Jane",
"rating": 8.5,
"children": [
"Jane A",
"Jane B"
],
"id": 2
},
{
"firstname": "Paijo",
"lastname": "Masni",
"rating": 9.8,
"children": [
"Bang Jo",
"Kak Jo"
],
"id": 3
}
];
The one I got stuck from generating the JSON is on the children: [], I want to have an array with double-quote "" separated by comma ',' inside the [].
Thank you.
NB: Currenty I am using codeigniter for my coding. If you have faced this kind of problem on codeigniter before, I'm looking forward to getting the sharing from you.