Problem when selecting php checkbox

Asked

Viewed 55 times

0

Good

I am with the following problem I am entering multiple data through checkbox that part already working now I wanted to do the validation to mark the box of those that are already inserted in the database but I am not getting are 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 
}
?>

Table Categories Products

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 ;

1 answer

1


You can use as follows (I have done according to the code you provided, only made the necessary changes, test and let us know 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 
}
?>
  • Appear all selected

  • So why this expression -> $_REQUEST['category'] === $row_cat->categoria_slug, is true. What would be the expression or what you would need to compare to to see whether or not the check should be selected?

  • I need it to check in the database for a product which are the categories you have inserted there and present as selected

  • Okay and what would be this table and what would be the field?

  • I edited the article and put the table structure

  • Dude is a little confused, what would be your table collections up there, what’s your link with categories_establishments? the $_REQUEST['category'], would need to be compared with which table after all to arrive at the 'checked' result you would need? Do you take this establishment out of somewhere? How does your logic work? It’s just a checked for any establishment or it can be multiple?

  • The collection table is where I will find the categories to list them along with the option to select I need to compare in the table categories_establishments the category field

  • @Césarsousa Try with the changes I posted now. I could not test, any error warn...

  • It worked right thanks for the help

  • @Césarsousa Dispo

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.