How to put a DB column loop inside an array?

Asked

Viewed 765 times

-1

I have a database with dynamic columns that can appear one time, another time not... can also be added more columns or even extracted the 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 recovered all the columns from my database and now I need to plant them inside the place that is arrow each comma field at the end and line break. I tried to put the loop inside the array but it didn’t work.

How am I going to put the columns of my bench there in place of the arrow?

  • 2

    Why not declare this array before while, and while putting $array['field'][] = $coluna["Field"];

  • I won’t know how to do it, you can help me?

  • If this array should have the contents that are in the table then the response of the bigown is what you need and is much cleaner.

  • Can you help me in Updating of what I managed to do. He is assigning the return in $array['field'] but this is inserting the index number. I need exactly the code demonstrated in Updating.

  • 2

    Marcos, you’ve been transforming the question and revealing new problems that weren’t in the original question. So the first answers lose meaning and the question becomes a salad. Please ask specific questions for specific problems and if you have a new problem that you cannot solve yourself ask a new question. As you noticed I went back on your question updates. Now the accepted answer is kind of out of date...

4 answers

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"];
}

That way that $array['field'] will be filled within while, and passed this code the array is "filled".

  • 2

    +1 Good solution if the problem requires extracting a particular column from the SHOW COLUMNS.

4

I don’t know if I understand, but I think that’s all:

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

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

  • 1

    +1 Excellent solution if the idea is to pass what comes from SHOW COLUMNS for entry into the other array: $array['field'] = $colunas->fetchAll(PDO::FETCH_ASSOC);.

  • @Zuul well noticed I moved there.

2

Simply create an array by taking the fields and then make the "field" receive this 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 stated in the comments just create the array before the loop and receive the data directly on array:

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

while ( $coluna = $colunas->fetch() ) { 
    $array ['field'][] = $coluna;
}
  • Can you help me with this question which is similar but different? Thank you. http://answall.com/questions/45259/likesearch data-do-banco-datas-e-coloc%C3%A1-los-num-array

1


Based on the update and here, the only change is in place a numeric index define a key which is composed by the description of the database field which 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'];

Exit:

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'
  • Just one observation. This output will not run my SOAP. I need it to be 'DATA' => 'Data cadastro' ... And I won’t be able to ride $array_banco. Thank you for your help.

  • @Marcosvinicius, I changed the answer, $array_banco has the same structure as an array returned by fetchAll(), I made it just for test.

  • I’m almost getting to what I need. Can you give me one more strength in Update 2?

  • I don’t understand, in the question code you’re recreating $array after the foreach

Browser other questions tagged

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