PHP MYSQLi Array no select html

3

I'm a beginner in php and I'm developing a small system for my company that already serves as a study, because I really like this area, the question is simple, I have the following code:

<?php
include('conn/conexao.php');
$edt = $_GET['edital'];
$sql = "SELECT * FROM edt_cadastro WHERE id = $edt";
$res = $mysqli->query($sql);
$num = $res->num_rows;

for ($i = 0; $i < $num; $i++) {
  $dados = $res->fetch_array();
  $arrRefs[$dados['id']] = $dados['ref0'];
}
?>

<label>Referências:</label>
<select name="ref" id="ref">
  <?php foreach($arrRefs as $value => $nome){
    echo "<option value='{$value}'>{$nome}</option>";
  }
?>
</select>

With this code it identifies the option of a previous select to load that select above (with jQuery), until it works right, only that the value that comes from the bank for this select, comes as an example array "1,2,3" eu I would like it to show me a list with 1 then the 2, the 3 and so on, the bank is separated with comma "," (1,2,3) I already tried to use the explode option but then in the list only appears Last record in my example number 3. Could anyone help? Thank you

    
asked by anonymous 29.12.2014 / 14:00

1 answer

2

I was able to, with the following cod:

<?php           
include('conn/conexao.php');
$edt = $_GET['edital'];
$sql = "SELECT * FROM edt_cadastro WHERE id = $edt";
$res = $mysqli->query($sql);
$i = 0;
?>
<label>Referências:</label>
    <select name="ref" id="ref">
        <?php while ($row = $res->fetch_array()) {
            $referencias = $row['ref0'];
            $quantidade_ref = explode(",",$referencias);
            $total=count($quantidade_ref);
            $n = 0;
            while($n < $total) {
                $i++;
                echo '<option value="'.$i.'">'.$quantidade_ref[$n];
                $n++;
            }           
        }?>
    </select> 
    
29.12.2014 / 15:05