4
I created a select command based on information sent by the user, e.g.:
$anoini = $_GET['anoini']; //variavel com ano escolhido pelo usuário
$anofim = 2016; //ano final é 2016
$s = 'SELECT pa_proc_id, procedimento, '; //inicio da query
while ($anoini < $anofim) { //laço para listar todos os anos
$s = $s . 'SUM( pa_qtdapr * ( 1 - ABS( SIGN( CONVERT( SUBSTRING( pa_cmp, 1, 4 ) , signed ) - '.$anoini.' ) ) ) ) AS a'.$anoini.', '; //a cada ano, ele inclui uma linha na query
$anoini++;
}
$s = $s . 'SUM( pa_qtdapr * ( 1 - ABS( SIGN( CONVERT( SUBSTRING( pa_cmp, 1, 4 ) , signed ) - '.$anofim.' ) ) ) ) AS a'.anofim.' '; //finaliza criando a linha com o ano atual
$s = $s . 'FROM sia WHERE pa_ufmun = "'.$ibge.'" and substring(pa_cmp,1,4) >= :anoini GROUP BY pa_proc_id'; //finaliza a query
the code above works perfectly and generates me a result with some dynamically created fields referring to each year from the indicated by the user until 2016, ex:a2014, a2015, a2016
run the query and create the array:
$lista = $db->prepare($s) or trigger_error($db->error);
$lista->bindValue(":anoini",$GLOBALS['anoini']);
$lista->execute();
$GLOBALS['procedimento'] = $lista->fetchAll(PDO::FETCH_ASSOC);
my question is: how to know which fields (a2014, a2015, etc) were created and how to give an "echo" in them?
EDITED
I gave a var_dump and the result was this below:
array (size=130)
0 =>
array (size=9)
'pa_proc_id' => string '0101010010' (length=10)
'procedimento' => string 'ATIVIDADE EDUCATIVA / ORIENTACAO EM GRUPO NA ATENCAO BASICA ' (length=60)
'a2010' => string '0' (length=1)
'a2011' => string '0' (length=1)
'a2012' => string '0' (length=1)
'a2013' => string '0' (length=1)
'a2014' => string '0' (length=1)
'a2015' => string '0' (length=1)
'a2016' => string '67' (length=2)
1 =>
array (size=9)
'pa_proc_id' => string '0101020015' (length=10)
'procedimento' => string 'ACAO COLETIVA DE APLICACAO TOPICA DE FLUOR GEL' (length=46)
'a2010' => string '0' (length=1)
'a2011' => string '0' (length=1)
'a2012' => string '0' (length=1)
'a2013' => string '0' (length=1)
'a2014' => string '0' (length=1)
'a2015' => string '0' (length=1)
'a2016' => string '23' (length=2)
2 =>
array (size=9)
'pa_proc_id' => string '0101020031' (length=10)
'procedimento' => string 'ACAO COLETIVA DE ESCOVACAO DENTAL SUPERVISIONADA' (length=48)
'a2010' => string '0' (length=1)
'a2011' => string '0' (length=1)
'a2012' => string '0' (length=1)
'a2013' => string '0' (length=1)
'a2014' => string '0' (length=1)
'a2015' => string '0' (length=1)
'a2016' => string '184' (length=3)
how to list the field names, not values (in this case from a2010 to a2016)?
hugs.
Would that be How to print the SQL statement being sent to the database? ?
– rray
See if this solves for you: http://pastebin.com/NnAG5p4R
– Sorack
no, the question is to know which fields were created (a2014, a2015, etc), because the fields are created according to the year chosen by the user. after discovering the fields, then lists them
– Italo Rodrigo
So, but it’s not just you make a copy of the minimum and play the while?
– Sorack
@Sorack did not understand
– Italo Rodrigo
Basically you already know the names of the fields pq when you do the while of the year already puts it in the
as
table. Then it is only vc to save it in an array or play the while– Sorack
I added more information
– Italo Rodrigo