0
<?php
date_default_timezone_set('America/Los_Angeles');
$time = date('Gi', time());
$day = date('l', time());
?>

<script type="text/javascript">
$(function() {
  $(".show_hide").click( function()
       {
        var locTime = <?php echo json_encode($time) ?>;
        var locDate = <?php echo json_encode($day) ?>;

$.getJSON( "url_to_json", function(data) {
    for (var i = 0; i < data.location[locDate].length; i++) { 
    console.log("sucess1");
    xr_mon= data.location.locDate[i];
    console.log("sucess2");
     if (locTime >= xr_mon.kai && locTime < xr_mon.guan ){
        console.log("cafe is open!");
        $('.xr').show(); 
        break;
        }


    else {
        console.log("cafe is closed");
        $('.xr').hide();
    }
    }
});
       }
  );
});

JSON

{ "location": 
{
    "Monday": [
    {"kai": 700, "guan": 1400},
    {"kai": 1700, "guan": 2100}
    ],

    "Tuesday": [
    {"kai": 700, "guan": 1400},
    {"kai": 1700, "guan": 2100}
    ],

    "Wednesday": [
    {"kai": 700, "guan": 1400},
    {"kai": 1700, "guan": 2100},
    {"kai": 2200, "guan": 2400},
    {"kai": 0, "guan": 200}
    ],

    "Thursday": [
    {"kai": 700, "guan": 1400},
    {"kai": 1700, "guan": 2100},
    {"kai": 2200, "guan": 2400},
    {"kai": 0, "guan": 200}
    ],

    "Friday": [
    {"kai": 700, "guan": 1400},
    {"kai": 1700, "guan": 2100},
    {"kai": 2200, "guan": 2400},
    {"kai": 0, "guan": 200}
    ],

    "Saturday": [
    {"kai": 1000, "guan": 1500},
    {"kai": 1700, "guan": 2100},
    {"kai": 2200, "guan": 2400},
    {"kai": 0, "guan": 200}
    ],

    "Sunday": [
    {"kai": 1000, "guan": 1500},
    {"kai": 1700, "guan": 2100},
    {"kai": 2200, "guan": 2400},
    {"kai": 0, "guan": 200}
    ]
}
}

where kai = opening hour and guan = closing hour

with locDate is pulled via php server side date, and i would like to replace the date generated with my JSON call string that has part where week day is.

from this

  for (var i = 0; i < data.location.'locDate'.length; i++)

to this

  for (var i = 0; i < data.location.Thursday.length; i++)

How do I do it in a correct way. There have been suggestion to use [locDate], but it would not replace the variable from php date data.

  • For the record, this is **not** JSON. These are native JavaScript objects. JSON is the _serialization_ of JavaScript objects--That's why there's an **N** for Notation. – Brad Christie Jun 27 '13 at 23:01
  • I'm trying to call an external JSON by using data.location.'locDate'.length but give me the current date via php server side. – user2510532 Jun 27 '13 at 23:09
  • A "JSON Call" would be the actual query using an AJAX (or alike) method. However, once you have it (and parsed) it's now a JavaScript _object_. – Brad Christie Jun 27 '13 at 23:11
  • I updated the original post, hope this clears up what I'm trying to do. – user2510532 Jun 27 '13 at 23:16
  • @user: I think you're missing what I'm after. When you see `"{x:1,y:2,z:3}"`, that's "JSON". However, when you're now referencing `foo.x`, `foo.y` and `foo.z` you're now using Javascript objects (the JSON servered its purpose--it sent the information and was deserialized back to JavaScript objects). That's all I was getting at. – Brad Christie Jun 27 '13 at 23:20
  • @Brad Christie got it – user2510532 Jun 27 '13 at 23:22
  • @FelixKling I tired the [ ], doesn't work, it comes out as "for (var i = 0; i < data.crossroads[locDate].length; i++)" in the Console instead of "for (var i = 0; i < data.crossroads.Thursday.length; i++)" as I'm trying to do. – user2510532 Jun 27 '13 at 23:24
  • That's correct. `locDate` is a *JavaScript variable*. The *value* of that variable will be `"Thursday"` (I guess) and `data.crossroads[locDate]` will then try to access the property `Thursday` of the object `data.crossroads`. The value doesn't get replaced in the source code. The property access and variable lookup happen at *run time*. Example: http://jsfiddle.net/drNLZ/. You should also read [MDN - Working with Objects](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects) to get a better understanding of objects. – Felix Kling Jun 27 '13 at 23:54

1 Answers1

2

Use bracket notation.

for (var i = 0; i < data.location[locDate].length; i++)

Another example:

thing = [1, 2, 3, 4, 5]
//The following lines do the same thing:
thing.pop()
thing['pop']()
tckmn
  • 57,719
  • 27
  • 114
  • 156