2

Now there are plenty of people with the same issue and their resolutions have not worked.

Problem

I have a list of Times coming back from a REST call. They are created exactly as they should, except I want them to be stored into a 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 FROM location');
    foreach($get_loc as $val){
    // call to Athena to get open appointments
        $res = getOpenAppointments($val->id, $val->provid, $startDate, $endDate, $apptTypeID);
        //print_r($res);

        // if we got some appointments back strip any that started before now
        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();
            for ($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 as $data) {
                $final_array[] = $data;
        }

        else
            $res; // something went wrong. return error message
    }
    echo json_encode($final_array);
}

Header.php

<script>
    var times = <?php getOpenAppts(); ?>;
    console.log(times); //Will display properly
</script>

enter image description here That is exactly how it should come back!

But.. When I run a console on the variable times (which is in the header making it a global variable. I get this. enter image description here

It should give me the exact same list that the console.log gave me.

What I Have tried

I ran:

PARSE.json(times);

No effect...

I did in PHP:

json_encode(json_decode($appts),true);

No effect...

What part of this process is incorrect?

7
  • Is that PHP what is inside the function getOpenAppts()? Commented Apr 7, 2017 at 17:26
  • 2
    Maybe somewhere else you are overriding it? Try changing its name to something unusual and keep the console.log Commented Apr 7, 2017 at 17:27
  • Just updated the PHP Commented Apr 7, 2017 at 17:27
  • @DiogoSgrillo I will do that right now!! Didnt think about that. Commented Apr 7, 2017 at 17:28
  • 2
    I don't think this has something to do with PHP at all! Commented Apr 7, 2017 at 17:29

1 Answer 1

1

You are using time as a global variable.

Since the console.log right after the declaration prints everything fine, you are probably overriding its value somewhere after.

Avoid the most you can global variables, they're evil :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.