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 ao trabalhar com Array Dinâmico. Estou fazendo da seguinte forma:
Loop PHP:
<?php
while($totf = $totalf->fetch(PDO::FETCH_ASSOC)) {
var_dump($totf);
//}
?>
<tr>
<td><?php //echo trim($totf['FATURA']); ?></td>
<td><?php //echo trim($totf['EMISSAO']); ?></td>
<td><?php //echo trim($totf['CONTATO']); ?></td>
<td><?php echo trim($totf['Reserva']); ?></td>
</tr>
<?php
}
?>
Resultado do SQL:
array (size=31)
'NM_SET' => string '' *(length=0)*
'IS_SET' => string '0' *(length=1)*
'IS_FOR' => string '6' *(length=1)*
'Fornecedor' => string 'AZUL LINHAS AEREAS' *(length=18)*
'Passageiro' => string 'NICORRAY' *(length=15)*
'Reserva' => string '335 ' *(length=10)*
'Emissao' => string '28/03/2014' *(length=10)*
'VlOriginal' => string '0.00' *(length=4)*
'Moeda' => string 'R *(length=2)*
'Cambio' => string '1' *(length=1)*
'NR_MST_BIL' => string '5229367270' *(length=10)*
'NR_MST_OPV' => null
'Solicitacao' => string '' *(length=0)*
'Saida' => null
'Dt_Entrada' => null
'Dt_Saida' => null
'Valor' => string '0.00' *(length=4)*
'Rota' => null
'Produto' => string 'Aéreo' *(length=6)*
'Complemento' => null
'Taxas' => string '0.00' *(length=4)*
'Taxa_Servico' => string '0.00' *(length=4)*
'Desconto' => string '0.00' *(length=4)*
'Total' => string '40.00' *(length=5)*
'Observacao' => string ' ' *(length=1)*
'Taxas_Repasse' => string '40' *(length=2)*
'Integracao' => string 'ADVA8Q43G1' *(length=10)*
'TP_CLI_DED_ISS' => string 'P' *(length=1)*
'TP_CLI_INC_ISS_TDU' => string 'N' *(length=1)*
'TP_CLI_INC_ISS_TSE' => string 'N' *(length=1)*
'TX_IEM' => null
array (size=31)
'NM_SET' => string '' *(length=0)*
'IS_SET' => string '0' *(length=1)*
'IS_FOR' => string '10' *(length=2)*
'Fornecedor' => string 'TAM LINHAS AEREAS' *(length=17)*
'Passageiro' => string 'PEDRO KASSAB MR' *(length=15)*
'Reserva' => string '334 ' *(length=10)*
'Emissao' => string '31/03/2014' *(length=10)*
'VlOriginal' => string '2260.00' *(length=7)*
'Moeda' => string 'R *(length=2)*
'Cambio' => string '1' *(length=1)*
'NR_MST_BIL' => string '2486673153' *(length=10)*
'NR_MST_OPV' => null
'Solicitacao' => string '' *(length=0)*
'Saida' => null
'Dt_Entrada' => null
'Dt_Saida' => null
'Valor' => string '2260.00' *(length=7)*
'Rota' => null
'Produto' => string 'Aéreo' *(length=6)*
'Complemento' => null
'Taxas' => string '87.01' *(length=5)*
'Taxa_Servico' => string '0.00' *(length=4)*
'Desconto' => string '0.00' *(length=4)*
'Total' => string '2573.01' *(length=7)*
'Observacao' => string ' ' *(length=1)*
'Taxas_Repasse' => string '226' *(length=3)*
'Integracao' => string '1910417' *(length=7)*
'TP_CLI_DED_ISS' => string 'P' *(length=1)*
'TP_CLI_INC_ISS_TDU' => string 'N' *(length=1)*
'TP_CLI_INC_ISS_TSE' => string 'N' *(length=1)*
'TX_IEM' => null
Erro retornando por cada campo **<?php echo trim($fat->Reserva); ?>** **Notice: Trying to get property of non-object in...**Olá! Tente usar FETCH_ARRAY. Assim ele se tornara um Array.
Espero ter Ajudado ;)
Galera,
Obrigado pela ajuda, mas não funcionou. Estou tentando da seguinte forma:
<?php
while($totf = $totalf->fetchAll(PDO::FETCH_ASSOC)) {
var_dump($totf);
//}
?>
<tr>
<td><?php //echo trim($totf['FATURA']); ?></td>
<td><?php //echo trim($totf['EMISSAO']); ?></td>
<td><?php //echo trim($totf['CONTATO']); ?></td>
<td><?php echo trim($totf[0]['Reserva']); ?></td>
</tr>
<?php
}
?>
E só consigo pegar o valor do array, quando especifico **<?php echo trim($totf[0]['Reserva']); ?>** neste caso, peguei o de valor **[ZERO]**. Eu gostaria que fosse dinâmico, ou seja, não quero especificar $totf[1]['Reserva'], $totf[2]['Reserva'], etc.
Como devo fazer?
Abaixo segue o resultado de: while($totf = $totalf->fetchAll(PDO::FETCH_ASSOC)) {
array (size=3)
0 =>
array (size=31)
'NM_SET' => string '1' *(length=0)*
'IS_SET' => string '0' *(length=1)*
'RESERVA' => string '65' *(length=1)*
1 => 'NM_SET' => string '4' *(length=0)*
'IS_SET' => string '0' *(length=1)*
'RESERVA' => string '2' *(length=1)*
2 =>
array (size=31)
'NM_SET' => string '7' *(length=0)*
'IS_SET' => string '0' *(length=1)*
'RESERVA' => string '9' *(length=1)*<?php
$totf = $totalf->fetchAll(PDO::FETCH_ASSOC);
foreach($totf as $dados):
?>
<tr>
<td><?php //echo trim($totf['FATURA']); ?></td>
<td><?php //echo trim($totf['EMISSAO']); ?></td>
<td><?php //echo trim($totf['CONTATO']); ?></td>
<td><?php echo trim($totf['Reserva']); ?></td>
</tr>
<?php endforeach; ?>Paulinho,
Obrigado pela ajuda, deu certinho.
Aproveitando, eu gostaria de AGRUPAR/QUEBRAR por fornecedor as linhas referentes a ele. Por exemplo:
- Tam
Linha 01,
Linha 02, etc..
-Azul
Linha 03,
Linha 04, etc..
Eu já possuo um Order By na consulta, mas como faço para a linha de Fornecedor ser ÚNICA e logo abaixo exibir apenas o que estiver com este fornecedor?
Eis minha tabela completa:
<table width="100%">
<tr>
<td>Reserva</td>
<td>Passageiro</td>
<td>Rota</td>
<td>Valor</td>
</tr>
<?php
foreach($totf as $dados):
?>
<tr>
<td colspan="4">Fornecedor: <?php echo trim($dados['Fornecedor']); ?></td>
</tr>
<tr>
<td><?php echo trim($dados['Reserva']); ?></td>
<td><?php echo trim($dados['Passageiro']); ?></td>
<td><?php echo trim($dados['Rota']); ?></td>
<td><?php echo trim($dados['Valor']); ?></td>
</tr>
<?php
endforeach;
?>
</table><?php
$prev = '';
foreach($totf as $dados):
?>
if( $prev!==trim($dados['Fornecedor']) ) {
$prev = trim($dados['Fornecedor']);
<tr>
<td colspan="4">Fornecedor: <?php echo trim($dados['Fornecedor']); ?></td>
</tr>
}Perfeito William! Muito obrigado!
Como vc fez: FETCH_ASSOC vc deve acessar com array, e não como objeto.
-> é objeto
[] é array.