The function below does not return me as expected.
I want it to return a sum and the fields with the id
of the collaborator, however in JavaScript I get an error, and I believe it is in PHP.
The select works fine by running directly in phpMyAdmin.
public function select($id) {
$return_values = array();
try {
$sql = "SELECT SUM(custo_preco) as SOMA , custo_descricao,custo_id,custo_data,custo_mes,custo_local,custo_preco FROM tb_custo WHERE colaborador_colaborador_id= :colaborador_colaborador_id;";
$p_sql = Conexao::getInstance()->prepare($sql);
$p_sql->bindValue(':colaborador_colaborador_id', $id);
$execute = $p_sql->execute();
if ($execute) {
while ($array = $p_sql->fetch(PDO::FETCH_OBJ)) {
$return_values['linhas'].="<tr data-id='{$array->custo_id}'>"
. "<td class='actions'> "
. "<a href='#' name='botao_editar_custo' class='btn btn-primary btn-xs' data-toggle='tooltip' data-placement='top' data-original-title='Edit'><i class='fa fa-pencil'></i></a>"
. "<a href='#' name='botao_excluir_custo' class='btn btn-danger btn-xs' data-toggle='modal' data-target='#delete-modal' data-toggle='tooltip' data-placement='top' data-original-title='Delete'><i class='fa fa-times'></i></a>"
. "<td>{$array->custo_descricao}"
. "<td>" . date('d/m/Y H:i', strtotime($array->custo_data)
. "<td>{$array->custo_local}"
. "<td>{$array->custo_preco}");
}
$return_values['soma'] = $array->SOMA;
} else {
$return_values['erro'] = "Erro ao recuperar dados.";
}
return $return_values;
} catch (PDOException $e) {
return "Erro, contate o suporte!\n" . $e->getCode() . " Mensagem: " . $e->getMessage();
}
}
JavaScript Function
function preencher_table_custo() {
$.ajax({
url: 'formularios/custo/php/select.php',
type: 'POST',
dataType: 'json',
data: {id: $("input[name='colaborador_logado']").val()},
beforeSend: function () {
$(".formulario_geral_custo table tbody").html("<tr data-id=''><td colspan='9'><img src='./imagens/loading.gif' height='26' width='26' align='center'>");
},
success: function (data) {
alert(data.soma);
$(".formulario_geral_custo table tbody").html(data.linhas);
$(".formulario_geral_custo table tbody > tr").hover(function () {
$(this).toggleClass("hover");
});
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
$(".mensage").css("background", "#980000");
$(".mensage").text("Verifique sua conexão com a internet e tente novamente.");
fadeGeral(4000);
}
});
}