1

I'm currently creating a newsfeed on my website. A PHP script is generating JSON after a SELECT query in my database. When I print_r() the result of this query, I get the following :

Array ( 
        [0] => Array ( 
                       [nom] => Monsieur 
                       [0] => Monsieur 
                       [prenom] => X 
                       [1] => X 
                       [id_promotion] => 17 
                       [2] => 17 
                       [id_commercant] => 236 
                       [3] => 236
                       [article] => article test
                       [4] => article test 
                       [rubrique] => 4 
                       [5] => 4 
                       [date_debut] => 2015-06-17 
                       [6] => 2015-06-17 
                       [date_fin] => 2015-06-25 
                       [7] => 2015-06-25 
                       [prix_origine] => 25 
                       [8] => 25 
                       [prix_promotion] => 20 
                       [9] => 20
                 )
        [1] => Array ( 
                       [nom] => Monsieur 
                       [0] => Monsieur 
                       [prenom] => X 
                       [1] => X 
                       [id_promotion] => 18 
                       [2] => 18 
                       [id_commercant] => 
                       236 [3] => 236 
                       [article] => article test2 
                       [4] => article test2 
                       [rubrique] => 4 
                       [5] => 4 
                       [date_debut] => 2015-06-18 
                       [6] => 2015-06-18 
                       [date_fin] => 2015-06-23 
                       [7] => 2015-06-23 
                       [prix_origine] => 30 
                       [8] => 30 
                       [prix_promotion] => 10 
                       [9] => 10 
             ) 
); 

The JSON output is the following :

[
{
    "0": "Monsieur",
    "1": "X",
    "2": "17",
    "3": "236",
    "4": "article test",
    "5": "4",
    "6": "2015-06-17",
    "7": "2015-06-25",
    "8": "25",
    "9": "20",
    "nom": "Monsieur",
    "prenom": "X",
    "id_promotion": "17",
    "id_commercant": "236",
    "article": "article test",
    "rubrique": "4",
    "date_debut": "2015-06-17",
    "date_fin": "2015-06-25",
    "prix_origine": "25",
    "prix_promotion": "20"
},
{
    "0": "Monsieur",
    "1": "X",
    "2": "18",
    "3": "236",
    "4": "article test2",
    "5": "4",
    "6": "2015-06-18",
    "7": "2015-06-23",
    "8": "30",
    "9": "10",
    "nom": "Monsieur",
    "prenom": "X",
    "id_promotion": "18",
    "id_commercant": "236",
    "article": "article test2",
    "rubrique": "4",
    "date_debut": "2015-06-18",
    "date_fin": "2015-06-23",
    "prix_origine": "30",
    "prix_promotion": "10"
}

]

MY PROBLEM : I'm then trying to get the information I want through this script :

 $(document).ready(function (){
$.getJSON("newsfeed.php", function(result){
    $.each(result, function(i, field){
        $("#news").html(field.nom + " " + field.prenom + " vous propose " + field.article + " à " + field.prix_origine + "€ au lieu de " + field.prix_promotion + "€ ");
        console.log(field);
    });
  });
});

It results in showing the second array, but I don't know how I can chose the array I want to use. If someone has any informations in order to help me, I would really appreciate.

6
  • Of course your getting the second array, you're overwriting it because you are looping with $.each Commented Jun 18, 2015 at 12:42
  • I see where the problem was so. But how can I fix it ? Commented Jun 18, 2015 at 12:45
  • Put this in your each: if(i == 0) { //first array } else { //second array } Commented Jun 18, 2015 at 12:47
  • The problem with your solution is I will only be able to use 2 arrays, or maybe I'm wrong ? What if tomorrow, I decide to add 1, 2 or more news to show, Commented Jun 18, 2015 at 12:49
  • 1
    Then you need to mention that in your question, have a look at Uchiha's answer. Commented Jun 18, 2015 at 12:50

2 Answers 2

3

Try using as

htmldata = '';
$.each(result, function(key){
        htmldata += result[key].nom + " " + result[key].prenom + " vous propose " + result[key].article + " à " + result[key].prix_origine + "€ au lieu de " + result[key].prix_promotion + "€ ";
});
$("#news").html(htmldata);
Sign up to request clarification or add additional context in comments.

4 Comments

Because field contains the object with nom etc.
Check my updated result @Rameleu. Sorry I've been in too much hurry to post it as an answer
So if I want to show the first array, I have to replace key by an int am I right ?
Yes you have to replace your key by i and its going to work for you
0

html() function removes the current contents of the HTML element, and replaces it with the value passed. So effectively, you are rewriting the previous value on every iteration. That is why, you will always get the last value of the array in the output. You may want to use the append() function instead if you want to show all values :

    $.each(result, function(i, field){
        $("#news").append("<p>"+field.nom + " " + field.prenom + " vous propose " + field.article + " à " + field.prix_origine + "€ au lieu de " + field.prix_promotion + "€ "+"</p>");
        console.log(field);
    });
});

1 Comment

Thank you for this, I knew but I was using html() in order to see if the javascript was launched or if there was a problem. Anyway thank you for this answer !

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.