Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Galera,
Estou precisando de uma ajuda para poder agrupar e totalizar os resultados por centro de custo. No PHP, consigo agrupar por centro de custo, mas não estou conseguindo TOTALIZAR por cada centro de custo. Confira nesta imagem como está http://prntscr.com/41hpxn
Veja que a palavra Total: aparece abaixo do PRIMEIRO REGISTRO, mas deveria exibir abaixo do ÚLTIMO registro.
Obs.: já fiz o ORDER BY CENTROCUSTO para poder agrupar.
Segue o código da página:
<table class="no-border">
<?php
if (count($ana) > 0) {
?>
<thead>
<tr class="small no-border">
<th width="5%" class="small">Reserva</th>
<th width="25%" class="small">Emissao</th>
<th width="15%" class="small">Passageiro</th>
<th width="10%" class="small">Cia</th>
<th width="10%" class="small">Trecho</th>
<th width="10%" class="small">Bilhete</th>
<th class="small">Produto</th>
<th class="small">Melhor Tarifa</th>
<th class="small">Tarifa Aplicada</th>
<th class="small">Taxas</th>
<th class="small">Extras</th>
<th class="small">Total</th>
</tr>
</thead>
<tbody class="no-border-x no-border-y">
<?php
$TotDesconto = 0;
$TotTaxa = 0;
$TotTotal = 0;
$TotTotDesconto = 0;
$TotTotTaxa = 0;
$TotTotTotal = 0;
$prev = '';
$tot = '';
foreach ($ana as $linha) {
?>
<?php
if( $prev!==trim($linha['CENTROCUSTO']) ) {
$prev = trim($linha['CENTROCUSTO']);
?>
<tr class="small">
<td colspan="12" class="small"><strong>Centro de Custo: <?php echo trim($linha['CENTROCUSTO']); ?></strong></td>
</tr>
<?php }?>
<tr class="small">
<td class="small"><?php echo trim($linha['RESERVA']); ?></td>
<td class="small"><?php echo trim($linha['EMISSAO']); ?></td>
<td class="small"><?php echo trim($linha['PASSAGEIRO']); ?></td>
<td class="small"><?php echo trim($linha['CIA']); ?></td>
<td class="small"><?php echo trim($linha['TRECHO']); ?></td>
<td class="small"><?php echo trim($linha['BILHETE']); ?></td>
<td class="small"><?php echo trim($linha['PRODUTO']); ?></td>
<td class="small"><?php echo toReal(trim($linha['MELHORVALOR'])); ?></td>
<td class="small"><a href="#" title="Tairfa: <?php echo toReal(trim($linha['TARIFAAPLICADA'])); ?> - Desconto: <?php echo toReal(trim($linha['DESCONTOCONTRATUAL'])); ?>" data-toggle="tooltip"><?php echo toReal(trim($linha['TARIFAAPLICADA']) - trim($linha['DESCONTOCONTRATUAL'])); ?></a></td>
<td class="small"><?php echo toReal(trim($linha['TAXA'])); ?></td>
<td class="small"><?php echo toReal(trim($linha['EXTRAS'])); ?></td>
<td class="small"><?php echo toReal(trim($linha['TOTAL'])); ?></td>
</tr>
<?php
if( $tot!==trim($linha['CENTROCUSTO']) ) {
$tot = trim($linha['CENTROCUSTO']);
?>
<tr class="small">
<td colspan="12" class="small"><strong>Total: <?php //echo trim($linha['CENTROCUSTO']); ?></strong></td>
</tr>
<?php }?>
<?php
}
?>
<tr class="small">
<td colspan="12" align="left" class="small"><strong>Total de Registros:</strong> <?php echo count($ana);?></td>
</tr>
<?php
}else {
?>
<tr class="small alert alert-warning">
<td colspan="12" align="center" class="small"><strong>Não há pax viajando neste Período e/ou Aeroporto</strong></td>
</tr>
</tbody>
<?php
}
$analitico->closeCursor();
$ana = null;
$con_stur = null;
?>
</table>
Obrigado!Player,
Eu não entendi. Vc tá pedindo para eu tirar o if do laço???
Tinha entendido que você queria mostrar somente o total no final
Pelo que ví agora você quer somar os centros de custo, exibir ele e mostrar o total somente quando o próximo centro não foi igual
Como não tenho alguns valores fiz um simulado e alterei algumas coisas
<?php
$ana=[
0=>[
'CENTROCUSTO' =>'Ninja',
'RESERVA' =>'res',
'EMISSAO' =>'emi',
'PASSAGEIRO' =>'pas'
],
1=>[
'CENTROCUSTO' =>'Ninja',
'RESERVA' =>'res1',
'EMISSAO' =>'emi1',
'PASSAGEIRO' =>'pas1'
],
2=>[
'CENTROCUSTO' =>'Ninja2',
'RESERVA' =>'res2',
'EMISSAO' =>'emi2',
'PASSAGEIRO' =>'pas2'
],
2=>[
'CENTROCUSTO' =>'Ninja3',
'RESERVA' =>'res3',
'EMISSAO' =>'emi3',
'PASSAGEIRO' =>'pas3'
]
];
?>
<table class="no-border">
<?php
if (count($ana) > 0) {
?>
<thead>
<tr class="small no-border">
<th width="5%" class="small">Reserva</th>
<th width="25%" class="small">Emissao</th>
<th width="15%" class="small">Passageiro</th>
<th width="10%" class="small">Cia</th>
<th width="10%" class="small">Trecho</th>
<th width="10%" class="small">Bilhete</th>
<th class="small">Produto</th>
<th class="small">Melhor Tarifa</th>
<th class="small">Tarifa Aplicada</th>
<th class="small">Taxas</th>
<th class="small">Extras</th>
<th class="small">Total</th>
</tr>
</thead>
<tbody class="no-border-x no-border-y">
<?php
$TotDesconto = 0;
$TotTaxa = 0;
$TotTotal = 0;
$TotTotDesconto = 0;
$TotTotTaxa = 0;
$TotTotTotal = 0;
$prev = '';
$tot = '';
$i=0;
foreach ($ana as $linha) {
?>
<?php
if( $prev!==trim($linha['CENTROCUSTO']) ) {
$prev = trim($linha['CENTROCUSTO']);
?>
<tr class="small">
<td colspan="12" class="small"><strong>Centro de Custo: <?php echo trim($linha['CENTROCUSTO']); ?></strong></td>
</tr>
<?php }?>
<tr class="small">
<td class="small"><?php echo trim($linha['RESERVA']); ?></td>
<td class="small"><?php echo trim($linha['EMISSAO']); ?></td>
<td class="small"><?php echo trim($linha['PASSAGEIRO']); ?></td>
<td class="small"><?php echo trim($linha['CIA']); ?></td>
<td class="small"><?php echo trim($linha['TRECHO']); ?></td>
<td class="small"><?php echo trim($linha['BILHETE']); ?></td>
<td class="small"><?php echo trim($linha['PRODUTO']); ?></td>
<td class="small"><?php echo toReal(trim($linha['MELHORVALOR'])); ?></td>
<td class="small"><a href="#" title="Tairfa: <?php echo toReal(trim($linha['TARIFAAPLICADA'])); ?> - Desconto: <?php echo toReal(trim($linha['DESCONTOCONTRATUAL'])); ?>" data-toggle="tooltip"><?php echo toReal(trim($linha['TARIFAAPLICADA']) - trim($linha['DESCONTOCONTRATUAL'])); ?></a></td>
<td class="small"><?php echo toReal(trim($linha['TAXA'])); ?></td>
<td class="small"><?php echo toReal(trim($linha['EXTRAS'])); ?></td>
<td class="small"><?php echo toReal(trim($linha['TOTAL'])); ?></td>
</tr>
<?php
if( $tot!==trim($linha['CENTROCUSTO']) ) {
$tot = trim($linha['CENTROCUSTO']);
}
if($ana[$i]['CENTROCUSTO']!=$ana[$i+1]['CENTROCUSTO']){
?>
<tr class="small">
<td colspan="12" class="small"><strong>Total: <?php //echo trim($linha['CENTROCUSTO']); ?></strong></td>
</tr>
<?php
}
$i++;
?>
<?php
}
?>
<tr class="small">
<td colspan="12" align="left" class="small"><strong>Total de Registros:</strong> <?php echo count($ana);?></td>
</tr>
<?php
}else {
?>
<tr class="small alert alert-warning">
<td colspan="12" align="center" class="small"><strong>Não há pax viajando neste Período e/ou Aeroporto</strong></td>
</tr>
</tbody>
<?php
}
// $analitico->closeCursor();
$ana = null;
$con_stur = null;
?>
</table>
<?php
function toReal($val){
return $val;
}
A diferença básica é que coloquei um contador $i que em determinada parte do código compara se o nome do atual centro de custo é diferente do próximo, se for ele imprime!
Você vai ter que alterar algumas coisas e por favor tente separar mais a lógica do html
Player,
Obrigado pela ajuda!
Fiz conforme você informou, mas ficou da mesma forma, conforme pode visualizar o código no http://pastebin.com/GAfBGTny e a imagem da tela no http://prntscr.com/41rpv1
Veja que o Centro de Custo ADECE possui 2 registro, mas o total aparece ENTRE OS DOIS. O correto seria aparecer abaixo do ÚLTIMO REGISTRO, no caso, o Passageiro GILSON JOSE GONDIM FH
O que eu desejo é isto: http://prntscr.com/41roxl
Vocẽ precisa comparar se nome do centro atual é diferente do próximo, só assim você imprime.
Player,
Fiz e funcionou, mas está gerando um erro na linha 264, conforme abaixo:
if($ana[$i]['CENTROCUSTO']!=$ana[$i+1]['CENTROCUSTO']){ // LINHA 264
?>
<tr class="small">
<td colspan="12" class="small"><strong>Total: <?php //echo trim($linha['CENTROCUSTO']); ?></strong></td>
</tr>
<?php
}
$i++;
?>
<?php
}
Confira a tela com a mensagem de erro: http://prntscr.com/41ta0iO erro é pq quando chega no último resultado você está tentando acessar um resultado a frente que não existe.
Para evitar isso você pode usar um isset para checar se existe, talvez o código que passei abaixo não resolva, se não resolver têm que trocar a lógica.
if(! isset($ana[$i+1]['CENTROCUSTO']) || $ana[$i]['CENTROCUSTO']!=$ana[$i+1]['CENTROCUSTO'])
Como eu não tenho todo o seu código ele pipoca milhares de erros quando o php está com os erros notices liberados por isso eu não tenho como testar.
Em um servidor de produção esse erro some.
Se quiser testar, altere o error_reporting no php-ini para
error_reporting = E_ALL & ~E_NOTICE
Player,
Coloquei conforme vc informou e removeu os erros, mas os centros de custo alguns ficam agrupados e outros não, daí o total não fica como deveria.
Aqui coloquei o código completo, inclusive vc conseguirá rodar em sua máquina http://pastebin.com/kWAtDifG
Obrigado.
Alguém sabe como posso resolver isto???
Você está mandando exibir o total dentro do laço foreach. Termine ele e só então exiba