How to put a loop of DB columns inside an array? [closed]

-1

I have a database with dynamic columns that can one hour appear, another time not ... also more columns can be added or even extracted ones that already exist.

I made a code that allows me to capture these columns in my database in real time and now I need to make these columns appear in another part of the code:

<?php

require("setup_do_banco.php");

$colunas    = $pdo->prepare("SHOW COLUMNS FROM imovel"); 
$colunas->execute();

###### Aqui tenho todos as colunas do meu banco de dados
while ( $coluna = $colunas->fetch() ) { 
    $coluna = $coluna["Field"]; echo "$coluna <br />"; 
}

$array = array (
    'key'       => '8b0dc65f996f98fd178a9defd0efa077',
    'module'    => 'imoveis',
    'method'    => 'busca_imoveis',
    'field'     => array (
    ////////////////////////////////
    #                   <------------
    ////////////////////////////////
    )   
);

$client = new SoapClient(null, array (
    'uri' => 'http://soap.imo.bi/',
    'location' =>
    'http://soap.imo.bi/soap.dll',
    'trace' => 'trace'
    )
);

$res = $client->get($array);

echo "<pre>"; print_r($res); echo "</pre>";

?>

As you can see, through a loop I retrieved all the columns from my database and now I need to plant them inside the arrow field with comma at the end and line break. I tried putting the loop inside the array but it did not work.

How do I put the columns of my bank there in place of the arrow?

    
asked by anonymous 26.12.2014 / 20:43

4 answers

1

Based on the update and here , the only change is in place a numerical index defining a key that is composed by the description of the bank field that is represented by: $item['field'] .

<?php
//O fetchAll() deve retornar um array nesse formato.
$array_banco = array(
                0 => array('field' =>  'DATA', 'description' => 'Data cadastro'),
                1 => array('field' => 'ENDERECO', 'description' => 'Endereço'),
                2 => array('field' => 'BAIRRO', 'description' => 'Bairro'));


$array = array (
    'key'       => '8b0dc65f996f98fd178a9defd0efa077',
    'module'    => 'imoveis',
    'method'    => 'busca_imoveis',
    'field'     => array() 
);


foreach($array_banco as $item){
    $array['field'][$item['field']] = sprintf("'%s' => '%s'", $item['field'], $item['description']);
}
echo '<pre>';
print_r($array);


echo $array['field']['DATA'] .' - '. $array['field']['ENDERECO'] .' - '. $array['field']['BAIRRO'];

Output:

Array
(
    [key] => 8b0dc65f996f98fd178a9defd0efa077
    [module] => imoveis
    [method] => busca_imoveis
    [field] => Array
        (
            [DATA] => 'DATA' => 'Data cadastro'
            [ENDERECO] => 'ENDERECO' => 'Endereço'
            [BAIRRO] => 'BAIRRO' => 'Bairro'
        )

)
'DATA' => 'Data cadastro' - 'ENDERECO' => 'Endereço' - 'BAIRRO' => 'Bairro'
    
28.12.2014 / 01:05
4

I think you need to change the order of things to simplify ...

Suggestion:

require("setup_do_banco.php");

$array = array (
    'key'       => '8b0dc65f996f98fd178a9defd0efa077',
    'module'    => 'imoveis',
    'method'    => 'busca_imoveis',
    'field'     => array ()   
);

$colunas    = $pdo->prepare("SHOW COLUMNS FROM imovel"); 
$colunas->execute();

while ( $coluna = $colunas->fetch() ) { 
    $array['field'][] = $coluna["Field"];
}

In this way this $array['field'] will be filled within while, and past this code the array is "populated".

    
26.12.2014 / 20:52
4

I do not know if I understand it but I think it would be just this:

$array['field'] = $colunas->fetchAll(PDO::FETCH_ASSOC);

FetchAll returns just a full associative array whose keys are the names of the fields and the values that were returned in it.

    
26.12.2014 / 20:53
2

Simply create an array by picking up the fields and then have the field array :

###### Aqui tenho todos as colunas do meu banco de dados
$fields = array();
while ( $coluna = $colunas->fetch() ) { 
    $fields[] = $coluna;
}

$array = array (
    'key'       => '8b0dc65f996f98fd178a9defd0efa077',
    'module'    => 'imoveis',
    'method'    => 'busca_imoveis',
    'field'     => $fields 
);

Or, as in the comments, just create the array before the loop and get the data straight into array :

$array = array (
    'key'       => '8b0dc65f996f98fd178a9defd0efa077',
    'module'    => 'imoveis',
    'method'    => 'busca_imoveis',
    'field'     => array() 
);

while ( $coluna = $colunas->fetch() ) { 
    $array ['field'][] = $coluna;
}
    
26.12.2014 / 20:55