Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Dexter Morgan

Editar e Atualizar varios Checkbox

Recommended Posts

Bom dia, preciso fazer uma seleção de opcionais com checkbox, mas não consigo Editar e Atualizar; acho que comecei do modo certo, coloquei os dados no BD:

--
-- Estrutura da Tabela Garagem Automovel Opcionais
--

CREATE TABLE garagem_automovel_opcionais(
	id_garagem_automovel_opcional int(11) NOT NULL auto_increment,
	garagem_automovel_opcional varchar(255) NOT NULL,
	PRIMARY KEY(id_garagem_automovel_opcional)
) ENGINE = InnoDB;

INSERT INTO garagem_automovel_opcionais VALUES (1, '4 x 4');
INSERT INTO garagem_automovel_opcionais VALUES (2, 'ABS');
INSERT INTO garagem_automovel_opcionais VALUES (3, 'Air Bag Lateral');
INSERT INTO garagem_automovel_opcionais VALUES (4, 'Air Bag Motorista');
INSERT INTO garagem_automovel_opcionais VALUES (5, 'Air Bag Passageiro');
INSERT INTO garagem_automovel_opcionais VALUES (6, 'Ajuste de Altura');
INSERT INTO garagem_automovel_opcionais VALUES (7, 'Ajuste Elétrico');
INSERT INTO garagem_automovel_opcionais VALUES (8, 'Alarme');
INSERT INTO garagem_automovel_opcionais VALUES (9, 'Ar Condicionado');
INSERT INTO garagem_automovel_opcionais VALUES (10, 'Ar Quente');
INSERT INTO garagem_automovel_opcionais VALUES (11, 'Bancos de Couro');
INSERT INTO garagem_automovel_opcionais VALUES (12, 'Blindado');
INSERT INTO garagem_automovel_opcionais VALUES (13, 'Cambio Aut.');
INSERT INTO garagem_automovel_opcionais VALUES (14, 'CD');
INSERT INTO garagem_automovel_opcionais VALUES (15, 'Computador');
INSERT INTO garagem_automovel_opcionais VALUES (16, 'Couro');
INSERT INTO garagem_automovel_opcionais VALUES (17, 'Desembaçador Traseiro');
INSERT INTO garagem_automovel_opcionais VALUES (18, 'Direção Hidráulica');
INSERT INTO garagem_automovel_opcionais VALUES (19, 'DVD');
INSERT INTO garagem_automovel_opcionais VALUES (20, 'Engate');
INSERT INTO garagem_automovel_opcionais VALUES (21, 'Entrada USB');
INSERT INTO garagem_automovel_opcionais VALUES (22, 'Farol Milha');
INSERT INTO garagem_automovel_opcionais VALUES (23, 'Farol Neblina');
INSERT INTO garagem_automovel_opcionais VALUES (24, 'Lim. Traz.');
INSERT INTO garagem_automovel_opcionais VALUES (25, 'Lona Maritma');
INSERT INTO garagem_automovel_opcionais VALUES (26, 'Prot. Caçamba');
INSERT INTO garagem_automovel_opcionais VALUES (27, 'Rádio');
INSERT INTO garagem_automovel_opcionais VALUES (28, 'Ret. Elétrico');
INSERT INTO garagem_automovel_opcionais VALUES (29, 'Retrovisores Elétricos');
INSERT INTO garagem_automovel_opcionais VALUES (30, 'Roda Liga');
INSERT INTO garagem_automovel_opcionais VALUES (31, 'Rodas de Liga Leve');
INSERT INTO garagem_automovel_opcionais VALUES (32, 'Trava');
INSERT INTO garagem_automovel_opcionais VALUES (33, 'Teto Solar');
INSERT INTO garagem_automovel_opcionais VALUES (34, 'Toca Fitas');
INSERT INTO garagem_automovel_opcionais VALUES (35, 'Vidro');

-- --------------------------------------------------------

e na tabela de automoveis, eu coloquei um opcionais text:

--
-- Estrutura da Tabela Garagem Automoveis
--

CREATE TABLE garagem_automoveis(
	id_garagem_automovel int(11) NOT NULL auto_increment,
	id_garagem int(11) NOT NULL default '0',
	id_garagem_automovel_marca int(11) NOT NULL default '0',
	id_garagem_automovel_cor int(11) NOT NULL default '0',
	id_garagem_automovel_porta int(11) NOT NULL default '0',
	id_garagem_automovel_combustivel int(11) NOT NULL default '0',
	id_garagem_automovel_ano int(11) NOT NULL default '0',
	id_garagem_automovel_marcha int(11) NOT NULL default '0',
	id_garagem_automovel_situacao int(11) NOT NULL default '0',
	modelo varchar(255) NOT NULL default '',
	km varchar(255) NOT NULL default '',
	placa varchar(255) NOT NULL default '',
	obs text NOT NULL,
	opcionais text NOT NULL,
	preco double(10, 2) NOT NULL DEFAULT '00.00',
	data date NOT NULL,
	data_mod date NOT NULL,
	foto varchar(255) NOT NULL,
	publicado char(1) default 'N',
	click int(11) NOT NULL,
	PRIMARY KEY(id_garagem_automovel),
	INDEX i2 (id_garagem, id_garagem_automovel_marca, id_garagem_automovel_cor, id_garagem_automovel_porta, id_garagem_automovel_combustivel, id_garagem_automovel_ano, id_garagem_automovel_marcha, id_garagem_automovel_situacao),
	FOREIGN KEY (id_garagem) REFERENCES garagens(id_garagem) ON DELETE CASCADE,
	FOREIGN KEY (id_garagem_automovel_marca) REFERENCES garagem_automovel_marcas(id_garagem_automovel_marca) ON DELETE CASCADE,
	FOREIGN KEY (id_garagem_automovel_cor) REFERENCES garagem_automovel_cores(id_garagem_automovel_cor) ON DELETE CASCADE,
	FOREIGN KEY (id_garagem_automovel_porta) REFERENCES garagem_automovel_portas(id_garagem_automovel_porta) ON DELETE CASCADE,
	FOREIGN KEY (id_garagem_automovel_combustivel) REFERENCES garagem_automovel_combustiveis(id_garagem_automovel_combustivel) ON DELETE CASCADE,
	FOREIGN KEY (id_garagem_automovel_ano) REFERENCES garagem_automovel_anos(id_garagem_automovel_ano) ON DELETE CASCADE,
	FOREIGN KEY (id_garagem_automovel_marcha) REFERENCES garagem_automovel_marchas(id_garagem_automovel_marcha) ON DELETE CASCADE,
	FOREIGN KEY (id_garagem_automovel_situacao) REFERENCES garagem_automovel_situacoes(id_garagem_automovel_situacao) ON DELETE CASCADE
) ENGINE = InnoDB;

-- --------------------------------------------------------

eu consigo fazer o cadastro, ele salva no BD, os ID exe: 8, 19, 21

porém, preciso da tela que edita e atualiza:

<?php

include "connection.php";


$opcionais_explode = explode(", ", $opcionais);

$sql = "SELECT * FROM garagem_automovel_opcionais ORDER BY garagem_automovel_opcional ASC";
$resultado = mysql_query($sql) or die (mysql_error());
if(mysql_num_rows($resultado) < 1){
}
else{
while($linha = mysql_fetch_array($resultado)){
$id_garagem_automovel_opcional = $linha["id_garagem_automovel_opcional"];
$garagem_automovel_opcional = $linha["garagem_automovel_opcional"];

foreach($opcionais_explode AS $valores){
if($id_garagem_automovel_opcional == $valores){
echo "<td width=\"15%\"> </td>";
echo "<td width=\"30%\"><input class=\"input\" name=\"opcionais[]\" type=\"checkbox\" value=\"$id_garagem_automovel_opcional\" checked=\"checked\" /> $garagem_automovel_opcional</td>";
}
else{
echo "<td width=\"15%\"> </td>";
echo "<td width=\"30%\"><input class=\"input\" name=\"opcionais[]\" type=\"checkbox\" value=\"$id_garagem_automovel_opcional\" /> $garagem_automovel_opcional</td>";
}
}

}
}
echo "</table>";

?>

o arquivo que edita acima repete por causa do foreach dentro do while:

10za6ud.jpg

 

ele até consegue jogar as que estão no BD como checked, mas faz essa repetição;

e também, como eu atualizo, sem que duplique o valor na tabela ?

Compartilhar este post


Link para o post
Compartilhar em outros sites

Não tem a necessidade do foreach

 

só verificar se existe no array

$linha

os ids

 

para isso use

 

is_array

in_array

 

Exemplo

 if (isset($atividade_id) && is_array($atividade_id) && in_array($atividade['ConfiguracaoAtividade']['id_atividade'], $atividade_id)) {
    // Set the $checked string
    $checked = "checked='checked'";
} else {
    $checked = " ";
}

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.