Checkboxes from Codeigniter database

Asked

Viewed 590 times

8

I have a database with a table called tblapoio, with 4 fields (id, descricao, valor, tag)

Needed to make a form with a list of checkboxes with these values that exist in the table.

I am using Codeigniter.

Follow a picture of what I want: base de dados para checkbox

Right now I have this model:

public function apoioPertendido(){      

    $query = $this->db->get('tblapoio');
        if($query->num_rows()>0){
            foreach ($query->result() as $rows)) {


            $apoio = array(

                'idapoio' => $rows->idApoio,
                'descricao' => $rows->descricao,
                'valor' => $rows->valor,
                'tag' => $rows->tag,
                );
        }

        return $apoio;

RESOLVED:

MODEL:

 public function apoioPertendido(){     

    $query = $this->db->get('tblapoio');        
    return $query->result(); 


}

CONTROLER:

public function proporEvento(){

            $natureza = $this->evento_model->naturezaEvento();
            $apoio = $this->evento_model->apoioPertendido();
            $espaco = $this->evento_model->espaco();
            $material = $this->evento_model->material();
            $suportgraf = $this->evento_model->suporteGrafico();
            $audiovisual = $this->evento_model->audioVisual();                  

            $data['title']              = 'Propor Evento';
            $data['naturezaEvento']     = $natureza;
            $data['apoioPertendido']    = $apoio;
            $data['espaco']             = $espaco;
            $data['material']           = $material;
            $data['suporteGraf']        = $suportgraf;
            $data['audioVisual']        = $audiovisual;



            $this->load->view('cliente/clienteheaderdash_view', $data);
            $this->load->view('cliente/clientemenu_view', $data);
            $this->load->view('cliente/clienteproporevento_view', $data);
            $this->load->view('cliente/clientefooterdash_view', $data);
        }

VIEW:

label>Apoio Pertendido</label>
          <div class="form-group">
                 <?php foreach ($apoioPertendido as $row) { ?>
                 <label>
                  <input type="checkbox" class="flat-red" name="<?php  echo $row->tag ?>" value="<?php   echo $row->idApoio  ?>"/> <?php   echo $row->descricao;  ?>
                   </label>
                   </br>
                <?php }  ?>
          </div>
  • 1

    please explain better and if you’ve already made some part of the code try to post here.

  • 1

    Post the resolution in the answer area, not in the question itself.

2 answers

0

Can the type of support be more than one per call? If you can’t, follow my tip, try using radiobuttons with the same name, as follows:

<?php foreach ($apoioPertendido as $row) { ?>
<label>
<input type="radio" class="flat-red" id="apoio" name="apoio" value="<?php   echo $row->idApoio  ?>"/> <?php   echo $row->descricao;  ?>
</label>
</br>
<?php }  ?>

Because that way, you don’t need to check 3 or 4 times (or according to the number of items registered in the bank), to know which the idApoioi that is being sent. To receive the value in your controller would look like this:

$tua_variavel = $this->input->post('apoio');

Remembering that, this solution will only serve if each call has a type of support, otherwise, in addition to a relationship N to N in the database, you must go through another foreach inside the controller that will receive the data.

0

Currently I see that you are printing inputs with various names (different tags). But currently you still have control of it, in the future when doing the POST it may be necessary to read again all available tags during the sending for example to know which inputs are on the page, this can be fixed using an array in your POST:

In your VIEW:

<?php foreach ($apoioPertendido as $row) { ?>
<label>
    <input type="checkbox" class="flat-red" name="apoio[]" value="<?php   echo $row->idApoio  ?>"/> <?php   echo $row->descricao;  ?>
</label>
</br>
<?php }  ?>

name="support[]"

This way all checkboxes marked with the name support[] will be transformed into an Array with the value of each when doing the POST.

Browser other questions tagged

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