Problem selecting checkbox php

0

Good

I have the following problem I am inserting multiple data through checkbox that part is already working now I wanted to do the validation to check the box of those that are already inserted in the database but I am not able to get all marked.

Code

 <?php 
 $result_cat=mysql_query("select * from colecoes where activo=1");
 while($row_cat=mysql_fetch_object($result_cat)){
 ?>
 <div style="float: left; margin: 5px 5px 5px 0px;"><input type="checkbox" <?php // if($_REQUEST['categoria'] == $row_cat->categoria_slug) echo "checked='checked'"; ?> name="categoria[]" value="<?php echo $row_cat->slug; ?>" /> <?php echo $row_cat->titulo; ?></div>
<?php 
}
?>

Product categories table

CREATE TABLE IF NOT EXISTS 'categorias_estabelecimentos' (
'id' int(11) NOT NULL AUTO_INCREMENT,
'estabelecimento_id' int(11) NOT NULL,
'categoria_slug' varchar(250) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY ('id')
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;
    
asked by anonymous 16.02.2015 / 15:51

1 answer

1

You can use as follows (I already did according to the code you provided, I just made the necessary changes, test and report the result):

<input type="checkbox" name="categoria[]" <?= $_REQUEST['categoria'] === $row_cat->categoria_slug ? 'checked' : ''?>  value="<?= $row_cat->slug ?>" /> <?= $row_cat->titulo ?>

Modified according to the information passed by the user:

 <?php 
 $result_cat=mysql_query('SELECT * FROM colecoes WHERE activo=1');
 while($row_cat=mysql_fetch_object($result_cat)) {
     $result_cat_atual = mysql_query("SELECT * FROM categorias_estabelecimentos WHERE estabelecimento_id = ID AND categoria_slug = $row_cat->categoria_slug");//Modificar o id 
 ?>
 <div style="float: left; margin: 5px 5px 5px 0px;">
    <input type="checkbox" <?= mysql_num_rows($result_cat_atual) > 0 ? 'checked' : '' ?> name="categoria[]" value="<?= $row_cat->slug ?>" /> <?= $row_cat->titulo; ?>
 </div>

<?php 
}
?>
    
16.02.2015 / 15:54