Link to home
Start Free TrialLog in
Avatar of ltpitt
ltpitt

asked on

Javascript: correctly display arrays data in the same row

I have two arrays that look like this:

var payments = {"items":[{"id_decoder":"1","mesi_pagati":"2"},{"id_decoder":"2","mesi_pagati":"1"}]}

var decoders = {"items":[{"id_decoder":"1","decoder":"decoder1","id_cliente":"1","username":"decoder1","password":"decoder1","note":"decoder1"},{"id_decoder":"2","decoder":"decoder2","id_cliente":"2","username":"decoder2","password":"decoder2","note":"decoder2"},{"id_decoder":"3","decoder":"decoder3","id_cliente":"3","username":"decoder3","password":"decoder3","note":"decoder3"},{"id_decoder":"4","decoder":"decoder4","id_cliente":"4","username":"decoder4","password":"decoder4","note":"decoder4"},{"id_decoder":"5","decoder":"decoder5","id_cliente":"5","username":"decoder5","password":"decoder5","note":"decoder5"},{"id_decoder":"6","decoder":"decoder6","id_cliente":"5","username":"decoder6","password":"decoder6","note":"decoder6"}]}

Open in new window


They are results coming from my database.
The 1st one is from payments table and the second one is from decoders table.

I want to write a row with all the following information in it:

all data coming from decoders and the total months that were not paid.

For this I can simply display in the right <span> the current month number (debts are calculated on a yearly base). If the id_decoder has payments then I want to subtract the payment number from the span number.

I've tried various approaches but I am not able to obtain the result I want.

The thing that came closer is:

function caricaUtentiGestioneUtenti() {
    var decoder;
    var pagamenti;

    $.getJSON(serviceURL + 'getdecoder.php', function (dati_decoder) {
        decoder = dati_decoder
        $.getJSON(serviceURL + 'getpagamenti.php', function (dati_pagamenti) {
            pagamenti = dati_pagamenti

            $.each(decoder, function (i, item) { // For each client
                console.log(decoder.items);

                item.pays = [];
                $.each(pagamenti, function (i, pay) { // Check each payment

                    /* COMMENTED SINCE THE DATA IS NOT YET OK
                $('#autocomplete_debitori').append('<li><a href="#popupPagamento" data-rel="popup" data-position-to="window" data-transition="flip">' +
                                '<p class="username">Username: <strong>'+ gruppi.username  +'</strong></p>'+
                                '<p class="password">Password: <strong>'+ gruppi.password  +'</strong></p>'+
                                '<p class="nome" id="'+ gruppi.nome  +'">Nome: <strong>'+ gruppi.nome  +'</strong></p>'+
                                '<p class="cognome" id="'+ gruppi.cognome  +'">Cognome: <strong>'+ gruppi.cognome  +'</strong></p>'+
                                '<p class="id_decoder" id="'+ gruppi.id_decoder +'">Decoder: <strong>'+ gruppi.nome_decoder  +'</strong></p>'+
                                '<p class="nome_gruppo">Gruppo: <strong>'+ gruppi.nome_gruppo  +'</strong></p>'+
                                '<p class="note">Note: <strong>'+ gruppi.note +'</strong></p>'+
                                '<span class="ui-li-count ui-li-count-f">'+ gruppi.MesiNonPagati +'</span>'+
                               '<a href="modifica_cliente.html"></a></a></li>');

        $('#autocomplete_debitori').trigger('create');
        $('#autocomplete_debitori').listview('refresh');
*/

                    if (item.id_decoder == pay.id_decoder) { // If matching ID
                        item.pays.push(pay); // Add it to this client
                    }

                });
            });
        });

    });
}

Open in new window


I'd appreciate hints and suggestions about how to do this properly (maybe I should just study better queries or enhance my for and while skills?): I am learning to program and every suggestion is very very welcome.

Thanks a lot to everyone for your time and patience.
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Going to need some more explanation here. You talk about total months, yearly payments, month numbers etc, but your data doesn't resemble that. Your decode array contains items in this format:

"id_decoder": "1",
"decoder": "decoder1",
"id_cliente": "1",
"username": "decoder1",
"password": "decoder1",
"note": "decoder1"

and your payments array contains items in this format:

"id_decoder": "2",
"mesi_pagati": "1"

I don't see anything about months, years or payments!
Avatar of ltpitt
ltpitt

ASKER

I am sorry (my head is a mess after fighting with it for hours).

In the array coming from payments query you find id_decoder and number of total paid months :)
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ltpitt

ASKER

Perfect, brilliant, well commented.

Anything else?

Thanks.