Creating 7 selects with PHP and Mysql

Asked

Viewed 89 times

1

I have a database and I want to retrieve its contents and present it within <select>'s, the initial I get, I have 7 names in the database and I can present them all in the same select box, what I want now is to have 7 select’s each with the 7 names, would serve to choose in the future the order of arrival, I do not know if I made me understand, the code I have now is this:

    <select name='listagem_dia'>
    <?php
    while ($dados = mysql_fetch_array($resultado)) {
        $nome = $dados["nome"];
        $id= $dados["id"];
        echo "<ul><li><option value=".$id.">".$nome."</option></li></ul>";
    }
    ?>
    </select>

It works well only I can not duplicate the boxes depending on the number of results I have... I have already got the 7 boxes but only one name appears in each of them... someone knows how to duplicate the boxes on top depending on the number of results obtained?

  • @Rafael You will have to repeat everything 7 times, inside the while will not give.

  • @Andrey, but it was supposed to be all automatic, he repeats according to the number of results obtained that will not be constant... I’ve tried with while but it’s not working...

1 answer

1


If you want to display 7 SELECT’s with the same results, you can do so

<?php
$db = mysql_connect('localhost', 'root', 'vertrigo');
$bd = mysql_select_db('cadastrosDados', $db);

$SQL = mysql_query("SELECT * FROM cadastros");

$opts = '';

while($exe = mysql_fetch_array($SQL)){

    $opts .= '<option value="'.$exe["nome"].'">'.$exe["nome"].'</option>';
}

for($i = 1;$i<=7;$i++){

    echo '<select>';
    echo $opts;
    echo '</select>';
}
?>

Here you will create a loop 7 times with the same select

  • It does not work, had already tested, in the first correct presentation box, the remaining 6 are created but are empty

  • @Rafael I edited the code, I did so in mine and it worked. Edit the code as you need and test.

  • Now it worked, thank you so much for the tip @Alissonacioli

Browser other questions tagged

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