Checkboxes coming from the database with CodeIgniter

6

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

I needed to make a form with a list of checkboxes with those values that exist in the table.

I'm using CodeIgniter.

Here is an image of what I want:

AtthismomentIhavethismodel:

publicfunctionapoioPertendido(){$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:

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

CONTROLER:

publicfunctionproporEvento(){$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']='ProporEvento';$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>ApoioPertendido</label><divclass="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>
    
asked by anonymous 04.06.2015 / 18:48

1 answer

0

What kind of support can be more than one per call? If you can not, 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 }  ?>

In this way, you will not have to check 3 or 4 times (or according to the amount of items registered in the bank), to know what idApoio i is being sent. To get the value in your controller would look like this:

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

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

    
23.12.2015 / 19:24