Query in the send and use database in javascript

1

My difficulty is after sending to the java script how to separate each record:

Example: In my code I'm running this function:

$culturas=array();
        $resultado=mysqli_query($conexao,"select *from mandioca_iea where id_cult=1");
            while($cultura=mysqli_fetch_assoc($resultado))
            {
                array_push($culturas,$cultura);
            }
        echo json_encode($culturas);

And to receive the query:

$.post('envia.php', {nome: nome}, function(resposta) {}

So far so good, it sends something like this to Javascript:

On my console returned this: [{"iea_id":"1","man_ins_area_00":"0","man_ins_prod_00":"0","man_ins_area_05":"0"‌​, "man_ins_prod_05":"0","man_ins_area_10":"0","man_ins_prod_10":"0","man_ins_area_‌​13":"0", "man_ins_prod_13":"0","id_cid":"1","id_cult":"1"}, {"iea_id":"2","man_ins_area_00":"0","man_ins_prod_00":"0","man_ins_area_05":"0", "man_ins_prod_05":"0","man_ins_area_10":"0","man_ins_prod_10":"0", "man_ins_area_13":"0","man_ins_prod_13":"0","id_cid":"2","id_cult":"1"}]

Where: "iea_id": "1" and "iea_id": "2" are two different records.

My doubt is if there is any way to access the "records" strong> as a vector , for mounting a chart?

EDITING WITH THE ERROR:

Fullcodebeingused:

<scripttype="text/javascript" language="javascript">
$(function($) {
    $("#formulario").submit(function() {
        $("#status").html("<img src='loader.gif' alt='Enviando' />");
        $.post('envia.php', {nome: nome}, function(resposta) {
    for(var i=0; i<resposta.length; i++) {
        var registro = resposta[i];
        console.log(registro.iea_id);
    }
});
    });
});
</script>
    
asked by anonymous 29.01.2015 / 03:02

1 answer

4

You have an array of objects. The brackets define the array, and each pair of { and } defines an object (one for each record). You access the logs by looping in the array:

$.post('envia.php', {nome: nome}, function(resposta) {
    for(var i=0; i<resposta.length; i++) {
        var registro = resposta[i];
        console.log(registro.iea_id); // 1 na primeira passada, 2 na segunda
    }
});

It's simple, inside the loop you can access the fields by name / key.

As for the assembly of the chart, you can reprocess the data in JavaScript and reorganize it to go to the graphics generator, or to pass in the right format from PHP. It depends on whether or not you need complete data (and organized by record) in JS.

    
29.01.2015 / 03:26