how to get json out put to html table - php

I tried to print the json out put in my php page from facebook graph url .
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<?php
$jsonurl = "https://graph.facebook.com/search?q=%23IIFA&access_token=TOKEN_GOES_HERE";
$json = file_get_contents($jsonurl);
$json_output = json_encode($json);
echo $json_output;
?>
Out put :
"data":[
{
"id":"1003224",
"from":{
"id":"1000042242",
"name":"abc"
},
"message":"#abc",
"privacy":{
"value":""
},
"type":"status",
"created_time":"2014-06-09T09:49:58+0000",
"updated_time":"2014-06-09T09:49:58+0000"
}
],
This page give out put as json data and i want to add the data into html layout /table
Please advice me how to proceed
UPDATE
$json=array();
foreach ($json_output as $json_result) {
$json[] = array(
'value' => $json_result["name"]
'value' => $json_result["message"],
);
echo $json_result['name'];
echo $json_result['message'];
}

You're not currently navigating the JSON properly.
if your output looks like this:
"data":[
{
"id":"1003224",
"from":{
"id":"1000042242",
"name":"abc"
},
"message":"#abc",
"privacy":{
"value":""
},
"type":"status",
"created_time":"2014-06-09T09:49:58+0000",
"updated_time":"2014-06-09T09:49:58+0000"
}
],
JSON does not search all of its levels to find the thing you're asking for, you need to tell it exactly where to look. So, for instance, in order to get "abc" you need to echo
$json_result["data"][0]["from"]["name"]
and if you want to get "#abc" you need to echo
$json_result["data"][0]["message"]
EDIT:
try this:
$json=array();
foreach ($json_output["data"] as $json_result) {
$json[] = array(
'value' => $json_result["from"]["name"],
'value' => $json_result["message"]
);
echo $json_result["from"]['name'];
}

Related

How to convert JSON to rich HTML with relevant tags [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm working with a charity who need their own specific CMS to post blogs. I have set up QuillJS which works well and output JSON.
How do I convert JSON data back to HTML with the relevant tags? I need the HTML tags to be shown too such as '<p>Hello</p>'. I can get the JSON data via php mysql but don't know how to show the tags.
For example how to I convert the following
JSON
{
"time" : 1589493865899,
"blocks" : [
{
"type" : "header",
"data" : {
"text" : "Editor.js",
"level" : 2
}
},
{
"type" : "paragraph",
"data" : {
"text" : "Hey. Meet the new Editor. On this page you can see it in action — try to edit this text."
}
}
],
"version" : "2.17.0"
}
To
HTML
<html>
<h2>EditorJS</h2>
<p>Hey. Meet the new Editor. On this page you can see it in action — try to edit this text.</p>
</html>
Many thanks in advance for any help!
Here's a complete solution. Each step is mentioned in the comments:
<?php
$jsonData = '{
"time" : 1589493865899,
"blocks" : [
{
"type" : "header",
"data" : {
"text" : "Editor.js",
"level" : 2
}
},
{
"type" : "paragraph",
"data" : {
"text" : "Hey. Meet the new Editor. On this page you can see it in action — try to edit this text."
}
}
],
"version" : "2.17.0"
}';
//decode the JSON
$data = json_decode($jsonData);
//loop through the blocks
foreach ($data->blocks as $block)
{
$start = "";
$end = "";
//detect the element type and set the HTML tag string
switch ($block->type)
{
case "header":
$start = "<h2>";
$end = "</h2>";
break;
case "paragraph":
$start = "<p>";
$end = "</p>";
break;
}
//output the final HTML for that block
echo $start.$block->data->text.$end;
}
Demo: http://sandbox.onlinephpfunctions.com/code/ab862de1113d8744bc2d9463f7a7df2496491110
So you need to first decode the JSON so php can use it as an array and loop through foreach to display.
$someJSON = '{
"time" : 1589493865899,
"blocks" : [
{
"type" : "header",
"data" : {
"text" : "Editor.js",
"level" : 2
}
},
{
"type" : "paragraph",
"data" : {
"text" : "Hey. Meet the new Editor. On this page you can see it in action — try to edit this text."
}
}
],
"version" : "2.17.0"
}';
Now we decode and display
$someData = json_decode($someJSON, true);
//--> Create an empty variable to hold the display text...
$output = null;
//--> Run foreach loop, we can see the associative keys in the array, this gives
//--> us everything we need to pull the data out and display it properly...
//--> loop through the converted arrays first child 'blocks' and get the values
foreach ($someData['blocks'] as $value) {
//--> If the 'type' === 'header' wrap value -> ['data'] -> ['text'] in <h2> tag
if($value['type'] === "header"){
$output .= '<h2>'.$value['data']['text'].'</h2>';
}
//--> If the 'type' === 'paragraph' wrap value -> ['data'] -> ['text'] in <p> tag
if($value['type'] === "paragraph"){
$output .= '<p>'.$value['data']['text'].'</p>';
}
}
In your HTML output the variable within php tags to display the concatenated HTML held within $output
<html>
<div id="my_id">
<span class="my_class">
<?=$output?> or <?php echo $output; ?>
</span>
</div>
</html>
http://sandbox.onlinephpfunctions.com/code/e8fdb5b84af5346d640e92e6788e5c2836b9ad07

Print JSON Content in PHP

I'd like to print this json
i can print the first part with the actions
PHP:
$print= json_decode($output_data);
foreach($print->actions as $mydata)
{
echo $mydata->ID.'<br><br>';
}
JSON:
{
"actions": [
{
"ID":"Search",
"name":"Select"
}
],
"content": {
"TEST": "false",
"nickname": "brad"
}
}
but i cant print the content part to show the TEST and nickname content.
Can you please help me ?
Thanks
In the assumption you Json format is like this:
{
"actions" : [{
"ID" : "Search",
"name" : "Select"
}
],
"content" : {
"TEST" : "false",
"nickmane" : "brad"
}
}
You can do it like in next example:
$print= json_decode($output_data);
// Print all actions IDs:
foreach($print->actions as $mydata)
{
echo $mydata->ID . '<br><br>';
}
// Print TEST.
echo "TEST: " . $print->content->TEST . " Nickname: " . $print->content->nickname;
Also, if you need to dump all the JSON, try this:
$print = json_decode($output_data);
print_r($print);
Or this:
$print = json_decode($output_data);
$output = print_r($print, true);
echo $output;
You can read more about print_r() and other ways to dump variables here: http://php.net/manual/es/function.print-r.php
You can do it quite simply like this:
$print= json_decode($output_data);
echo $print->content->TEST;
echo "<br/>";
echo $print->content->nickmane;
The difference from the "actions" is that "actions" is an array whereas "content" is an object, so you don't need a loop.
Click here to run a demo of this code
Your JSON is not a valid format. I appears your JSON should be the following to do what you want:
{
"actions" : [{
"ID" : "Search",
"name" : "Select"
}
],
"content" : {
"TEST" : "false",
"nickmane" : "brad"
}
}

Parsing JSON with json_decode in PHP

This is the JSON data I get from our ticket-system. I would like to parse it with PHP and save the data in a database to create statistics and a dashboard so everyone can see how many tickets are open and the latest closed tickets.
I can read some of the data but not everything.
{
"address":"belgium",
"workers":{
"peter":{
"worker":"peter",
"open_close_time":"45.6 T/h",
"closed_tickets":841,
"open_tickets":7,
"last_checkin":1498768133,
"days_too_late":0
},
"mark":{
"worker":"mark",
"open_close_time":"45.9 T/h",
"closed_tickets":764,
"open_tickets":2,
"last_checkin":1498768189,
"days_too_late":0
},
"walter":{
"worker":"walter",
"open_close_time":"20.0 T/h",
"closed_tickets":595,
"open_tickets":4,
"last_checkin":1498767862,
"days_too_late":0
}
},
"total_tickets":2213,
"tickets":[
{
"id":2906444760,
"client":"297",
"processed":0
},
{
"id":2260,
"client":"121",
"processed":0
},
{
"id":2424,
"client":"45",
"processed":0
}
],
"last_closed_tickets":[
{
"id":2259,
"client":"341",
"closed_on":"2017-06-25T10:11:00.000Z"
},
{
"id":2258,
"client":"48",
"closed_on":"2017-06-20T18:37:03.000Z"
}
],
"settings":{
"address":"belgium",
"email":"",
"daily_stats":0
},
"open_close_time":"161.1 T/h",
"avgopen_close_time":123298,
"ticket_time":"27.1 T/h",
"stats":{
"time":1498768200087,
"newest_ticket":1498768189000,
"closed_tickets":2200,
"open_tickets":13,
"active_workers":3
},
"avg_paid_tickets":64.55,
"avg_afterservice_tickets":35.45
}
This is the PHP code I tried to get the names of the worker but this doesn't work.
<?php
$string = file_get_contents("example.json");
$json = json_decode($string, true);
echo $json['address'];
foreach($json->workers->new as $entry) {
echo $entry->worker;
}
?>
If I try it like here below it works but then I've got to change the code everytime another employee starts.
echo $json['workers']['mark']['closed_tickets'];
<?php
$string = file_get_contents("example.json");
$json = json_decode($string, true);
foreach($json['workers'] as $entry){
echo $entry['worker'] . " has " . $entry['open_tickets'] . " tickets" . "\n";
}
?>

How to make auto complete form in cakephp?

I am trying to make an auto complete function in CakePHP but did not succeed. I tried the following code.
public function find() {
if ($this->request->is('ajax')) {
$this->autoRender = false;
$country_name = $this->request->data['Country']['name'];
$results = $this->Country->find('all', array(
'conditions' => array('Country.name LIKE ' => '%' . $country_name . '%'),
'recursive' => -1
));
foreach($results as $result) {
echo $result['Country']['name'] . "\n";
}
echo json_encode($results);
}
}
// Form and jquery
<?php
echo $this->Form->create('Country', array('action' => 'find'));
echo $this->Form->input('name',array('id' => 'Autocomplete'));
echo $this->Form->submit();
echo $this->Form->end();
?>
<script type="text/javascript">
$(document).ready(function($){
$('#Autocomplete').autocomplete({
source:'/countries/find',
minLength:2
});
});
</script>
foreach($results as $result) {
echo $result['Country']['name'] . "\n";
}
Breaks your JSON structure.
Keep in mind that autocomplete by default expects "label" and value keys in your JSON table, so all the script should do after fetching DB records is:
$resultArr = array();
foreach($results as $result) {
$resultArr[] = array('label' =>$result['Country']['name'] , 'value' => $result['Country']['name'] );
}
echo json_encode($resultArr);
exit(); // may not be necessary, just make sure the view is not rendered
Also, I would create the URL to your datasource in the jQuery setup by
source:'<?=$this->Html->url(array("controller" => "countries","action"=> "find")); ?>',
And try to comment-out (just to make sure if the condition is not met by the request when autocomplete makes its call)
if ($this->request->is('ajax')) {
condition

Json Decode Twitch Api PHP

I am trying to get propriety of Json Decoded string of twitch tv
$hue = file_get_contents('https://api.twitch.tv/kraken/streams/?channel=starladder1');
$hue = json_decode($hue);
print_r($hue->display_name);
but it doesnt work tryed almost everything please help
Try following code:
<?php
$hue = file_get_contents('https://api.twitch.tv/kraken/streams/?channel=starladder1');
$hue1 = json_decode($hue, TRUE);
foreach ($hue1 as $data)
{
foreach ($data as $datas) {
echo ($datas['channel']['display_name']."<br/>");
}
}
?>
the reason why it is not working is because you try to access "display_name" directly without analyzing the structure of the object.
Try this:
print_r($hue->streams[0]->channel->display_name);
You see that streams begins with an "[" which means that its elements are accessed like an array
Your object really looks like this, and this helps you to understand the structure better:
{
"streams":[{
"_id":10954982848,
"game":"Dota 2",
"viewers":11918,
"_links":{
"self":"https://api.twitch.tv/kraken/streams/starladder1"
},
"preview":{
"small":"http://static-cdn.jtvnw.net/previews-ttv/live_user_starladder1-80x50.jpg",
"medium":"http://static-cdn.jtvnw.net/previews-ttv/live_user_starladder1-320x200.jpg",
"large":"http://static-cdn.jtvnw.net/previews-ttv/live_user_starladder1-640x400.jpg",
"template":"http://static-cdn.jtvnw.net/previews-ttv/live_user_starladder1-{width}x{height}.jpg"
},
"channel":{
"_links":{
"self":"http://api.twitch.tv/kraken/channels/starladder1",
"follows":"http://api.twitch.tv/kraken/channels/starladder1/follows",
"commercial":"http://api.twitch.tv/kraken/channels/starladder1/commercial",
"stream_key":"http://api.twitch.tv/kraken/channels/starladder1/stream_key",
"chat":"http://api.twitch.tv/kraken/chat/starladder1",
"features":"http://api.twitch.tv/kraken/channels/starladder1/features",
"subscriptions":"http://api.twitch.tv/kraken/channels/starladder1/subscriptions",
"editors":"http://api.twitch.tv/kraken/channels/starladder1/editors",
"videos":"http://api.twitch.tv/kraken/channels/starladder1/videos",
"teams":"http://api.twitch.tv/kraken/channels/starladder1/teams"
},
"background":null,
"banner":null,
"display_name":"starladder1",
"game":"Dota 2",
"logo":"http://static-cdn.jtvnw.net/jtv_user_pictures/starladder1-profile_image-557367f831a49ebb-300x300.png",
"mature":false,
"status":"NewBee vs LGD-Gaming 1:0 # WEC Lan-Finals Day 2 by v1lat",
"url":"http://www.twitch.tv/starladder1",
"video_banner":"http://static-cdn.jtvnw.net/jtv_user_pictures/starladder1-channel_offline_image-c29311bb34830472-640x360.png",
"_id":28633177,
"name":"starladder1",
"created_at":"2012-03-01T18:05:14Z",
"updated_at":"2014-09-06T06:59:23Z",
"abuse_reported":null,
"delay":0,
"followers":118574,
"profile_banner":null,
"profile_banner_background_color":null,
"views":186419614,"language":"en"
}
}],
"_total":1,
"_links":{
"self":"https://api.twitch.tv/kraken/streams?channel=starladder1&limit=25&offset=0",
"next":"https://api.twitch.tv/kraken/streams?channel=starladder1&limit=25&offset=25",
"featured":"https://api.twitch.tv/kraken/streams/featured",
"summary":"https://api.twitch.tv/kraken/streams/summary",
"followed":"https://api.twitch.tv/kraken/streams/followed"
}
}
Here is code for you:
$hue = json_decode(file_get_contents('https://api.twitch.tv/kraken/channels/starladder1'));
echo "Name :" .$hue->display_name;
You just do wrong url to fetch

Categories