Edit status of multiple records with checkbox checked?

0

I need to send an array with the ids of the records that have the checkbox checked, to edit the status field at one time. Note1: All records will suffer the same change listed in a select. Note2: I can edit each record individually through a link with its respective id.

<form action="<?php echo current_url(); ?>" id="formExportacao" method="post" class="form-horizontal" >
  <?php echo form_hidden('idExportacao',$result->idExportacao) ?>
     <div class="control-group">

<div class="control-group">
   <label for="status" class="control-label"><span class="required">Status*</span></label>
    <div class="controls">
       <select class="span3" name="status" id="status" value="">
       <option <?php if($result->status == 'Faturado'){echo 'selected';} ?> value="Faturado">Faturado</option>                      
       <option <?php if($result->status == 'Finalizado'){echo 'selected';} ?> value="Finalizado">Finalizado</option>
       <option <?php if($result->status == ''){echo 'selected';} ?> value="Não Faturado">Não faturado</option>                                     
      </select>             
   </div>
</div>    
<div class="form-actions">
 <div class="span12">
  <div class="span6 offset3">
   <button type="submit" class="btn btn-primary"><i class="icon-ok icon-white"></i> Alterar</button>
   <a href="<?php echo base_url()?>index.php/exportacao" id="btnAdicionar" class="btn"><i class="icon-arrow-left"></i> Voltar</a>
   </div>
  </div>
 </div>
</form>

Controller: Export.php
This function is adapted from the "edit individually" The editStatus function will only be for the status, and the other case will have some wrong data entered, being able to change all its fields, but this is already solved.

function editarStatus() {

    $this->load->library('form_validation');
    $this->data['custom_error'] = '';

    if ($this->form_validation->run('exportacao') == false) {
        $this->data['custom_error'] = (validation_errors() ? '<div class="form_error">' . validation_errors() . '</div>' : false);
    } else {          
        $data = array(
            'status' => $this->input->post('status')
        );
        if ($this->fatexpo_model->editStatus('exportacao', $data, 'idExportacao', $this->input->post('idExportacao')) == TRUE) {
            $this->session->set_flashdata('success', 'Registro editado com sucesso!');
            redirect(base_url() . 'index.php/exportacao/editarStatus/'.$this->input->post('idExportacao'));
        } else {
            $this->data['custom_error'] = '<div class="form_error"><p>Ocorreu um erro.</p></div>';
        }
    }

    $this->data['result'] = $this->fatexpo_model->getById($this->uri->segment(3));
    $this->data['view'] = 'exportacao/editarExpoStatus';
    $this->load->view('tema/topo', $this->data);

}

Model: fatexpo_model.php

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

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

    return FALSE;  
}

In the page of my table, I can display the ids marked by the checkbox in this way:

<button onclick='pegaIDs()'>Pegar IDs</button>
<p id='result'></p>

<script type="text/javascript">
    function pegaIDs(){
      var ids = [];
      $(".check:checked").each(function( index ) {
       ids.push($(this).attr('id'));
      });
      $("#result").html(ids.join('<br />'));
    }
</script>
    
asked by anonymous 06.11.2017 / 13:50

1 answer

0

Do as follows: Create a method named alterCheckbox, and place the action of your form on it, and upon receiving it, will update according to the selected ones.

    // Excluir com checkbox
    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('idExportacao', $valor);
                if($this->db->update('exportacao', $dados)){
                    echo $valor." > Atualizado <br>";
                }
            }

        } else {
            echo "Nenhum item foi marcado.";
        }
    }
    
06.11.2017 / 17:56