0

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);
            }
        });
    }
});
Ryan
  • 767
  • 3
  • 9
  • 31
  • 1
    see this similar question http://stackoverflow.com/questions/777455/is-there-a-query-language-for-json – Daveo Jan 15 '12 at 10:32
  • 1
    The best way to do it is to fetch only the last four featured items from db in readdb.php. – Diode Jan 15 '12 at 12:53
  • @Diode you're absolutely right. I've gone for this option in the end, much, much simpler. – Ryan Jan 15 '12 at 13:39

2 Answers2

0

Sorry you do want the last four depending to which parameter?

The last four in your code seems to be the first 4 elements that matches Featured = Yes , so for example no date is considered.

Try write $. Each that way and then also don't use equal comparator, try to force a string search with f.e. indexOf. Try Debugging it first for function name and foe getting parameter with chrome, I write it without testing:

$.each(json.blogs, function(KEY, VALUE){
        var info = '<h2>' + this['title'] + '</h2><p>' + this['content'] + '</p><p      id="date">' + this['date'] + '</p>';
feat = VALUE.featured;
        if(feat.indexOf('Yes')!=0) {
            $('#featured').append(info);
        }
        else {$('#standard').append(info);
        }
    }); 
giuseppe
  • 827
  • 1
  • 9
  • 19
  • Yes, I would like to display the last four JSON objects with `"featured" = "yes"` or another way of looking at it, the four most recently added. – Ryan Jan 15 '12 at 10:20
  • Your code appears to remove everything that does not have the value 'yes'. – Ryan Jan 15 '12 at 10:21
  • have you tried it ? I don't think so :D you first retrieve the value of featured of each json object. the indexOf != 0 means that the word Yes was found in string this.featured, so you can append it to your div. – giuseppe Jan 15 '12 at 10:54
  • I understand that, but this doesn't give me the last four entries with the 'yes' parameter, it just gives me all the entries with the 'yes' parameter. – Ryan Jan 15 '12 at 10:57
  • In fact I told you this in first question. What conditions determines the " last " condition ? you want the latest comment left? you want the first 4 comment ( the oldest one ) ? if this json blog list comes ( and it's 99% probable ) from an SQL query , you should prefer to filter results directly in the query, that is more efficient. You may so give a range in your sql / data source, and show all results. If not you have to sort your json object first with your condition and then use an external counter to determine which are the 4 first featured. – giuseppe Jan 15 '12 at 14:03
0

The best way to do it is to fetch only the last four featured items from db in readdb.php.

Diode
  • 24,570
  • 8
  • 40
  • 51