Edit status of multiple records with checkbox marked?

Asked

Viewed 557 times

1

I need to send an array with the id of the records that have the checkbox checked, to edit the status field at once. Note1: All records will undergo the same change listed in a select. Note 2: I can edit each record individually via 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" function The editarStatus function will only be for the status, and the other if there is some data entered wrong, can 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;  
}

On my table page, I can display the checkbox-marked ids 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>
  • You tried to foreach() the $_POST to edit the marked ones?

  • @Andrébaill could give me an example for kindness?

  • @Andrébaill can help friend?

  • *Do it this way: Create a method called alterarCheckbox, and put the action of your form to this, and upon receiving, will update according to the selected. How do I put the action into the form? Does the Heckbox method get inserted into Controller? Could someone please enlighten me? @Matheus Madalozzo, like you did?

1 answer

0


Do it this way: Create a method called alterarCheckbox, and put the action of your form to this, and upon receiving, will update according to the selected.

    // 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.";
        }
    }

Browser other questions tagged

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