Generating JSON events with CodeIgniter Array |
[eluser]Timothy_[/eluser]
Hello, I'm new to JSON, PHP and programming in general so please excuse me if there is an easy solution. I am using the excellent FullCalendar (http://arshaw.com/fullcalendar) to list JSON events from my database. The format for the JSON events is like this: Code: echo json_encode(array( Now I want to generate this format with a foreach loop from my active record. Code: $query = $this->db->query("YOUR QUERY"); How can I go about doing this? I imagined something like this Code: $query = $this->db->query("MY QUERY"); This doesn't work because its expecting array items, rather than the foreach loop. I need a complete shift of thinking, but I'm struggling to look at this problem in a new light. Thanks for any help, Tim Mohr
[eluser]umefarooq[/eluser]
hi try $query->result_array and make json from it. Code: foreach ($query->result_array() as $row) for single row you can use $query->row_array() Code: $query = $this->db->query("YOUR QUERY"); check in here http://ellislab.com/codeigniter/user-gui...sults.html
[eluser]Timothy_[/eluser]
Hello, I will test this when I get home, but it looks promising, How do I format the JSON like this though? Code: 'title' => '$row->['name'];', if i encode the JSON Code: echo json_encode($row); then that wouldn't pull the 'title' => ' across? thanks again! Tim
[eluser]umefarooq[/eluser]
no need to with code you can put in query like Code: $sql = 'select title as id, name as title, body as start from table'; try it will work and save your extra effort in code.
[eluser]Phil Sturgeon[/eluser]
Code: $query = $this->db->query('select title as id, name as title, body as start from table'); To further umefarooq's suggestion here is the complete code.
[eluser]Timothy_[/eluser]
Wooooo, that is soo cool guys! Thanks heaps. Theres just one thing... It doesn't loop. I am only getting the first query outputed to JSON. Is there some way to fix this? Thanks for your help. Tim
[eluser]Timothy_[/eluser]
Just thinking outloud... I have researched and found that mysql allows loops, but as to the syntax, the examples I've found don't help me.
[eluser]Timothy_[/eluser]
Anyone? I'm getting pretty desperate. I appreciate your help. Tim
[eluser]yuvaraj[/eluser]
Hi, Try this out for looping your data into JSON. Think this will help, if u find any bugs in this let me know i'll look through it. Code: foreach ($data as $row) { Regards, Yuva
[eluser]Aken[/eluser]
Congratulations! You have answered a 2 1/2 year old thread! (sigh) |
Welcome Guest, Not a member yet? Register Sign In |