0

I am working on a jQuery script that retrieves a PHP array from MySQL database and returns it as a JSON object. This all works correctly and I can output the raw JSON object, including the key.

The issue I am having is the way I am outputting it, which I currently do using jQuery's $.each.

Example: (Not the full script, just what is needed)

                if(data){
                $.each(data, function(key, data) {
                $('#div').append(  key + ' : '  + data  );

                });

        }

    } , 'json')

So at the moment the output is like so: Example

     Name: Tom Age: 20 Gender: Male

However I don't want all results of the array to be returned, which is what $.each is doing. I want to manually output different parts of the array, where and when I want.

I tried something like this so that I could just get the array result that has the Key of "name" but it did not work (one of my array keys is name):

    $('#div').append(  key + ' : '  + data['name']  );

Any suggestions?

EDIT: Making it a bit more clear:

Rather than looping through and outputting the entire array I want to output only select parts: e.g;

data.name data.age

Those for example will only output the 2 arrays with the name and age key, even if the array has 10 other results.

Array we are working with is like so, only 1 result, but with multiple values.:

    {Name: 'Tom' Age: 20 Gender: 'Male'}
1
  • console.log(data) and copy paste the console's output here. Commented Jan 15, 2013 at 5:16

4 Answers 4

1

What about:

$.each(data, function(key, data) {
    $('#div #span-'+key).html(data);
});

So if you have the keys name, age, gender, you need 3 spans with the ID: #span-name, #span-age, #span-gender and they will be filled in with the data. If you only need name and age, just delete your #span-gender span and it won't show.

Your html will be something like:

<div id="div">
    <span id="span-name"></span>
    <span id="span-age"></span>
</div>
1
  • Thats pretty much what I am looking for. Now I am able to select particular array results. Commented Jan 15, 2013 at 5:28
0

I think you want to do this:

$.each(data, function(key, val) {
   $('#div').html(key + ' : '  + val.name);
});

suggestion: use .html() it will let you to update the div with latest results .append() will append the result with previous results.

0

Use different variable names in the each . You are using data as the main object as well as second argument of callback. Also note that use of keys is case sensitive

Change the second to something else

$.each(data, function(key, item) {
                $('#div').append( item.Name  )

ALso not clear what your data looks like. I am assuming it is an array of objects like:

[ {Name: 'Tom' Age: 20 Gender: 'Male'}, {Name: 'Jane' Age: 20 Gender: 'Female'}]

If the data is only a single object only, and no array, you don't need to use $.each and just access the object directly using object notations

{Name: 'Tom' Age: 20 Gender: 'Male'}
alert( data.Name);
5
  • Right that. Unless and until we know how the array looks like, no luck in any solutions Commented Jan 15, 2013 at 5:07
  • The issue with that though is it still outputs all other results as undefined. Commented Jan 15, 2013 at 5:08
  • @JPDP no idea what you are talking about Commented Jan 15, 2013 at 5:12
  • I would do that, but I want to grab and output just the Name and Age at one point in the script and then gender at a later part in the script, without alerting as they will be added to a table. Commented Jan 15, 2013 at 5:12
  • alert() is simply a demo. Not sure what your data even looks like to help further Commented Jan 15, 2013 at 5:13
-1

In your jQuery code, do this.

if(data){
    var dec_data = jQuery.parseJSON(data[0]);
    //Loop over it now
    jQuery.each(dec_data, function(key, val){
        //Stick it in the div
        jQuery('div').append(key+' - '+val);
    });
}
7
  • Try it before you downvote! You're already json_encoding it, but you're not decoding it on jQuery side. Commented Jan 15, 2013 at 4:59
  • OP is trying to parse data that already exists in javascript, what does json_encode have to do with it? Commented Jan 15, 2013 at 4:59
  • Not encode, but decode on the jQuery side does have a lot to do with it. Commented Jan 15, 2013 at 5:00
  • Doing that provides the following Firebug error: "TypeError: e is null" Although from the looks of it that wont accomplish what I need, that still looks like it loops through and displays the entire array, rather than letting me select the parts I want. Commented Jan 15, 2013 at 5:02
  • Can you post the raw array over here? Because all of the answers assume your array is like this: Array( key1 => val1, key2 => val2...). If its not like this, then the above solutions will fail. Commented Jan 15, 2013 at 5:04

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.