Welcome Guest, Not a member yet? Register   Sign In
Generating JSON events with CodeIgniter Array
#1

[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(
    
        array(
            'id' => 111,
            'title' => "Event1",
            'start' => "$year-$month-10",
            'url' => "http://yahoo.com/"
        ),
        
        array(
            'id' => 222,
            'title' => "Event2",
            'start' => "$year-$month-20",
            'end' => "$year-$month-22",
            'url' => "http://yahoo.com/"
        ),

        array(
            'id' => 333,
            'title' => "Event3",
            'start' => "$year-11-20",
            'end' => "$year-11-22",
            'url' => "http://yahoo.com/"
        )
    
    ));

Now I want to generate this format with a foreach loop from my active record.

Code:
$query = $this->db->query("YOUR QUERY");

foreach ($query->result() as $row)
{
   echo $row->title;
   echo $row->name;
   echo $row->body;
}

How can I go about doing this?

I imagined something like this

Code:
$query = $this->db->query("MY QUERY");

echo json_encode(array(

foreach ($query->result() as $row)
{
   array(
   'id' => '$row->title',
   'title' => '$row->name',
   'start' => "$row->body",
   ),
}

));

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
#2

[eluser]umefarooq[/eluser]
hi try $query->result_array and make json from it.

Code:
foreach ($query->result_array() as $row)
{
   echo $row['title'];
   echo $row['name'];
   echo $row['body'];
echo json_encode($row);
}

for single row you can use $query->row_array()
Code:
$query = $this->db->query("YOUR QUERY");

if ($query->num_rows() > 0)
{
   $row = $query->row_array();

   echo $row['title'];
   echo $row['name'];
   echo $row['body'];
}

check in here

http://ellislab.com/codeigniter/user-gui...sults.html
#3

[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
#4

[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.
#5

[eluser]Phil Sturgeon[/eluser]
Code:
$query = $this->db->query('select title as id, name as title, body as start from table');

echo json_encode($query->result_array());

To further umefarooq's suggestion here is the complete code.
#6

[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
#7

[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.

Sad
#8

[eluser]Timothy_[/eluser]
Anyone? I'm getting pretty desperate.

I appreciate your help.

Tim
#9

[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) {
$jsondata[] = array('id' =>    $row['id'],
                  'title' => $row['title'] ,
                  'start' => $row['start'],
                  'end'   => $row['end'],
                  'url'   => $row['url']  
                            );
}
echo json_encode($jsondata );


Regards,
Yuva
#10

[eluser]Aken[/eluser]
Congratulations! You have answered a 2 1/2 year old thread! (sigh)




Theme © iAndrew 2016 - Forum software by © MyBB