2

I'm attempting to build an application that uses JSON generated using PHP which is then managed using JavaScript.

At the moment it is not working as expected and I'm not sure why.

Snippet of code:

$(document).ready(function(){

      var projects = <?= $json; ?>;
      $(window).bind('hashchange', function(){

         var potential = window.location.hash.substring(1);

         $.each(projects, function(i,project){

            if (project.permalink == potential)
            {
               alert(project.title);
               $('#title').text(project.title);
               $('#agency').text(project.agency);
            }
            else
            {
               alert('potential: ' + potential + '. project.permalink: ' + project.permalink);
            }
        });
      });
   });

So after changing the URL's hash section, I check to see if the permalink is listed in a block of JSON I have. (The permalink value). The alert says that project.permalink is undefined.

The PHP variable $json is created using PHP arrays and the json_encode() function.

The JSON is here:

   var projects = [{"hand-made-cards":{"id":"3","title":"Hand Made Cards","type":"","description":"","website_url":"http:\/\/northumberlhand-made.co.uk","agency":"-","permalink":"hand-made-cards","position":"1","added_by":"1","updated_by":"1","deleted_by":"0","published":"1","deleted":"0","datetime_added":"2012-06-03 16:42:14","datetime_updated":"2012-06-03 17:44:37","datetime_deleted":"0000-00-00 00:00:00","rel_id":"13"}},{"olive-design":{"id":"2","title":"Olive Design","type":"","description":"Olive Design website description. What happened here?","website_url":"http:\/\/olive-design.co.uk","agency":"Gardiner Richardson","permalink":"olive-design","position":"2","added_by":"1","updated_by":"1","deleted_by":"0","published":"1","deleted":"0","datetime_added":"2012-06-03 16:41:31","datetime_updated":"2012-06-03 17:43:50","datetime_deleted":"0000-00-00 00:00:00","rel_id":"14"}},{"riba-microsite":{"id":"1","title":"RIBA Microsite","type":"","description":"Some info abou the RIBA MS.","website_url":"http:\/\/ram.grtest.com","agency":"Gardiner Richardson","permalink":"riba-microsite","position":"3","added_by":"1","updated_by":"1","deleted_by":"0","published":"1","deleted":"0","datetime_added":"2012-06-03 16:40:55","datetime_updated":"2012-06-03 17:43:29","datetime_deleted":"0000-00-00 00:00:00","rel_id":"15"}}];

Updated JSON is here, as advised I have removed the additional object wrapping each object:

  var projects = [[{"id":"3","title":"Hand Made Cards","type":"","description":"","website_url":"http:\/\/northumberlhand-made.co.uk","agency":"-","permalink":"hand-made-cards","position":"1","added_by":"1","updated_by":"1","deleted_by":"0","published":"1","deleted":"0","datetime_added":"2012-06-03 16:42:14","datetime_updated":"2012-06-03 17:44:37","datetime_deleted":"0000-00-00 00:00:00","rel_id":"13"}],[{"id":"2","title":"Olive Design","type":"","description":"Olive Design website description. What happened here?","website_url":"http:\/\/olive-design.co.uk","agency":"Gardiner Richardson","permalink":"olive-design","position":"2","added_by":"1","updated_by":"1","deleted_by":"0","published":"1","deleted":"0","datetime_added":"2012-06-03 16:41:31","datetime_updated":"2012-06-03 17:43:50","datetime_deleted":"0000-00-00 00:00:00","rel_id":"14"}],[{"id":"1","title":"RIBA Microsite","type":"","description":"Some info abou the RIBA MS.","website_url":"http:\/\/ram.grtest.com","agency":"Gardiner Richardson","permalink":"riba-microsite","position":"3","added_by":"1","updated_by":"1","deleted_by":"0","published":"1","deleted":"0","datetime_added":"2012-06-03 16:40:55","datetime_updated":"2012-06-03 17:43:29","datetime_deleted":"0000-00-00 00:00:00","rel_id":"15"}]];
6
  • You do not refer to project.potential anywhere in your posted code. Commented Jun 3, 2012 at 20:12
  • Sorry, typo. Should (and now does) read project.permalink Commented Jun 3, 2012 at 20:13
  • 3
    projects is not JSON, it's a JavaScript array. Your problem has nothing to do with JSON. PHP might have produced JSON, but your problem does not seems to be related to JSON as a data exchange format. Commented Jun 3, 2012 at 20:19
  • You might also want to consider using var projects = JSON.parse(<?= $json; ?>); rather then executing the JSON literal. This way you can protect your code against unexpected things being injected and executed. Commented Jun 3, 2012 at 20:23
  • @FelixKling: apparently he's posting the result of his server-side JSON generation; PHP's json_encode() is routinely used to prepare data structures for inclusion in generated Javascript. Commented Jun 3, 2012 at 20:25

3 Answers 3

2

You actually have an extra level in your JSON that does not make much sense: each project is enclosed in an additional object (re-indented the JSON for clarity):

[
    {
        "hand-made-cards": {
            "id":"3",
            "title":"Hand Made Cards",
            "type":"",
            "description":"",
            "website_url":"http:\/\/northumberlhand-made.co.uk",
            "agency":"-",
            "permalink":"hand-made-cards",
            "position":"1",
            "added_by":"1",
            "updated_by":"1",
            "deleted_by":"0",
            "published":"1",
            "deleted":"0",
            "datetime_added":"2012-06-03 16:42:14",
            "datetime_updated":"2012-06-03 17:44:37",
            "datetime_deleted":"0000-00-00 00:00:00",
            "rel_id":"13"
        }
    },

    {
        "olive-design": {
            "id":"2",
            "title":"Olive Design",
            "type":"",
            "description":"Olive Design website description. What happened here?",
            "website_url":"http:\/\/olive-design.co.uk",
            "agency":"Gardiner Richardson",
            "permalink":"olive-design",
            "position":"2",
            "added_by":"1",
            "updated_by":"1",
            "deleted_by":"0",
            "published":"1",
            "deleted":"0",
            "datetime_added":"2012-06-03 16:41:31",
            "datetime_updated":"2012-06-03 17:43:50",
            "datetime_deleted":"0000-00-00 00:00:00",
            "rel_id":"14"
        }
    },
    ... (snip) ...

My guess is you've got the JSON generated incorrectly in your PHP code, and need to change it to generate each project on the top-level in the JSON array instead of wrapped in an additional object.

Sign up to request clarification or add additional context in comments.

4 Comments

I have now done this (JSON update in question). I'm still getting the same result though. (output: potential: olive-design. project.permalink: undefined potential: olive-design. project.permalink: undefined potential: olive-design. project.permalink: undefined)
It looks like you now have an array embedded in the array, which is still wrong.
Your output should start like this: {"hand-made-cards":{"id":"3","title":"Hand Made Cards",...
I did! Thank you very much. I had accidentally left the array in from when it was nested. I need a way to format the code so I can see this stuff more easily!
1

Take a look at your JSON data. You have a list of objects...

[{"hand-made-cards":{key:val,...}}, 
 {"olive-design":{key:val,...}}];

You really want the {key:val,...} as the members of the list projects. Currently you have a list of objects where each object has one key : value pair. You could reference the correct values by doing project.hand-made-cards.permalink or project.olive-design.permalink but that's probably not what you want to do.

Comments

0

according to your example json: projects[0]["hand-made-cars"].permalink is defined, but when you do the each on projects, then you "loop" over the array (where there is the 0 in my example), and you probably want to loop over projects[0], or what seems to be better to fix the json, and don't add the array arount the hash, so it'll look like:

projects = {"hand-made-cars":{...}, "other-cars": {...}}

Also if your $json is the output of json_encode, then have a look at http://php.net/manual/en/function.json-encode.php and you'll see that there's a 2nd parameter. You might want to use JSON_FORCE_OBJECT.

Comments

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.