Take mysql field dynamically

Asked

Viewed 167 times

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.

  • See if this solves for you: http://pastebin.com/NnAG5p4R

  • 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

  • 1

    So, but it’s not just you make a copy of the minimum and play the while?

  • @Sorack did not understand

  • 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

  • I added more information

Show 2 more comments

2 answers

3


I resolved:

I use the following code in loop to know if the fields exist and echo them.

if (isset($linha[0]['a'.$anoini])){
    echo $linha[0]['a'.$anoini];
}

hug to all who helped

0

you will perform a loop and within this loop will bring the result in your echo.

Example:

while($rs = $lista->fetchAll(PDO::FETCH_ASSOC)){
  $var1 = $rs['campo_da_tabela'];
  ...
}
  • 1

    the question is precisely to know which the 'campo_da_table' is created according to the year chosen by the user

  • 1

    changed my question, see if you can help me. hug.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.