I have a form where among other things I have a multiple select where the types of jobs that the user does are selected.
When you sign up, choose these items in a select and record them in the BD with the format of ["1", "2", "4"] with those numbers being the ids of what you chose.
Now, now I'm analyzing the possibility that I can modify this data, so I'll take it to a profile editing page and the same selection will charge it like this:
<select id="tipoUsuario" class="ui fluid search dropdown" multiple name="clase[]">
<option value="0"></option>
<?php
//Traigo el array del perfil
$valores = json_decode ($clase, true);
$conexion = new Conexion();
$stmt = $conexion -> prepare("SELECT idclase, clase FROM usuarios_clase WHERE vigente = 1 AND visible = 1 ORDER BY idclase");
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
foreach ($valores as $key => $value) {
if ($value == $row['idclase']) { ?>
<option value = "<?php echo $row['idclase']?>" selected><?php echo utf8_encode($row['clase'])?></option>
<?php } else { ?>
<option value = "<?php echo $row['idclase']?>"><?php echo utf8_encode($row['clase'])?></option>
<?php }
}
}
?>
</select>
What happens to me is that if for example the user did not select anything in the record, in the BD I record a ["0"] and the select is loaded perfectly in the editing process.
But if the user has something loaded, that is, he chose some items, the items that are loaded in the select are duplicated. I mean for example if you chose "work A", "work B", "work D" in the select me appear
"C work"
"C work"
"Work E"
"Work E"
"F work"
"F work"
and the worst of all is that I do not understand what I'm doing wrong ...
If you can give me a hand, I thank you! Greetings