Harikris Harikris - 10 months ago 39
JSON Question

How will i print the result in view in code igniter?

I am trying to get a result in view returned as array in controller.This is a result of an api cal using the json.
I need to display the results in the view. There is design integrated in view to display it.
How could i do it in code igniter?

Function call:

$data['nearme_values'] = $this->swsdk->nearMe($lat, $lon, $radius, $page, $size);


This is the var_dumb result of the function call.

stdClass Object
( [content] => Array
( [0] => stdClass Object
( [id] => 1 [name] => Make America Great
[screenShotUrl] => http://52.77.190.221/SW/speaker_wire_images/streamSShot/stream-1479113014667.jpg
[type] => PUBLIC [status] => RECORDED [userId] => 1 [userHandle] => abhilekh
[channelId] => 1 [channelName] => Sports [likes] => 0 [views] => 0
[liveViewers] => 0
)
[1] => stdClass Object
( [id] => 2 [name] => Make America Great
[screenShotUrl] => http://52.77.190.221/SW/speaker_wire_images/streamSShot/stream-1479110563909.jpg
[type] => PUBLIC [status] => RECORDED [userId] => 1 [userHandle] => abhilekh
[channelId] => 2 [channelName] => Politics [likes] => 0 [views] => 0
[liveViewers] => 0
)
[2] => stdClass Object
( [id] => 3 [name] => Make America Great
[screenShotUrl] => http://52.77.190.221/SW/speaker_wire_images/streamSShot/stream-1479109601008.jpg
[type] => PUBLIC [status] => RECORDED [userId] => 2 [userHandle] => sudhanshu
[channelId] => 3 [channelName] => Fun [likes] => 0 [views] => 0
[liveViewers] => 0
)
[3] => stdClass Object
( [id] => 4 [name] => Make America Great
[screenShotUrl] => http://52.77.190.221/SW/speaker_wire_images/profiles/pic-130.jpg
[type] => PUBLIC [status] => RECORDED [userId] => 2 [userHandle] => sudhanshu
[channelId] => 4 [channelName] => Art [likes] => 0 [views] => 0
[liveViewers] => 0
)
) [last] => 1 [totalPages] => 1 [totalElements] => 4
[numberOfElements] => 4 [sort] => [first] => 1
[size] => 10 [number] => 0
)


I am new to the CodeIgniter. Please support with sample code.

Answer Source

if you are gonna call as API, no need to put in view.

Just echo like this in controller's method.

$nearme_values = $this->swsdk->nearMe($lat,$lon,$radius,$page,$size);
echo json_encode($nearme_values);

Displaying in View.

// in Controller.
    $data['nearme_values'] = $this->swsdk->nearMe($lat, $lon, $radius, $page, $size);
    $this->load->view('ur_view_file', $data);


// in view
foreach ($nearme_values->content as $nearme) {
            echo $nearme->id .' / '. $nearme->screenShotUrl .' / '. $nearme->type .'<br>';
        }