Select checkboxes that come from the database in the edit form

Asked

Viewed 228 times

0

Hello, I have a user editing form that has some chekbox with city names that must be selected when the form opens. Well, I am selecting in the database the cities to which the user being edited has access and making a comparison in the tag <input> to print 'checked' if the comparison is true. It turns out that the query only returns the first database record in the array and the others does not. Can someone tell me where I’m going wrong, please? Thanks in advance. Code and more information below:

O sistema só exibe uma checckbox selecionada mesmo o banco retornando mais de uma

I had difficulty posting the code directly so I put it in the Pastebin. I hope it’s not a hassle.

    {   <?php
   include_once ('../dao/usuario_dao.php');

   $id = $_GET['id_usuario'];

   //Dados do usuário
   $resultado = sel_usuario($id);
   $reg = mysqli_fetch_assoc($resultado);

   //Dados das regionais para as quais o usuário tem acesso
   $resultado2 = sel_regionais_usuario($id);
   $regional = mysqli_fetch_assoc($resultado2);

   //var_dump($regional['id']);

?>

<div class="container-fluid">
    <div class="panel panel-primary">
        <div class="panel-heading">
            <i class="fa fa-pencil-square-o"></i> EDITAR USUÁRIO
        </div><br>

        <div id="filtros" style="padding-left: 20px">
            <a id="voltar_pagina" href="menu.php" class="btn btn-primary" title="Voltar ao início">
                <i class="fa fa-home"></i>
            </a>
            <a id="voltar_lista" href="javascript:history.back()" class="btn btn-primary" title="Voltar">
                <i class="fa fa-chevron-left"></i>
            </a>                        
        </div>

        <div class="panel-body">

            <form id="form_cad_usuario" data-toggle="validator" class="form-horizontal" method="POST" action="../config/usuario/processa_cad_usuario.php">

                <!--DADOS DO USUÁRIO-->
                <div class="page-header">
                    <h4>Dados do Usuário</h4s>
                    <hr>
                </div>


                <div class="form-group">                
                    <label for="status_check" class="col-sm-2 control-label">Ativo</label>
                    <div class="col-sm-10">
                        <div class="checkbox">
                            <label>
                                <input id="status_check" name="status_check" type="checkbox" value="ativo" checked>
                            </label>
                        </div>
                    </div>
                </div>

                <div class="form-group">
                    <label for="nome_usuario" class="col-sm-2 control-label">Nome</label>
                    <div class="col-sm-10">
                        <input
                           type="text" class="form-control" id="nome_usuario"
                           name="nome_usuario" placeholder="Nome"
                           data-error="Por favor, informe o nome completo para o usuário." required
                           value="<?php echo $reg['nome']; ?>"
                        >
                        <div class="help-block with-errors"></div>
                    </div>
                </div>

                <!--DADOS DE ACESSO-->
                <div class="page-header">
                    <h4>Dados de Acesso</h4>
                    <hr>
                </div>

                <div class="form-group">
                    <label for="login_usuario" class="col-sm-2 control-label">Usuário</label>
                    <div class="col-sm-10">
                        <input
                           type="text" class="form-control" id="login_usuario"
                           name="login_usuario" placeholder="Usuário"
                           data-error="Por favor, informe o nome de usuário que será usado no login."
                           required
                           value="<?php echo $reg['login']; ?>"
                        >
                        <div class="help-block with-errors"></div>
                    </div>
                </div>

                <div class="form-group">
                    <label for="permissao_usuario" class="col-sm-2 control-label">Permissão</label>
                    <div class="col-sm-10">
                        <select class="form-control" id="permissao_usuario" name="permissao_usuario" data-error="Por favor, selecione um nível de permissão para o usuário." required>
                            <option value="4">Jogos</option>
                            <option value="3">Regional</option>
                            <option value="2">Administrador</option>
                            <option value="1">Master</option>
                        </select>
                        <div class="help-block with-errors"></div>
                    </div>
                </div>

                <!--DADOS DA  REGIONAL-->
                <div class="page-header">
                    <h4>Regionais Permitidas</h4>
                    <hr>
                </div>

                <?php
               $resultado = lista_regionais();

               while($reg = mysqli_fetch_assoc($resultado))
               {
                   $id_regional = $reg['id'];
                   $nome_regional = $reg['nome'];

                   //var_dump($id_regional);
               ?>

                <div class="col-sm-2">
                    <div class="form-group">
                        <div class="col-sm-1">
                            <div class="checkbox">
                                <label>
                                    <label class="col-md-10" for="<?php echo"regional_$id_regional" ?>">
                                        <input
                                           type="checkbox" name="<?php echo"regional[$id_regional]"; ?>"
                                            id="<?php echo"regional_$id_regional" ?>"
                                            value="<?php echo $nome_regional; ?>"
                                            <?php
                                               if($id_regional == $regional['id'])
                                               {
                                                   echo 'checked';
                                               }
                                           ?>
                                        >    
                                        <small><?php echo $nome_regional; ?></small>
                                    </label>
                                </label>
                            </div>
                        </div>
                    </div>
                </div>

                <?php
               }
               ?>

                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                        <button id="botao_salvar" type="submit" class="btn btn-primary pull-right">Salvar <span class="glyphicon glyphicon-floppy-save"></span></button>
                    </div>
                </div>

            </form>
        </div>
    </div>
</div>}

Ahh! And if you have a more efficient way to treat this data to show them in the form I appreciate the tip. Hug!

  • When making the comparison you need a foreach to compare $regional with $id_regional

  • Even the vardump of the back that comes from the bank only returning an element? Could show the code, please?

  • I figured it out based on your tip. Thanks, man.

1 answer

1

I managed to solve the problem, I’ll leave the code here if it is of interest to someone else. Follow the php code I used inside the that generates the checkboxes.

<input 
                                        type="checkbox" name="<?php echo"regional[$id_regional]"; ?>" 
                                        id="<?php echo"regional_$id_regional" ?>" 
                                        value="<?php echo $nome_regional; ?>"
                                        <?php
                                            $resultado2 = sel_regionais_usuario($id);
                                            while($reg_usuario = mysqli_fetch_assoc($resultado2))
                                            {
                                                if($reg_usuario['id'] == $id_regional)
                                                {
                                                    echo 'checked';
                                                }
                                            }
                                        ?>
                                    >

Browser other questions tagged

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