Select checkboxes that come from the database in the edit form

0

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

I had difficulty posting the code directly then I put in the pastebin. I hope this is not a nuisance.

    {   <?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 of handling these data to show them in the form I appreciate the tip. Hugs!

    
asked by anonymous 13.07.2016 / 19:37

1 answer

1

I was able to solve the problem, I'll leave the code here if it is of interest to another person. Here is the php code I used inside that generates 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';
                                                }
                                            }
                                        ?>
                                    >
    
14.07.2016 / 16:47