Dúvidas while horizontal e vertical
Olá pessoal, tudo em paz?
Estou com uma dúvida, estou precisando alocar os resultados anual das lojas mês a mês: exemplo:
LOJA JAN FEV MAR ABR MAI JUN JUL AGO SET OUT NOV DEZ
Tenho um select que me retorna os seguintes dados:
>
Citar
ORDEM
MES
LOJ_NOME
QTDE
TOTAL
CUSTO
CUSTO_TOTAL
LUCRO
24217
1
KI JOY
14.307,00
450.806,00
9.611,54
253.051,51
197.754,49
24218
2
KI JOY
10.221,00
313.901,50
9.365,83
158.561,90
155.339,60
24219
3
KI JOY
15.955,00
506.029,00
15.933,71
262.959,86
243.069,14
24220
4
KI JOY
16.204,00
515.074,00
19.459,65
292.481,96
222.592,04
24221
5
KI JOY
17.606,00
547.894,10
22.489,37
385.566,68
162.327,42
24222
6
KI JOY
17.443,00
556.943,00
21.624,05
369.893,94
187.049,06
24223
7
KI JOY
7.077,00
233.240,29
6.774,07
128.176,53
105.063,76
24226
10
KI JOY
55,00
1.655,00
51,19
1.290,05
364,95
24227
11
KI JOY
100,00
120,00
-
-
120,00
24217
1
LOJA 01
2.411,00
83.134,39
23.917,42
55.814,65
27.319,74
24218
2
LOJA 01
1.858,00
64.328,11
19.359,79
40.712,18
23.615,93
24219
3
LOJA 01
2.547,00
87.305,81
31.296,61
60.244,14
27.061,67
24220
4
LOJA 01
3.387,00
119.363,22
43.724,84
82.547,43
36.815,79
24221
5
LOJA 01
3.904,00
131.980,62
50.214,26
89.803,42
42.177,20
24222
6
LOJA 01
5.482,00
190.600,63
68.720,98
131.246,72
59.353,91
24223
7
LOJA 01
1.841,00
67.069,81
25.917,03
46.197,01
20.872,80
24224
8
LOJA 01
236,00
10.433,20
1.050,03
6.035,91
4.397,29
24225
9
LOJA 01
4,00
183,00
78,00
105,00
78,00
24226
10
LOJA 01
1,00
43,00
27,00
27,00
16,00
24217
1
LOJA 02
945,00
31.405,04
9.993,66
21.152,80
10.252,24
24218
2
LOJA 02
661,00
22.533,72
7.460,97
13.998,16
8.535,56
24219
3
LOJA 02
1.211,00
41.591,92
19.294,92
28.750,41
12.841,51
24220
4
LOJA 02
1.234,00
42.314,62
18.705,19
29.195,66
13.118,96
24221
5
LOJA 02
1.423,00
47.658,68
21.249,80
32.358,28
15.300,40
24222
6
LOJA 02
2.007,00
70.813,12
29.160,02
48.402,52
22.410,60
24223
7
LOJA 02
799,00
28.410,32
12.270,44
19.369,21
9.041,11
24217
1
LOJA 03
143,00
5.472,56
2.615,18
3.148,54
2.324,02
24218
2
LOJA 03
177,00
7.203,48
3.170,67
4.178,75
3.024,73
24219
3
LOJA 03
148,00
6.000,44
2.941,20
3.500,39
2.500,05
24220
4
LOJA 03
194,00
7.313,62
3.454,24
4.379,05
2.934,57
24221
5
LOJA 03
137,00
5.762,48
2.868,90
3.287,94
2.474,54
24222
6
LOJA 03
448,00
18.571,00
9.077,45
10.949,21
7.621,79
24223
7
LOJA 03
233,00
10.120,16
4.251,44
5.847,35
4.272,81
24225
9
LOJA 03
13,00
640,00
308,26
340,44
299,56
24226
10
LOJA 03
35,00
3.002,56
574,53
804,26
2.198,30
Se observar a loja KI JOY teve resultados até novembro, porém o resultado de janeiro da loja 01 está sendo alocado no mês de dezembro da loja KI JOY, o correto seria a coluna DEZ da loja KI JOY ficar 0,00.
Resultado PHP está em anexo.
minha pagina php
<?php
header("Content-Type: text/html; charset=utf-8",true);
$servidor = 'localhost:E:\bk\DADOS.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 = 2018;
$sql = "select
ordem,
mes,
loj_nome,
qtde,
total,
custo,
custo_total,
lucro
from sp_dash_evolutivo_lojas($ano)
order by loj_nome, ordem";
$query= ibase_query ($dbh, $sql);
$sql_lojas = "SELECT
LOJ_ID ,
loj_nome
FROM LOJAS order by LOJ_id";
$meses = array();
$valor_mes = array();
$lojas = array();
$indice_lojas = 0;
$indice_mes = 0;
$total = 0;
//Executa a instrução SQL
$qry_lojas = ibase_query ($dbh, $sql_lojas);
?>
<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">LOJAS</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>
</tr>
</thead>
<tbody>
<?php
$total_mes = 0;
$total_jan = 0;
$total_fev = 0;
$total_mar = 0;
$total_abr = 0;
$total_mai = 0;
$total_jun = 0;
$total_jul = 0;
$total_ago = 0;
$total_set = 0;
$total_out = 0;
$total_nov = 0;
$total_dez = 0;
while ($row = ibase_fetch_object($qry_lojas)){
//gravando os nomes no array
$lojas[$indice_lojas] = $row->LOJ_NOME;//indice 0
$indice_lojas = $indice_lojas + 1;
}
while ($row = ibase_fetch_object($query)) {
//guardar os valores no array
$valor_mes[$indice_mes] = $row->LUCRO ?? 0;
$indice_mes = $indice_mes + 1;
}
//zerar os índices
$indice_lojas = 0;
$indice_mes = 0;
foreach($lojas as $nome){
//fazer echo com array
echo '<tr>';
echo '<td>' . $lojas[$indice_lojas] . '</td>';
$indice_lojas = $indice_lojas + 1;
//OBS: todos as formas de pagamento deverão ter OBRIGATÓRIAMENTE os 12 meses preenchidos
for($i = 1; $i < 13; $i++){
echo '<td>' . number_format($valor_mes[$indice_mes] ?? 0, 2, ',', '.') . '</td>';
$indice_mes = $indice_mes + 1;
}
echo '</tr>';
}
//Libera a memoria usada
ibase_free_result($query);
//fecha conexão com o firebird
ibase_close($dbh);
?>
</html>
O correto seria preencher com zero os meses que não resultado, porém não estou conseguindo.

Discussão (0)
Carregando comentários...