Frist send data view page than json_encoded array data by Laravel. json_encode after var livetrde = '';
livetrdeObj = jQuery.parseJSON(livetrde);
Data formate almost done. I can't understand why does not show my chart data.
enter image description here
`for($i=0; $i<$different_days; $i=$i+$day){
$data[$i] = TradingProduct::where('group_product_id', $group_product_id)
->whereBetween(TradingProduct::raw('date(created_at)'), [Carbon::now()->subDays($i+$day), Carbon::now()->subDays($i)])
->orderBy('created_at', 'ASC')
->get();
$liveTrade = [];
$arrData = [];
foreach ($data as $key => $value) {
$arrData[$key] = $value;
$open = [];
$high = [];
$low = [];
$close = [];
$arrPrice = [];
$time = [];
$dd = 15;
$j = 0;
foreach ($arrData[$key] as $arrDatakey => $arr) {
if(!empty($arr['price'])){
$arrPrice[$arrDatakey] = $arr['price'];
$time[] = $arr['created_at'];
$open = reset($arrPrice);
$high = max($arrPrice);
$low = min($arrPrice);
$close = last($arrPrice);
$start_date_time = Carbon::now()->subDays($j);
$end_date_time = Carbon::now()->subDays($j+$dd);
$different_days_time = $start_date_time->diffInDays($end_date_time);
$currentTime = $start_date_time->subDays($different_days_time)->timestamp;
$j = $j+$dd;
}
}
$liveTrade[] = '{x: new Date('.$currentTime.'), y: ['.$open.','.$high.','.$low.','.$close.']}';
}
}`
The problem is here in creating the $liveTrade[] data. You are using Date() as string where it should create an object (Note that I'm already formatting the date in php itself in the format 'Y-m-d' which can be changed as per your needs). Update it to be like this:
$liveTrade[] = '{x: ' . (date('c', $currentTime)) .', y: ['.$open.','.$high.','.$low.','.$close.']}';
The better solution would be to push and array into $liveTrade[] and later in the end convert it into json like below:
// within the foreach loop you need to convert timestamp to date
$liveTrade[] = array('x' => date('c', $currentTime), 'y' => [$open, $high, $low, $close]);
// use date('D M d Y H:i:s O', $currentTime) to mimic JS date object
// ... Before returning response
$response = json_encode($liveTrade);
echo $response;
Related
I have getpunch method in script and PHP also what i want is pass the hours:minuts between in_time and current time
This is my script :
AttendanceAdapter.method('getPunch', function(successCallBack) {
var that = this;
var object = {};
object['date'] = this.getClientDate(new Date()).toISOString().slice(0, 19).replace('T', ' ');
object['offset'] = this.getClientGMTOffset();
var reqJson = JSON.stringify(object);
var callBackData = [];
callBackData['callBackData'] = [];
callBackData['callBackSuccess'] = successCallBack;
callBackData['callBackFail'] = 'getPunchFailCallBack';
this.customAction('getPunch','modules=attendance',reqJson,callBackData);
});
This is my PHP :
public function getPunch($req){
$date = $req->date;
$arr = explode(" ",$date);
$date = $arr[0];
$employee = $this->baseService->getElement('Employee',$this->getCurrentEmployeeId(),null,true);
//Find any open punch
$attendance = new Attendance();
$attendance->Load("employee = ? and DATE_FORMAT( in_time, '%Y-%m-%d' ) = ? and (out_time is NULL or out_time = '0000-00-00 00:00:00')",array($employee->id,$date));
if($attendance->employee == $employee->id){
//found an open punch
return new IceResponse(IceResponse::SUCCESS,$attendance);
}else{
return new IceResponse(IceResponse::SUCCESS,null);
}}
using this when user try to punch_out in that form showing in_time i want show in that form how many hours they worked when they try to punch_out
There are plenty of examples of using database records to disable certain dates (unavailable) using JSON and AJAX however for some reason, these are done in PHP. Is there reason why it isn't done in JAVA and how can I replace the PHP file with Java for the exact same result?
checkDates.php
<? php
$sql = "SELECT start from gbh_rooster_afwijkend WHERE dlnmrID = '".$_GET['dld'].
"'";
$res = mysql_query($sql) or die(mysql_error());
$checkDates = array();
while ($row = mysql_fetch_assoc($res)) {
$checkDate['start'] = $row['start'];
$checkDates[] = $checkDate;
}
echo json_encode($checkDates);
?>
Javascript
$.getJSON('script/php/afwijkendrooster/checkDates.php?dld='+ id, function(json){dates=json;});
function checkAvailability(mydate){
var myBadDates = dates;
var $return=true;
var $returnclass ="available";
$checkdate = $.datepicker.formatDate('dd-mm-yy', mydate);
// start loop
for(var x in myBadDates)
{
$myBadDates = new Array( myBadDates[x]['start']);
for(var i = 0; i < $myBadDates.length; i++)
if($myBadDates[i] == $checkdate)
{
$return = false;
$returnclass= "unavailable";
}
}
//end loop
return [$return,$returnclass];
}
PHP Code
$sql = "SELECT location FROM data";
$result=$conn->query($sql);
$data = array();
while($row = $result->fetch_assoc()){
$data[] = $row["location"];
}
Javascript
function getPoints(){
return [
var coordinateArray = <?php echo json_encode($data); ?>;
for(i-0;i<coordinateArray.length; i++({
var sep = coordinateArrays.split(',');
new google.maps.LatLng(sep[0], sep[1]);
}
];
}
Sample data in table:
location = 12.121212,13.131313 -> No quotes
data = "Hello"
author="Me"
Currently it breaks declaring the Array variable in javascript.Something is breaking there. Any help is appreciated
seems you have a name mismatch coordinateArray / coordinateArrays and and index problem try
var coordinateArray = <?php echo json_encode($data); ?>;
for(i=0; i<coordinateArray.length; i++) {
var sep = coordinateArray[i].split(',');
new google.maps.LatLng(sep[0], sep[1]);
}
and also problem with parentesis i++( and assignment i-1 instead of i++) and i=1
I have the following data in an SQL database
cname, ccode, colour
Great Britain, GB, 1
Italy, IT, 1
France, FR, 1
Spain, ES, 1
How can I create a JavaScript object like below?
var countries = {"GB":1, "IT":1, "FR":1, "ES":1}
So far, I have the following code;
PHP
$query2 = "SELECT ccode,colour FROM country_data WHERE 1";
$result2 = mysql_query($query2);
if (!$result2) {
die('Invalid query: ' . mysql_error());
}
$rows2 = array();
while($r2 = mysql_fetch_assoc($result2)) {
$rows2[] = $r2;
}
JS
var colours = '<?php print json_encode($rows2); ?>';
Which returns:
[{"ccode":"GB","colour":"1"},{"ccode":"IT","colour":"1"},{"ccode":"FR","colour":"1"},{"ccode":"ES","colour":"1"}]
change this code
while($r2 = mysql_fetch_assoc($result2)) {
$rows2[] = $r2;
}
to this one:
while($r2 = mysql_fetch_assoc($result2)) {
$rows2[$r2['ccode']] = $r2['colour'];
}
change php code as told in others answers too and it should be like
$rows2 = array();
while($r2 = mysql_fetch_assoc($result2)) {
$rows2[$r2['ccode']] = $r2['colour'];
}
if it does not work then try this:
$rows2 = array();
while($r2 = mysql_fetch_assoc($result2)) {
$rows2["'".$r2['ccode']."'"] = $r2['colour'];
}
if it also does not work then try this:
$rows2 = array();
while($r2 = mysql_fetch_assoc($result2)) {
$rows2[{$r2['ccode']}] = $r2['colour'];
}
I have an array of some objects and I want to somehow organize them
Here is my Array in JavaScript :
doctors = [
{"prop":"A","hour":"08:00"},
{"prop":"A","hour":"08:00"},
{"prop":"A","hour":"08:00"},
{"prop":"A","hour":"08:00"},
{"prop":"A","hour":"08:00"},
{"prop":"A","hour":"08:00"},
{"prop":"B","hour":"08:00"},
{"prop":"B","hour":"08:00"},
{"prop":"B","hour":"08:00"},
{"prop":"C","hour":"08:00"},
{"prop":"C","hour":"08:00"} ]
This array comes from an ajax call from my back-end(PHP) and I want to change it to be like this :
doctors = [
{"prop":"A",
"hour":"10:00"
"hour":"12:00"
"hour":"23:00"
"hour":"12:00"
"hour":"02:00"
"hour":"01:00"
},
{"prop":"B",
"hour":"18:00"
"hour":"03:00"
"hour":"01:00"
"hour":"08:00"
},
{"prop":"C",
"hour":"04:00"
"hour":"12:00"
"hour":"08:12"
"hour":"04:00"
},
]
How can I do that ? I mean extracting similar keys and compact all similar keys into one array of values???
And if it is importtant , here is my back-end in PHP :
if($result = $db->query($sql)){
$hours = array();
while($row = $result->fetch_object()){
$drid = $row->id;
$hour = $row->b_h;
$matid = $row->mid;
$month = $row->b_m;
$day = $row->b_d;
$hours[] = [
'prop'=>$drid.'-'.$matid.'-'.$month.'-'.$day,
'hour'=>$hour
];
}
echo (json_encode($hours)) ;
}
numbers are not real and I changed my real prop's to A B C for simplicity
And if it helps , later on , I want use each prop in a ng-repeat in angular js to be like this :
<li ng-repeat="hour in propA">hour</li>
OR :
<li ng-repeat="hour in propB">hour</li>
thanks
Edited to fit changed question
var doctors = [
{"prop":"A","hour":"08:00"},
{"prop":"A","hour":"08:10"},
{"prop":"A","hour":"08:20"},
{"prop":"A","hour":"08:30"},
{"prop":"A","hour":"08:40"},
{"prop":"A","hour":"08:50"},
{"prop":"B","hour":"09:00"},
{"prop":"B","hour":"09:10"},
{"prop":"B","hour":"09:20"},
{"prop":"C","hour":"10:00"},
{"prop":"C","hour":"10:10"} ];
var doctors_morph = {};
for(var i in doctors) {
if(typeof doctors_morph[doctors[i].prop] == 'undefined') {
doctors_morph[doctors[i].prop] = [];
}
doctors_morph[doctors[i].prop].push(doctors[i].hour);
}
That way you can access a doctor in way:
doctors_morph['A'];
doctors_morph['B'];
doctors_morph['C'];
Your Example result will look like:
doctors_morph = {
'A' : {
['8:00','8:10','8:20','8:30','8:40','8:50']
}
// etc
}
AS for PHP, to skip above you can use:
if($result = $db->query($sql)){
$doctors = array();
while($row = $result->fetch_object()){
$drid = $row->id;
$hour = $row->b_h;
$matid = $row->mid;
$month = $row->b_m;
$day = $row->b_d;
if(!isset($doctors[$drid.'-'.$matid.'-'.$month.'-'.$day]) OR !is_array($doctors[$drid.'-'.$matid.'-'.$month.'-'.$day]))
$doctors[$drid.'-'.$matid.'-'.$month.'-'.$day] = array();
$doctors[$drid.'-'.$matid.'-'.$month.'-'.$day][] = $hour;
}
echo (json_encode($doctors)) ;
}