The tables in the example
Table cor_fundo
Table produto
The code is commented.
$pdo = db_connect();
$sql = "SELECT * FROM cor_fundo";
$query = $pdo->prepare($sql);
$query->execute();
$result = $query->fetchAll(PDO::FETCH_ASSOC);
//quantidade de cores na tabela cor_fundo
$quant = count($result);
//declaração das variáveis
for ($k = 0; $k <= $quant; $k++) {
$item = "z".$k;
$$item = "";
}
$option = "";
$id="";
$produto_cor_fundo="";
$produto_cor_fundo_novo="";
$userSelecionado="";
$userID="";
$usuarioSel="";
$selec="";
// verificação da possibilidade de update
if ((isset($_POST["cor_fundo"]))&&($_POST["userID"]==$_POST["userID_Old"])) {
//array das cores selecionadas
$optionArray = $_POST["cor_fundo"];
for ($i = 0; $i < count($optionArray); $i++) {
$produto_cor_fundo_novo=$produto_cor_fundo_novo.$optionArray[$i].",";
}
$produto_cor_fundo_novo = substr($produto_cor_fundo_novo,0,-1);
//condição para que haja update
if ($produto_cor_fundo_novo!=$_POST["produto_cor_fundoOld"]){
$id=$_POST["userID"];
$sql = "UPDATE produto SET produto_cor_fundo='$produto_cor_fundo_novo' WHERE id='$id'";
$query = $pdo->prepare($sql);
$query->execute();
}
}
//consulta das cores/usuario selecionado
if (isset($_POST["userID"])) {
$id=$_POST["userID"];
$sql = ("SELECT produto_cor_fundo,usuario FROM produto WHERE id = '".$id."'");
$query = $pdo->prepare($sql);
$query->execute();
$result2 = $query->fetchAll(PDO::FETCH_ASSOC);
foreach ($result2 as $row) {
$produto_cor_fundo = $row['produto_cor_fundo'];
$usuarioSel = $row['usuario'];
}
$cores = explode(",", $produto_cor_fundo);
$contad = count($cores);
$userSelecionado = "Usuario selecionado: ".$usuarioSel;
//criação das variaveis dinamicas com o atributo checked para os checkbox do usuario
for ($k = 0; $k < $contad; $k++) {
$item = "z".$cores[$k];
$$item = " checked";
}
}
$i=1;
foreach ($result as $row) {
$id = $row['cor_fundo_id'];
$nome = $row['cor_fundo_nome'];
if (isset($usuarioSel)) {
$item = "z".$i;
$option.='<label class="checkbox-inline"><input type="checkbox" name="cor_fundo[]" value="'.$id.'" '.$$item.'>'.$nome.'</label>';
}else{
$option.='<label class="checkbox-inline"><input type="checkbox" name="cor_fundo[]" value="'.$id.'">'.$nome.'</label>';
}
$i=$i+1;
}
//construção do select
$sql = ("SELECT id,usuario FROM produto");
$query = $pdo->prepare($sql);
$query->execute();
$result3 = $query->fetchAll(PDO::FETCH_ASSOC);
foreach ($result3 as $row) {
$id = $row['id'];
$usuario = $row['usuario'];
if ($usuarioSel==$usuario){
$selec.="<option value=\"".$id."\" selected>".$usuario."</option>";
}else{
$selec.="<option value=\"".$id."\">".$usuario."</option>";
}
}
echo $userSelecionado;
echo '<br>';
echo '<form method="post" action="">';
if (isset($_POST["userID"])){
echo '<input type="hidden" name="userID_Old" value="'.$_POST["userID"].'">';
echo '<input type="hidden" name="produto_cor_fundoOld" value="'.$produto_cor_fundo.'">';
}
echo "\n";
echo $option;
echo "\n";
echo '<br>';
echo '<select name="userID" size="4">';
echo $selec;
echo '</select>';
echo "\n";
echo '<br><button type="submit">Enviar</button>';
echo "\n";
echo '</form>';
This solution is based on creating dynamic variables also known as variables or even variables created during PHP execution. In the loop for ($k = 0; $k < $contad; $k++) {
through the variables $item
create the dynamic variables $$item
for each substring contained in the array $cores
, which will serve to print the attribute checked
in the selected options.
Have you tried adding the
checked
in the input?<input type="checkbox" name="cor_fundo[]" value="'.$id.'" checked>
– DNick
if I put it the way you told me... selects all, and not just those who are in the bank
– Betinho Silva
How do you save the option chosen by the user?
– DNick
@Djalmamanfrin writes in a column as array. Ex. dog, cat, rabbit = 3.4.5
– Betinho Silva
This doesn’t seem to make much sense. You’re selecting colors in the database and listing them in the form of
checkbox
. What column would that be "in array form"?– Woss
Your MER was not planned correctly. What is the list of client and background tables?
– DNick
In the table "bottom" I have the following columns: cor_bottom and bottom cor_name -- I relation with the table "product" by the following column: bottom product_cor_bottom. --- When I register a product it lists in checkbox the colors (background) that will appear in the product for the customer to buy. --- The site administrator selects the colors and they are saved in the database as an array (3,4,5)
– Betinho Silva
Okay and where’s that amount
produto_cor_fundo
for you to know which colors are selected? Is it in some variable that you omitted in the question? In fact, edit the question and add this explanation of the last direct comment in the statement. Comment should not be used to give additional information. This only happens if the question is not clear enough.– Woss