I want to use the MIN and MAX aht_value
value from my json_encoded
to use in my JavaScript function? How is this achievable?
thanks in advance.
Array:
[{
"username":"OXGOR",
"aht_value":"241",
"station":"B20"
}
{
"username":"AISAI2",
"aht_value":"199",
"station":"B21"
},
{
"username":"CAPAP3",
"aht_value":"NA",
"station":"B10"
}]
map.php - JS where everything will be output at the end
<script type="text/javascript">
$(document).ready(function() {
$('#aht').click(function(){
$.ajax({
type:"GET",
url : "show_aht.php",
data:{ } , // do I need to pass data if im GET ting?
dataType: 'json',
success : function(data){
//going through all DIVs only once with this loop
for(var i = 0; i < data.length; i++) { // loop over results
var divForResult = $('#desk_' + data[i]['station']); // look for div for this object
if(divForResult.length) { // if a div was found
divForResult.html(data[i]['aht_value']).css("background-color", colorMe(data[i]['aht_value']));
}//end if
}//end for
}//end success
});//end ajax
});//end click
});//end rdy
map.php
- JS: I want to put the MIN and MAX aht_value
here instead of 100 and 1800 but doesnt work
//get MIN value from the array
var min = Math.min.apply(null, a.map(function(x) {
var n = parseFloat(x.aht_value);
return isNaN(n) ? 9007199254740992 : n;
}));
//get MAX value from the array
var max = Math.max.apply(null, a.map(function(x) {
var n = parseFloat(x.aht_value);
return isNaN(n) ? 0 : n;
}));
//function for calculation of background color depending on aht_value
function conv(x){
return Math.floor((x - 100) / (1800 - 100) * 255);
}
//function for background color
function colorMe(v){
return "rgb(" + conv(v) + "," + (255-conv(v)) + ",0)";
}
</script>
show.php
: where I use json_encode
$result = array();
foreach ($memo as $username => $memodata) {
if (in_array($username, array_keys($user))) {
// Match username against the keys of $user (the usernames)
$userdata = $user[$username];
//if AHT is null give N/A as value
if (is_null($memodata['aht_value'])) {
$result[] = array( 'username' => $userdata['username'],
'aht_value' => 'NA',
'station' => $userdata['station']
);
}//end inner if
//else give the actual value of AHT without the decimals
else {
$result[] = array( 'username' => $userdata['username'],
'aht_value' => substr($memodata['aht_value'],0,-3),
'station' => $userdata['station']
);
echo json_encode($result);