Popular PHP dropdown error with array

Asked

Viewed 161 times

-1

Hello,

I have the following function that returns me the necessary data to popular my dropdown:

    function listaRedes($conexao){
    $dados = array();

    $resultado = mysqli_query($conexao, "SELECT DISTINCT rede FROM evolucao_originacao");
    while($valores = mysqli_fetch_array($resultado)){
        array_push($dados, $valores);
    }
    return $dados;
};

And in my dropdown, I’m making the following code:

 <?php $redesBanco = listaRedes($conexão); ?>
        <div class="form-group">
            <select class="form-control" id="dropdown-parceria">
                <option value="0">---SELECIONE---</option>
                <?php 
                    while($redesBanco){
                        echo "<option value='".$redesBanco."'>".$redesBanco."</option>";
                    }
                ?>
            </select>
        </div>

But as soon as he populates my dropdown, the result is this:: inserir a descrição da imagem aqui

  • When Voce does the while you’re still iterating under arrays, try to see what’s inside the com array var_dump($redesBanco),

  • @Rafaelacioly the array is with all the data I searched from the database.

  • The var_dump is to see the structure of your array.

2 answers

0

use foreach to traverse the array:

<?php $redesBanco = listaRedes($conexão); ?>
            <div class="form-group">
                <select class="form-control" id="dropdown-parceria">
                    <option value="0">---SELECIONE---</option>
                    <?php 
                        foreach ($redesBanco as $chave){
                            echo "<option value='".$chave[0]."'>".$chave[1]."</option>";
                        }
                    ?>
                </select>
            </div>

-2

Try to put $redesBanco[0]. Since you are trying to echo an array, it is recommended to var_dump to see the structure to write for example, <?php echo "<a href='".$redesBanco['link']."'>".$redesBanco['acesse']."</a>"; ?> But it can also be done the first way, informing the Dice with the number $redesBanco[0]. In the case of your example should be done in a way like this...:

<?php $redesBanco = listaRedes($conexão); ?>
    <div class="form-group">
        <select class="form-control" id="dropdown-parceria">
            <option value="0">---SELECIONE---</option>
            <?php 
                while($redesBanco){
                    echo "<option value='".$redesBanco[0]."'>".$redesBanco[1]."</option>";
                }
            ?>
        </select>
    </div>

I hope I’ve helped in some way

  • didn’t work @localhost

  • 1

    gives a var_dump($rediscovered); and puts the result there, so I can help better, if I have not been able

Browser other questions tagged

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