Currently I have two HTML sections, 'standard' and 'featured'. My jQuery code splits the JSON objects into the two categories and displays them within the designated section, dependant on whether they are 'featured' or not. Currently however, every object with the value 'featured' is being displayed. How do I change this so that only the last four JSON objects with the value 'featured' are displayed within the section?
I'm guessing it's as simple as adding an if
statement, however my attempts so far have not gone well.
JSON is as follows:
{"blog":[
{
"title":"This is the title",
"content":"And this is the content",
"date":"01/12/11",
"featured":"Yes"
},
{
"title":"This is another title",
"content":"And this is some more content",
"date": "01/01/12",
"featured":"No"
},
{
// Continuation
}
]}
Current jQuery is:
$.getJSON("/TEST/readdb.php", function(json){
if(json.blogs.length > 0) {
$.each(json.blogs, function(){
var info = '<h2>' + this['title'] + '</h2><p>' + this['content'] + '</p><p id="date">' + this['date'] + '</p>';
if(this['featured'] == "Yes") {
$('#featured').append(info);
}
else {$('#standard').append(info);
}
});
}
});