ré-affichage checkboxs depuis bdd pour update
Bonsoir,
Je bloque depuis pas mal de temps sur un problème. J'ai beaucoup cherché et ai tenté, sans succès, la solution de la discussion sur ce même forum : https://www.developpez.net/forums/d1...-checkbox-bdd/
Ce qui fonctionne :
J'ai un formulaire avec des checkbox. On peut en choisir plusieurs.
Les choix sont enregistrés en bdd dans une table associative.
Les utilisateurs souhaitent avoir la possibilité de pouvoir modifier le ou les choix.
Ce qui ne fonctionne pas :
J'arrive bien à récupérer les infos de la bdd. Mais je voudrais, dans le formulaire de modification, réafficher toutes les checkboxs et que celles qui ont été sélectionnées initialement soient cochées.
Ensuite je ferai un update.
Avec le code ci-dessous, toutes les checkboxs sont cochées alors que seulement 2 devraient l'être.
Mon code (d'après la discussion précitée) :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| <?php
$base = include 'include/connect.inc.php';
$resultat = $base->query('
SELECT atm.`ID_texte`, atm.`ID_metal`, m.`ID_metal`, m.`nom_metal`
FROM AS_TEXTE_METAL atm, METAL m
WHERE atm.`ID_metal` = m.`ID_metal`
AND atm.`ID_texte` = "'.$_SESSION['ID_texte'].'"
') or die($mysqli->error.__LINE__);
while($row = $resultat->fetch()) { $arr[] = $row; }
// transformation de la chaîne en tableau
$metal_array = explode(',', $row['nom_metal']);
foreach($metal_array as $valeur) {
?>
<?php if (in_array("$metal_array[0]", $metal_array)) { $check = "checked"; } else { $check = ''; } ?> <input name="metal[]" type="checkbox" value="1" <?php echo $check; ?> />
argent
<?php if (in_array("$metal_array[1]", $metal_array)) { $check = "checked"; } else { $check = ''; } ?> <input name="metal[]" type="checkbox" value="2" <?php echo $check; ?> />
bronze
<?php if (in_array("$metal_array[2]", $metal_array)) { $check = "checked"; } else { $check = ''; } ?> <input name="metal[]" type="checkbox" value="5" <?php echo $check; ?> />
cuivre
<?php if (in_array("$metal_array[3]", $metal_array)) { $check = "checked"; } else { $check = ''; } ?> <input name="metal[]" type="checkbox" value="8" <?php echo $check; ?> />
étain
<?php if (in_array("$metal_array[4]", $metal_array)) { $check = "checked"; } else { $check = ''; } ?> <input name="metal[]" type="checkbox" value="9" <?php echo $check; ?> />
fer
<?php if (in_array("$metal_array[5]", $metal_array)) { $check = "checked"; } else { $check = ''; } ?> <input name="metal[]" type="checkbox" value="10" <?php echo $check; ?> />
or
<?php if (in_array("$metal_array[6]", $metal_array)) { $check = "checked"; } else { $check = ''; } ?> <input name="metal[]" type="checkbox" value="11" <?php echo $check; ?> />
plomb
<?php if (in_array("$metal_array[7]", $metal_array)) { $check = "checked"; } else { $check = ''; } ?> <input name="metal[]" type="checkbox" value="12" <?php echo $check; ?> />
autre
<?php
}
?> |