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 sets the array by push, then to an input hidden, in the controler I get '$ _post [' json_prod '] and step into 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 is giving error
Warning: Invalid argument supplied for foreach () in C: \ xampp \ htdocs \ enginesystem \ models \ Os.php on line 19
Here is the $ jsonque create manual
"" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" , "sub_total": "6.61"}, "product": "45", "qt": "1", "vlr_unitario": "7.81", "service": "4", "vlr_servico": "5.00" , "sub_total": "12.81"}]} "
Whether or not you have a solution or a way to save.