I have stored xml file in mysql BLOB datatype,so now i want to get that data in xml or json format and need to display.
i tried like this,but getting binary values
$mqs = db_select('multiple','mq')
->fields('mq',array('data'))
->condition('tokenid', '1')
->execute();
foreach ($mqs as $row) {
$test = base64_encode($row->surveyid); //here junk binary data displaying
drupal_set_message("data ::".$test);
$xml = simplexml_load_string($test);
$json = json_encode($xml);
$array = json_decode($json,TRUE);
drupal_set_message("data::".$json);
}
actual data not displayed in XML or JSON formats?
any other alternatives?
Related
I need to generate 2 JSON files with PHP from 2 TCP commands in order to build a Highchart graph.
The first JSON file will contain the graph informations :
Graph Title, Graph Subtitle, Y axis title and a number related to the type of graph to be displayed.
The TCP string (from an IoT distant board) sends to the server the following arguments :
Name of JSON info file to built
Graph Title
Graph Subtitle
Graph Y axis title
Type of graph to be displayed
http:myserver/graphs/make-json_info.php?jsonfilename=S_007_nfo.json&graphtitle=S_007_Sensor&graphsubtitle=Office-Temp&Yaxistitle=Temp-Sensor&GraphType=3
How could i make a JSON file with a PHP file named 'make-json_info.php' from this TCP string ?
JSON file should look like this :
{
chart: {
type: '/*...*/'
},
xAxis: {/*...*/},
yAxis: {/*...*/},
plotOptions: {/*...*/}
/*...etc...*/
}
Regarding the 2sd JSON file needed to generate the graph, the IoT board sends every minutes another TCP string that contains :
Name of JSON data file to fill-in
Datetime stamp
Sensor value
http:myserver/graphs/make-json_data.php?S_007_data.json&datatime=1488271800000&value=22.5
Expected JSON file should be like this :
http://s529471052.onlinehome.fr/graphs/S_007_data.json
Can you show me how to write my two PHP files, these should generate 2 JSON files in order to built the expected Highcharts Graph afterwards ?
So far, i tried to extract informations from the JSON data file and JSON info file fro graph information is missing.
see jsfiddle below
http://jsfiddle.net/fbmohjgy/
I'm uncertain about many things in you question, but I will try my best.
The JSON file that contain the graph information... Does it only contain meta infromation about the graph (title, type, etc), or does the file also contains xAxis Data and yAxis Data...
From these two lines in your question, it seems like it may also contains the actual data:
xAxis: {/*...*/},
yAxis: {/*...*/},
Apologies if I got it all wrong, but this is what I came up with.
It is not tested, but should give you an idea of what to do:
Let's start with the second file/script first.
<?php
$file_name = $_GET['jsonfilename'];
$datatime = $_GET['datatime'];
$value = $_GET['value'];
$file_content = file_get_contents($file_name); //read data from json file
$temp_file_data = json_decode($file_content, true); //convert json string into php array
$temp_file_data['date'][] = [$datatime, $value]; //add your new values to the array
$new_file_content = json_encode($temp_file_data); //encode your new array as a json string
file_put_contents($file_name, $new_file_content); //save the new json string back to the file
?>
Now for the first PHP script. This script will use the json file from the above code to create the complete json file that the graph will use:
<?php
$file_name = $_GET['jsonfilename'];
$graphtitle = $_GET['graphtitle'];
$graphsubtitle= $_GET['graphsubtitle'];
$Yaxistitle= $_GET['Yaxistitle'];
$GraphType = $_GET['GraphType'];
$data_json = file_get_contents($file_name); //read data from json file
$data_array = json_decode($file_content, true); //convert json string into php array
$xAxis = []; //create array that will contain all xAxis values
$yAxis = []; //create array that will contain all yAxis values
for($i = 0; $i < sizeof($data_array['data']); $i++)
{
$xAxis[] = $data_array['data'][$i][0]; //add xAxis data from first json file to the xAxis array
$yAxis[] = $data_array['data'][$i][1]; //add yAxis data from first json file to the yAxis array
}
$json_array = []; //declare the array that will be converted to the final JSON string that the graph will use
$json_array['chart'] = ['type' => $GraphType];
$json_array['xAxis'] = $xAxis;
$json_array['yAxis'] = $yAxis;
$json_array['plotOptions'] = ['graphtitle' => $graphtitle,
'graphsubtitle' => $graphsubtitle,
'Yaxistitle' => $Yaxistitle];
?>
I'm trying to use the google charts api to present power (watt) over time. I have a sqlite database in which i store my data. A php script then gathers the data and outputs it into a json file. For the charts to work i need to keep a specific structure. Whenever i run the phpscript the json file gets overwritten with the new data. I need help with the php script to always output the data according to googles parameters.
I'm aiming to end up with an area chart that plots power on the y axis and the datestamps on the x axis.
I've read theese documents in googles documentation but i can't figure out how to output the data the way they do.
https://developers.google.com/chart/interactive/docs/php_example
https://developers.google.com/chart/interactive/docs/reference#dataparam
while ($res = $stmt->fetch(PDO::FETCH_ASSOC)){
$voltage[] = $res['voltage'];
$current[] = $res['current'];
$datestamp[] = $res['datestamp'];
}
$unixtimestamp = array();
foreach ($datestamp as $nebulosa){
$unixtimestamp[] = strtotime($nebulosa);
}
$power = array();
foreach ($voltage as $key => $door){
$power[] = $door * $current[$key];
}
//echo "<pre>", print_r($power, true), "</pre>";
$fp = fopen('data.json', 'w');
fwrite($fp, json_encode($power));
fwrite($fp, json_encode($datestamp));
fclose($fp);
The json file has this format after running the php script.
[3.468,5]["2016-10-14 14:56:22","2016-10-14 14:56:23"]
I need to update my JSON array in the file jsonData_0_123.json. I can get the data from the file and decode it, but I can't update the data in the file (in this case, the array answers)
$contents = file_get_contents('json_files/jsonData_0_123.json');
//Decode the JSON data into a PHP array.
$contentsDecoded = json_decode($contents, true);
//Update the decoded array
$contentsDecoded[1]["answers"] = "hello";
//Encode the array back into a JSON string.
$json = json_encode($contentsDecoded);
//Save the file.
file_put_contents('json_files/jsonData_0_123.json', $json);
I tried your code it is updating in json file with array answers.I found problem with file permission error for (jsonData_0_123.json) writing data into it.Otherwise it's working fine.
I'm using class to load html on one page, get HTML table content. The page is in windows-1250, so I'm using iconv to convert it to utf-8.
All this is done in one class, that I'm calling like this: $tableHtml = suplUpdater::getTableHtml(someParams,...);. When I echo that variable directly, everything looks nice. However, I want to parse the table rows with PHP DOMDocument to save them to database. Code looks like this:
$tableData = suplUpdater::getTableHtml(1400450400);
//echo($tableData);
$document = new DOMDocument();
$document->loadHTML($tableData);
$rows = $document->getElementsByTagName('tr');
$rows->item(0)->parentNode->removeChild($rows->item(0));//first row is just a header
$output = array();
foreach ($rows as $row) {
$currentOutput = array();
foreach ($row->childNodes as $cell) {
if ($cell->nodeType == 1) {
$currentOutput[] = $cell->nodeValue;
}
}
$output[] = $currentOutput;
}
When I do var_dump($output);, I get array, but it has messed up encoding. Where could be the problem? If needed, I can provide source table data.
EDIT:
When I copy table html to txt file, encoded in utf-8 and I do file_get_contents('tableHtml.txt'), I get the same result.
EDIT:
I have uploaded sample data here:http://anagmate.moxo.cz/data.txt
EDIT:
Screenshot of echo and var_dump is here:http://anagmate.moxo.cz/supl.png
Here is a JSON data example saved in a file data.txt
[
{"name":"yekky"},
{"name":"mussie"},
{"name":"jessecasicas"}
...// many rows
]
I would like to update the file so it will look like this:
[
{"name":"yekky","num":"1"},
{"name":"mussie","num":"2"},
{"name":"jessecasicas","num":"3"}
...// many rows
]
This is what I have got so far:
$json_data = file_get_contents('data.txt');
// What goes here?
And how do I count how many rows there are in the JSON tree?
Use json_decode() to decode the JSON data in a PHP array, manipulate the PHP array, then re-encode the data with json_encode().
For example:
$json_data = json_decode(file_get_contents('data.txt'), true);
for ($i = 0, $len = count($json_data); $i < $len; ++$i) {
$json_data[$i]['num'] = (string) ($i + 1);
}
file_put_contents('data.txt', json_encode($json_data));
You should use the PHP JSON library for such tasks. For example, after having read your JSON data from the file, do something like:
$json = json_decode($json_data);
$itemCount = count($json);
After having modified your JSON data, just encode it again:
$json_data = json_encode($json);
Also, you seem to want to beatify your JSON data. My advise is to just use whatever comes out of json_encode and save that to your file, because it will probably be the smallest (in file size) possible representation of your JSON data.
If you format it in a way readable for humans, you've got lots of extra spaces / tabs / line-breaks which increase file size and parsing time.
If you need to read it yourself, you can still beautify your JSON data by hand.
$file = 'data.txt';
$data = json_decode(file_get_contents($file));
foreach ($data as $key => $obj) {
$obj->num = (string)($key+1);
}
file_put_contents($file, json_encode($data));