Error with array passing in foreach

Asked

Viewed 136 times

0

function preencherJason() {
    document.getElementById('json_prod').value = '';
    var arrayProduto = [];
    var table = $('#products_table');
    table.find('tr').each(function() {
        var nodesTd = $(this).children();
        var array = [];
        $.each(nodesTd, function() {
            var txt = $(this).text();
            array.push(txt);
        });
        if (array[0] !== 'Nome do Produto') {
            var id = array[0].split('-');
            id = id[0].trim();
            var produto = array[0].split('-');
            array[1] = document.getElementById('quant[' + id + ']').value;
            array[3] = $('input[name="servico[' + id + ']"]').val();
            var servico = array[3].split('-');
            array[4] = $('input[name="vlr_servico[' + id + ']"]').val();
            arrayProduto.push('{"produto":"' + produto[0].trim() + '",' +
                '"qt":"' + array[1] + '",' +
                '"vlr_unitario":"' + array[2].replace(/[ R|$|.]/gi, '').replace(/[,]/gi, '.') + '",' +
                '"servico":"' + servico[0].trim() + '",' +
                '"vlr_servico":"' + array[4].replace(/[ R|$|.]/gi, '').replace(/[,]/gi, '.') + '",' +
                '"sub_total":"' + array[5].replace(/[ R|$|.]/gi, '').replace(/[,]/gi, '.') + '"}');
            document.getElementById('json_prod').value = '{"produtos":[' + arrayProduto.toString() + ']}';
        }
    });
}

Here is the function that runs through the rows and columns and mounts array by push, then step to an Hidden input, in the controller I get `$_post['json_prod'] and step to the model

public function add($id_veiculo, $dt_chegada, $json, $valor_total, $observacao, $id_company)
{
    $sql = $this->db->prepare("INSERT INTO os SET id_veiculo = :id_veiculo, dt_chegada= :dt_chegada,"
        . "valor_total=:valor_total,observacao=:observacao,id_company=:id_company");

    $sql->bindValue(":id_veiculo", $id_veiculo);
    $sql->bindValue(":dt_chegada", date("Y-m-d", strtotime(str_replace('/', '-', $dt_chegada))));
    $sql->bindValue(":valor_total", $valor_total);
    $sql->bindValue(":observacao", strtoupper($observacao));
    $sql->bindValue(":id_company", $id_company);
    $sql->execute();
    $ultimo_id = $this->db->lastInsertId();
    if (!empty($json)) {
        foreach ($json as $firstarray) {
            foreach ((array)$firstarray as $j1) {
                $sql = $this->db->prepare("INSERT INTO item_os set id_produto = :id_produto, qt = :qt, id_servico = :id_servico,"
                    . "vlr_servico = :vlr_servico, id_company=:id_company");
                    $produto = $j1["produto"];
                $id_produto = explode('-', $produto);
                $sql->bindValue(":id_produto", trim($id_produto[0]));
                $sql->bindValue(":qt", $j1["qt"]);
                if ($j1["servico"] === '') {$j1["servico"] = 1;}
                $sql->bindValue(":id_servico", $j1["servico"]);
                $vlr_servico = str_replace('R$', '', $j1["vlr_servico"]);
                $vlr_servico = str_replace('.', '', $vlr_servico);
                $vlr_servico = str_replace(',', '.', $vlr_servico);
                $sql->bindValue(":vlr_servico", $vlr_servico);
                $sql->bindValue(":id_company", $id_company);
                $sql->execute();
            }
        }
    }
    return $ultimo_id;
}

but it’s going wrong

Warning: Invalid argument supplied for foreach() in C: xampp htdocs enginesystem models Os.php on line 19

Here’s the $jsonque I create manual

"{"products":[{"product":"2","Qt":"1","vlr_unitario":"6.46","servico":"2","vlr_servico":"0.15","sub_total":"6.61"},{"product":"45","Qt":"1","vlr_unitario":"7.81","servico":"4","vlr_servico":"5.00","sub_total":"12.81"}]}"

If anyone has a solution or other way to save.

  • 2

    In Javascript, you can use JSON.stringify to create JSON, it does not need to be manually; and in PHP use the function json_decode to convert the string in a array or object.

  • how to recover from localstorage to php pass, when I give post, I want to send the form data and array of items I generated from the table

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.