CodeIgniter Forums
Query to json - HELP - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Query to json - HELP (/showthread.php?tid=51367)



Query to json - HELP - El Forum - 05-01-2012

[eluser]Near[/eluser]
Hi! I have a table named product containing this value
p_id | p_name | p_quantity
1 p1 10
2 p2 20
3 p1 5
4 p1 6

how would i get the records like this?

p1 [10,5,6]
p2 [20]

and then convert it to json.. Thanks!


Query to json - HELP - El Forum - 05-01-2012

[eluser]ojcarga[/eluser]
I don´t know if that is possible, but I´d do it this way,

Code:
$this->output
    ->set_content_type('application/json')
    ->set_output(json_encode(array('foo' => 'bar')));

array('foo' => 'bar') is the array containing the data retrieved from the database.

http://ellislab.com/codeigniter/user-guide/libraries/output.html


Query to json - HELP - El Forum - 05-01-2012

[eluser]CroNiX[/eluser]
Look into MySQLs group_concat() function.


Query to json - HELP - El Forum - 05-01-2012

[eluser]Near[/eluser]
[quote author="CroNiX" date="1335896855"]Look into MySQLs group_concat() function.[/quote]


hi thanks! Group_concat do the trick on my problem. I thought i need to use json on that. Anyways! Thanks again..