LAÇO HORIZONTAL E VERTICAL
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.Discussão (8)
Carregando comentários...