How to parse datetime for use in highchart graph? - php

Here i got timestamp value
$user_array1[] = $val['timestamp'];
$graph_data1 ='';
if(is_array($user_array1))
{
$graph_data1 = implode(',',$user_array1);
}
Output when i print_r($graph_data1);
I got
,2014-05-26 00:43:45,2014-05-26 00:43:52,2014-05-26 00:43:592014-05-26
00:43:19,2014-05-26 00:43:24,2014-05-26 00:43:27,2014-05-26
00:43:30,2014-05-26 00:43:34,2014-05-26 00:43:37,2014-05-26
00:43:41,2014-05-26 00:43:45,2014-05-26 00:43:52,2014-05-26
00:43:59,2014-05-26 00:44:11
Highchart side :
categories: [<?php echo $graph_data1 ?> ]
it does not show any things.

In your current format of data, your string are considered malformed, and therefore incorrect for input in highcharts. You must format it correctly fist. Consider this example:
$graph_data1 = "'".implode("','", $user_array1)."'";
Should output something like this:
'2014-05-26 00:43:45','2014-05-26 00:43:45','2014-05-26 00:43:45' ... etc ...

Related

Get JSON from a URL by PHP

I have a URL that returns a JSON object like this:
{
"USD" : {"15m" : 7809.0, "last" : 7809.0, "buy" : 7809.85, "sell" : 7808.15, "symbol" : "$"},
"AUD" : {"15m" : 10321.42, "last" : 10321.42, "buy" : 10322.54, "sell" : 10320.3, "symbol" : "$"},
}
URL : https://blockchain.info/ticker
more info : https://blockchain.info/api/exchange_rates_api
I want to get all the data from the first line and echo it and to have it keep requesting it so its live
I have used the examples on git hub
https://github.com/blockchain/api-v1-client-php/blob/master/docs/rates.md
but it displays all of the data output and you have to refresh it to get it updated
please can some one point me in the right direction
ideally I would end up with something like
15 m Last Buy Sell
USD ($) 7794.87 7794.87 7795.72 7794.02
I have the table and data going to the table but its echoing the whole data set rather than the first line and also I dont know how to select individual fields
How can I do it through PHP?
What you need is a request php page, which will make:
1 - Get data from te site:
$data = file_get_contents('https://blockchain.info/ticker');
2 - decode the json
$decodedData = json_decode($data);
3 - Here you can access it using OOP:
var_dump($decodedData->USD);
The point here will be to retrieve data as you wish, you can mix it up with HTML in a table for example.
Then, you need a JS script, that will execute a function with setInterval, each few miliseconds. That function should make a request to a PHP page that you created earlier, get that data and change with the updated one.
This Should do it:
<?
$seconds = 5;
function get_live_quote($key){
$json_string = file_get_contents('https://blockchain.info/ticker');
$json_array = json_decode($json_string, TRUE);
$quote = $json_array[$key];
$keys = implode(" ",array_keys($quote));
$values = implode(" ", array_values ($quote));
return "$keys $values \n";
}
while(TRUE){
echo get_live_quote("USD");
sleep($seconds);
}
Save the preceding code to a file like "quote.php". Then from your terminal just run: php quote.php

Printing and parsing JSON variable in PHP

I've the following PHP code with a JSON variable...
$route_geometry_json = "{
\"route_geometry\": [
[44.911537, 7.671326],
[44.911481, 7.671462],
[44.911455, 7.671531],
[44.911434, 7.671602],
[44.911358, 7.671859],
[44.911273, 7.672175],
[44.911198, 7.672458],
[44.91113, 7.672617],
[44.911069, 7.67275],
[44.911003, 7.672821],
[44.910945, 7.672881],
[44.910869, 7.672954],
[44.910868, 7.673046],
[44.91091, 7.673109],
[44.91095, 7.67319],
[44.910964, 7.673266],
[44.910958, 7.673407],
[44.910955, 7.6735],
[44.910947, 7.673632],
[44.910922, 7.673871],
[44.910828, 7.674786],
[44.910711, 7.675816],
[44.910606, 7.676364],
[44.910467, 7.676322],
[44.910368, 7.676308],
[44.910051, 7.676253],
[44.9097, 7.676162],
[44.90944, 7.676041],
[44.909297, 7.675958],
[44.909174, 7.67583],
[44.909107, 7.675722],
[44.908993, 7.675583],
[44.908758, 7.675448],
[44.90796, 7.675037]
]
}";
print "Route geometry -->" + json_encode($route_geometry_json);
The print return "0": any suggestion / example?
I'd like also to extract / print the coords couples like
44.908993, 7.675583
44.908758, 7.675448
Any suggestion will be appreciated ...
Thanks
Cesare
Use this code:
$route_geometry = json_decode($route_geometry_json);
foreach ($route_geometry->route_geometry as $value) {
echo $value[0].', '.$value[1].'<br />';
}
You need to print_r(json_decode($route_geometry_json)) it and not json_encode
json_encode is for creating a JSON string. But since you already have a JSON string already, you need to Decode it to make it an Array/Object.
UPDATE
Your requirement
echo "Route geometry -->";
print_r(json_decode($route_geometry_json));
You cannot concat a String and an Object, so you were getting that Parse error.

Generate the set of data into Json instead of one big mixed Json

How can i create one Json with many small sets of data separated by comma?
Instead of one big Json enclosed by double curly brackets?
I do receive a Json and with php i do use foreach to loop over it, making a lot of data processing inside.
Then generate a new Json, just to avoid the data processing on the client side wich will be processed by angularjs ng-repeat.
All the json data is mixed into one big json set (inside double curly brackets)
My goal is to separate into small sets of data.
I can use the NrType property. In this script the NrType receives the last atribution and just the last received is available.
//The php script
$arr = json_decode($returnedJson); //The original json to be pre-processed
$processedData = "[]";
$processedJson = json_decode($processedData,true);
foreach($arr as $key=>$value) {
foreach($value as $vkey=>$vvalue) {
if( $value[$i]->NrType == 1 ) {
$VlMIni = $value[$i]->QttyInitial;
$VlMSub = $value[$i]->QttyPeriod + $value[$i]->QttyRealAfter;
$VlMRec = $value[$i]->RealValue;
$VlMTotal += $VlMesRece;
//much more data processing going on here ...
} elseif( $value[$i]->NrType == 2 ) {
.
.
.
//and much more data processing going on here ...
}
}
//simple data atribution here
$processedJson['labelDesIni'] = 'Instruments';
$processedJson['labelValueMIni'] = $lblVlMIni;
$processedJson['labelValuePIni'] = $lblVlPlIni;
$processedJson['labelValueAIni'] = $lblVlAIni;
$processedJson['labelValuePAIni'] = $lblVlPAIni;
$processedJson['labelValuePercInic'] = $lblVlPercInic;
$processedJson['labelValuePerc2Inic'] = $lblVlPerc2Inic;
//much more data atribution ...
echo json_encode($processedJson); //the new hgenerated Json
The generated Json :
{
labelDesI: "Inspection",
labelValueMI: "2357",
labelValuePlI: "3914066",
labelValueAI: "1389406",
labelValuePAI: "2431425",
labelValuePercI: 57.143691456656,
labelValuePerc2I: 35.497766261478,
labelDesR: "Instruments",
labelValueMR: "734.54",
labelValuePR: "819.14",
labelValueAR: "660.05",
labelValuePAR: "877.94",
labelValuePercR: 80.087,
labelValuePerc2R: 44.739,
labelDesAcfi: "Fiscalização",
labelValueMAcfi: "343",
labelValuePlAcfi: "29907",
labelValueAAcfi: "16718",
labelValuePAAcfi: "16493",
labelValuePercAcfi: 101.36421512157,
labelValuePerc2Acfi: 55.899956531916,
labelDesT: "Totals",
labelValueMT: 365.59,
labelValuePlT: 547.62,
labelValueAnT: 909.63,
labelValuePAnT: 957.63,
labelValuePercT: 22949,
labelValuePerc2T: 25065
}
The desired format Would be this :
{
label: "Inspection",
labelValue1: "2357",
labelValue2: "3914066",
labelValue3: "1389406",
labelValue4: "2431425",
labelValue5: 57.1456656,
labelValue6: 35.4961478
},
{
labelDesR: "Instruments",
labelValue1: "734.54",
labelValue2: "819.14",
labelValue3: "660.05",
labelValue4: "877.94",
labelValue5: 80.087,
labelValue6: 44.739
},
{
labelDesT: "Totals",
labelValue1: 365.59,
labelValue2: 547.62,
labelValue3: 909.63,
labelValue4: 957.63,
labelValue5: 22949,
labelValue6: 25065
}
Thank´s in advance
generate all of your object separately, create an array of these objects and json_encode the array:
$processedJsonElement[] = ['labelDesT' => "Totals", 'labelValue1' => $whatTheValueIs, . . .];
and add it to you main object:
$processedJson[] = $processedJsonElement;
do this for each section of Json you want to represent. Not sure how you're structuring your Foreach loop as your code doesn't match the output, but whatever you structure is whatever you will will output when you call json_encode.
Basically, you need to structure your foreach loop to be able to compartmentalize the objects you wish to represent as an array of json objects.

Attempting to plot the time on the x-axis of my line graph?

Can anyone help? I'm attempting to plot the time on the x-axis of my line graph. Completely new to JSON and PHP and having real problems with this.
At the moment y-axis depicts "reading":
d3.json('getdata.php',function (jsondata) {
var data = jsondata.map(function(d) { return d.reading; });
var x = d3.scale.linear().domain([0, data.length]).range([0, width]);
var y = d3.scale.linear().domain([0, d3.max(jsondata, function(d) {
return parseInt(d.reading); })]).range([height,0]);
The output from "getdata.php" is in the following format:
[{"reading":"10","Time":"2012-11-01 00:00:00"},{"reading":"10.2","Time":"2012-11-01 00:02:00"}......
I get a parse error if I change:
var data = jsondata.map(function(d) { return d.reading; });
to
var data = jsondata.map(function(d) { return d.Value; });
I know it may seem like a very simple question but how do I return both "reading" and "Time"?
Would also really appreciate any help with how to format/parse DateTime to depict Time only?
You actually don't have to do anything to return both reading and time, your JSON object is already in that form. Maybe if you saw it in another form, you'd understand what I mean:
[{"reading":"10","Time":"2012-11-01 00:00:00"},{"reading":"10.2","Time":"2012-11-01 00:02:00"}
This is how JavaScript will see it:
[
{
reading: "10",
Time: "2012-11-01 00:00:00"
},
{
reading: "10.2",
Time: "2012-11-01 00:02:00"
}
];
If you pass this into .data(), you can access the individual properties by d.reading and d.Time.
For your call to d3.max, I'd just use +d.reading to convert reading to an int - either that, or be sure to provide the radix: parseInt(d.reading, 10):

Fullcalendar.js allDay parameter returned from MySql using Codeigniter

I'm trying to get FullCalendar working on my Codeigniter app, but coming across a problem with the events showing as all day from my JSON feed.
I've found that setting "allDay":false fixes the problem (tested with a static json file) but when i store that value in a mysql DB, then return the data and run the JSON encode, it converts the False into a string, which cause the event to show as all day!
Does anybody know how i can store true or false in my DB (i'm currently storing as text) and return it to my json feed without it being a string? I've tried casting and converting but can't get anything to work!
Examples of my model/controller/json feed below;
Thanks in advance;
Model function
function show_installs()
{
return $this->db->query(
"SELECT id, client_name as title, start, end, concat('/planner/view_install/',id) as url
FROM installs WHERE completed != 2"
)->result();
}
Controller function
function json_installs()
{
$this->load->model('installs_model');
$data = array();
if($query = $this->installs_model->show_installs())
{
$data = $query;
}
$json = json_encode($data);
echo $json;
}
JSON Feed (That doesnt work because of the " around false)
[
{
"id":"18",
"title":"John",
"allDay":"false",
"start":1339585200,
"end":1339592400,
"url":"\/planner\/view_install\/18"
},
{
"id":"19",
"title":"Mike",
"allDay":"false",
"start":"1339585200",
"end":"1339592400",
"url":"\/planner\/view_install\/19"
}
]
Any help would be really appreciated - i just need to get those bloomin quote marks off from false/true and it would work perfectly!
Thanks
Why don't you change the column type in your DB to be boolean instead of text? You could also parse your $json object to convert all booleans_as_text to booleans before you set it in the calendar - quick googling found me this:
for(var i=0; i<$json.length; i++)
$json[i]=/^true$/i.test($json[i]);
You could also set the allDayDefault to false to avoid set allday: false in each event.

Categories