PHP Array to Javascript Object no array after console.log -
now there plenty of people same issue , resolutions have not worked.
problem
i have list of times coming rest call. created should, except want them stored javascript array.
php
function getopenappts() { global $wpdb; $final_array = ""; $td = date('m/d/y'); $datetime = new datetime('tomorrow'); $tm = $datetime->format('y-m-d'); $startdate = $td; $enddate = $tm; $appttypeid = "23"; $get_loc = $wpdb->get_results('select provid,id location'); foreach($get_loc $val){ // call athena open appointments $res = getopenappointments($val->id, $val->provid, $startdate, $enddate, $appttypeid); //print_r($res); // if got appointments strip started before if (array_key_exists('totalcount', $res) && $res['appointments'] > 0) { $tzstr = "america/los_angeles"; $tzobject = new datetimezone($tzstr); $nowdt = new datetime(); $nowdt->settimezone($tzobject); //print_r($nowdt); //print_r("\n"); $appts = array(); ($i = 0; $i < count($res['appointments']); $i++) { $apptdt = new datetime($res['appointments'][$i]['date']." ".$res['appointments'][$i]['starttime'], $tzobject); //print_r($apptdt); //print_r("\n"); if ($nowdt < $apptdt) $appts[] = $res['appointments'][$i]; } } if (count($appts) > 0) foreach($appts $data) { $final_array[] = $data; } else $res; // went wrong. return error message } echo json_encode($final_array); }
header.php
<script> var times = <?php getopenappts(); ?>; console.log(times); //will display </script>
but.. when run console on variable times (which in header making global variable. this.
it should give me exact same list console.log gave me.
what have tried
i ran:
parse.json(times);
no effect...
i did in php:
json_encode(json_decode($appts),true);
no effect...
what part of process incorrect?
you using time
global variable.
since console.log
right after declaration prints fine, overriding value somewhere after.
avoid can global variables, they're evil :)
Comments
Post a Comment