Update the status of multiple ids at the same time Mysqli php mvc

Asked

Viewed 131 times

-1

Good morning! I need to "Pick up the selected Ids" and change their status to the option you choose in select status. Bulk editing, same value in the status field for all selected Ids.

inserir a descrição da imagem aqui

IDs selecionados

Models: Os_model

 public function edit($table, $data, $fieldID, $ID)
{
    $this->db->where($fieldID, $ID);
    $this->db->update($table, $data);

    if ($this->db->affected_rows() >= 0) {
        return true;
    }

    return false;
}

Controllers: Os.php

 public function alterarCheckbox(){

    $ids =  $this->input->post('status');
    $numRegs = count($ids);

    if($numRegs > 0){

        // atualiza status no banco de dados
        foreach($this->input->post('status') as $valor){
            $dados['status'] = 1;
            $this->db->where('idOs', $valor);
            if($this->db->update('os', $dados)){
                echo $valor." > Atualizado <br>";
            }
        }

    } else {
        echo "Nenhum item foi marcado.";
    }
}

os php.

<form name="form1" id="form1" method="post" action="<?php echo current_url(); ?>" onSubmit="return valida_campos();">
  <?php echo form_hidden('idOs',$result->idOs) ?>

  <label for="status" class="control-label"></label>
  <div class="controls">
    <select class="span2" name="status[<?php echo $res['id'] ?>]" id="status" value="">
      <option>status</option>
      <option <?php if ($result->status == 'Aguardando') { echo 'selected'; } ?> value="Aguardando">Aguardando</option>
      <option <?php if ($result->status == 'Liberado') { echo 'selected'; } ?> value="Liberado">Liberado</option>
    </select>

                          <span>
                             <button type="submit" class="btn" style="padding-top: px;border-top-width: px;margin-bottom: 9px;"><i class="fas fa-sync-alt"></i></button>
                          </span>
  </div>

/*input para conferir que os IDs foram selecionados*/
 <input class="span6" type="text" name="grupo_os" id="grupo_os" />

    <table>
      <thead>
        <tr>
          <th><input name="todos" id="todos" type="checkbox" value="todos" onchange="if(this.checked == true){marcacao_generico('form1',1,'os','grupo_os');}else{marcacao_generico('form1',0,'os','grupo_os');}" /> </th>
          <th>N° OS</th>
          <th>Campo 1</th>
          <th>Status</th>
        </tr>
      </thead>
      <tbody>
        <?php
             
                        foreach ($results as $r) {


                            echo '<tr>';
                            echo '<td><input type="checkbox" name="os" value="'. $r->idOs. '" onclick="acao_coleta_generico(\''. $r->idOs. '\', \'grupo_os\' );"></td>';
                            echo '<td>' . $r->idOs . '</td>';
                            echo '<td>' . $r->campo1 . '</td>';
                            echo '<td>' . $r->status . '</td>';
                            echo '</tr>';
                        } ?>
      </tbody>
    </table>
</form>

/*Código para CheckBox*/

<?PHP
if(isset($_POST['grupo_os']))
  {
    $os = '';
    $os = explode(",",str_replace(':','',$_POST['grupo_os']));
    ?>
  <br>
  <?PHP
    for($i = 0;$i < count($os) - 1; $i++)
      {
        echo($os[$i]);
        echo "<br>";
      }
    ?>
    <hr>
    <?PHP
  }
?>

      <script type="text/javascript">
        function acao_coleta_generico(valor, campo_alvo) {
          if (document.getElementById(campo_alvo).value.indexOf(':' + valor + ':,') == -1) {
            document.getElementById(campo_alvo).value += ':' + valor + ':,';
          } else {
            var grupo = document.getElementById(campo_alvo).value;
            grupo = grupo.replace(':' + valor + ':,', '');
            document.getElementById(campo_alvo).value = grupo;
          }
        }

        function marcacao_generico(formulario, opcao, campo, campo_alvo) {
          for (i = 0; i < document.getElementById(formulario).elements.length; i++) {
            if (document.getElementById(formulario).elements[i].name == campo) {
              if (document.getElementById(formulario).elements[i].checked != opcao) {
                document.getElementById(formulario).elements[i].checked = opcao;
                acao_coleta_generico(document.getElementById(formulario).elements[i].value, campo_alvo);
              }
            }
          }
        }

        function valida_campos() {
          if (document.getElementById('grupo_os').value == '') {
            alert('Por favor, selecione ao menos um item.');
            return false;
          }

        }
      </script>

  • Is it me or do you create various elements like the same id?

  • 1

    @Augusto Vasques As in the table the Id is auto-increment each time I add a record generates an Id number. So I have no way to define the id that will be generated. Example, record n 1 will be generated Id 1, record n. 2 will be generated Id 2. I don’t know if this was the expected answer. I thank you for your attention. I inserted an image of how it looks on my page.

1 answer

0

It’s not very clear, but if I understand correctly, you can use find_in_set to update all ids without having to loop.

update `TABELA` set status = 1
where find_in_set( cast( id as char ) , ? )

The find_in_set does not work with the incoming input type ids, so to bypass you use cast char

Browser other questions tagged

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