Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Olá pessoal, tudo em paz?
Estou precisando da ajuda de vocês. Tenho um select que me retorna os valores por formas de pagamento mês a mês do ano.
Retorno do Select está anexado.
Preciso organizar em uma tabela como essa abaixo:
FORMA DE PAGAMENTO JAN FEV MAR ABR...
DINHEIRO 500 10 0 52
CARTAO 1500 50 44 90
....
O problema é que não consigo fazer via php com que as formas de pagamento não se repitam.
Página PHP
<?php
header("Content-Type: text/html; charset=utf-8",true);
$servidor = 'localhost:E:\BANCO.FDB';
//conexão com o banco, se der erro mostrara uma mensagem.
if (!($dbh=ibase_connect($servidor, 'SYSDBA', 'masterkey')))
die('Erro ao conectar: ' . ibase_errmsg());
$loja = 0;
$ano = 2017;
$sql = 'SELECT
fpgto_id ,
fpgto_descricao ,
MES ,
qtde ,
valor
FROM SP_DASH_VENDAS_FPGTO_MES_A_MES('.$loja.', '.$ano.') ';
$sql_forma = 'SELECT
fpgto_id ,
fpgto_descricao
FROM forma_pagamento order by fpgto_id';
$meses = array();
$valores = array();
$forma = array();
$indice = 0;
$total = 0;
//Executa a instrução SQL
$query= ibase_query ($dbh, $sql);
$qry_forma = ibase_query ($dbh, $sql_forma);
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js" integrity="sha384-vFJXuSJphROIrBnz7yo7oB41mKfc8JzQZiCq4NCceLEaO4IHwicKwpJf9c9IpFgh" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js" integrity="sha384-alpBpkh1PFOepccYVYDB4do5UnbKysX5WZXm3XxPqe5iKTfUKjNkCk9SaVuEZflJ" crossorigin="anonymous"></script>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<style type="text/css">
.table td, .table th {
padding: .4rem;
font-size:12px
}
</style>
</head>
<body>
<div class="row col-md-12 ">
<div class="col-md-12 ">
<table class="table table-striped table-bordered table-hover">
<thead>
<tr class="text-center" style="background-color: #F5F5F5; ">
<th colspan="2">Ano 2017</th>
</tr>
<tr>
<th scope="col">Forma de Pagamento</th>
<th scope="col" class="text-center">JAN</th>
<th scope="col" class="text-center">FEV</th>
<th scope="col" class="text-center">MAR</th>
<th scope="col" class="text-center">ABR</th>
<th scope="col" class="text-center">MAI</th>
<th scope="col" class="text-center">JUN</th>
<th scope="col" class="text-center">JUL</th>
<th scope="col" class="text-center">AGO</th>
<th scope="col" class="text-center">SET</th>
<th scope="col" class="text-center">OUT</th>
<th scope="col" class="text-center">NOV</th>
<th scope="col" class="text-center">DEZ</th>
<th scope="col" class="text-center">TOTAL</th>
</tr>
</thead>
<tbody>
<?php
$forma_anterior = 0;
while ($row = ibase_fetch_object ($query)) {
if($forma_anterior == $row->FPGTO_ID){
echo "<tr>
<td>".$row->FPGTO_DESCRICAO."</td>
";
for ($i=1; $i < 13; $i++) {
if (($row->MES == $i) and ($row->VALOR > 0)){
echo "<td>".$row->VALOR."</td>";
$formas[$indice] = $row->FPGTO_ID;
$meses[$indice] = $row->MES;
$valores[$indice] = $row->VALOR;
} else {
echo "<td>0,00</td>";
$formas[$indice] = $row->FPGTO_ID;
$meses[$indice] = $i;
$valores[$indice] = 0;
}
}
$total += $row->VALOR;
echo "<td>".$total."</td></tr>";
}
$forma_anterior = $row->FPGTO_ID;
$indice = $indice+1;
}
?>
</tbody>
</table>
</div>
</div>
</body>
<?php
//Libera a memoria usada
ibase_free_result($query);
//fecha conexão com o firebird
ibase_close($dbh);
?>
</html>
Resultado atual do php está em anexo.
Desde já agradeço a todos.
Abs.A saida esta assim
DINHEIRO
DINHEIRO
DINHEIRO
.....
>
22 minutos atrás, 7master disse:
A saida esta assim
DINHEIRO
DINHEIRO
DINHEIRO
.....
Posta a saída completa, tira uma print das saídas dos selects do banco de dados e a saída do php
esta em anexoAcho que na query você deverá fazer o select usando GROUP BY e agrupar o resultado pela forma de pagamento. É só uma sugestão.
Já estou fazendo isto
Meu pitaco:
... sum(valor) ... group by forma_pagamento
Sua saída sql deve ter saído algo assim:
/monthly_2018_01/sql.png.d9c7e8a6ea9f7e24a0813c39d1bc55ba.png" />
alterei o código e fiz um teste e saiu assim:
Segue o código:
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" media="screen" href="css.css" />
<script src="main.js"></script>
<style>
table, td, th {
border: 1px solid black;
border-collapse: collapse;
}
</style>
</head>
<body>
<?php header("Content-Type: text/html; charset=ISO-8859-1",true);
//CONECTA COM O BANCO DE DADOS
//alterar para sua conexão
$conexao = mysqli_connect("localhost", "root", "","server") or die('Erro ao conectar com o servidor');
mysqli_select_db($conexao,'server') or die ('Erro ao selecionar o banco de dados');
echo '<table width="100%">';
echo '<thead><tr>';
echo '<th>FORMA DE PAGAMENTO</th>';
echo '<th>JAN</th>';
echo '<th>FEV</th>';
echo '<th>MAR</th>';
echo '<th>ABR</th>';
echo '<th>MAI</th>';
echo '<th>JUN</th>';
echo '<th>JUL</th>';
echo '<th>AGO</th>';
echo '<th>SET</th>';
echo '<th>OUT</th>';
echo '<th>NOV</th>';
echo '<th>DEZ</th>';
echo '</tr></thead>';
//ALTERAR CONFORME SUA TABELA NO BANCO DE DADOS!!!
//$sql vai selecionar somente os valores
$sql = mysqli_query($conexao,"SELECT valor FROM pagamento ORDER BY fpgto_id ASC");
//$sql_fpgto_desc vai selecionar somente a descrição das forams de pgto
$sql_fpgto_desc = mysqli_query($conexao,"SELECT DISTINCT fpgto_desc FROM pagamento ORDER BY fpgto_id ASC");
$nome_pagto = array();
$valor_mes = array();
$indice_pagto = 0;
$indice_mes = 0;
while ($row = mysqli_fetch_assoc($sql_fpgto_desc)){
//gravando os nomes no array
$nome_pagto[$indice_pagto] = $row['fpgto_desc'];//indice 0
$indice_pagto = $indice_pagto + 1;
}
while ($row = mysqli_fetch_assoc($sql)) {
//guardar os valores no array
$valor_mes[$indice_mes] = $row['valor'];
$indice_mes = $indice_mes + 1;
}
//zerar os índices
$indice_pagto = 0;
$indice_mes = 0;
foreach($nome_pagto as $nome){
//fazer echo com array
echo '<tr>';
echo '<td>' . $nome_pagto[$indice_pagto] . '</td>';
$indice_pagto = $indice_pagto + 1;
//OBS: todos as formas de pagamento deverão ter OBRIGATÓRIAMENTE os 12 meses preenchidos
for($i = 1; $i < 13; $i++){
echo '<td>' . $valor_mes[$indice_mes] . '</td>';
$indice_mes = $indice_mes + 1;
}
echo '</tr>';
}
echo '</tbody></table>';
mysqli_close($conexao);
?>
</body>
</html>
Acredito que tenha uma maneira mais fácil e rápida de gerar o que você quer, acho que seria melhor você alterar seu banco de dados.
Espero que tenha dado certo, boa sorte.
>
1 hora atrás, 7master disse:
FORMA DE PAGAMENTO JAN FEV MAR ABR...
DINHEIRO 500 10 0 52
CARTAO 1500 50 44 90
....
O problema é que não consigo fazer via php com que as formas de pagamento não se repitam.
Página PHP