form_radio in CodeIgniter does not work

1

I have a system that is made in Code Igniter 2, I have a form that contains a form_radio that stores the parameter if an employee uses invoice or not, this is the code

 <div>
   <label for="factura" class="forma" >Factura de comisionista:</label>
    <?php echo form_radio('factura', '1', FALSE, 'class="factura"'); ?>S&iacute; 
      &nbsp;&nbsp;&nbsp;&nbsp; 
    <?php echo form_radio('factura', '0', FALSE, 'class="factura"');?>No
 </div>

This is in the main view, where the employee's data is stored for the first time, now the problem is in the view where I want to update the information, just when I want to update that form_radio, if I update and save the change, the change never really applies, the form_radio stays the same.

the code I did not do, however I feel that the way in which the instruction was done is not well used

Is there any way to update this form_radio parameter correctly and save it?

annex image

    
asked by Javier 12.01.2018 в 02:34
source

1 answer

0

For this kind of thing I use this structure:

<?php

    $registro['factura'] = 1; // $comisionista->factura;

    $field = array(
      'name' => 'factura',
      'label'=>'Factura de comisionista:',
      'default' => 0,
      'values' => array( 1 => 'SI', 0 => 'NO'),
    );

    echo form_label( $field['label'], $field['name']);
    foreach ($field['values'] as $option_value => $option_label) :
      echo form_radio(
        $field['name'],
        $option_value,
        (
          set_value($field['name'], (
            (isset($registro[$field['name']]) && $registro[$field['name']] != NULL) ?
            $registro[$field['name']] :
            $field['default'])
          ) == $option_value)
      );
      echo $option_label."&nbsp;&nbsp;";
    endforeach;
?>

In the array $registro come the values of the record (what is in the database).

In the array $field I define:

  • name,
  • label,
  • default value
  • the array of possible values: valor => etiqueta .

After printing the global label of the field, I go through the possible values (field options) and print the form_radio with the following options:

form_radio('nombre', 'valor', 'true si está seleccionado')

It is the "if selected" the most curled part, for this we use set_value that has the form:

set_value("nombre", "defult")

If the field "name" already has value (after a form_validation->run() ) that value is assigned, if it is not like that in the second parameter we indicate the default that is in case it is the first time you access the form, here what we do, to know what value "default" to use, is to verify that "name", the name of the field, exists between the values of $registro if it is so we use that value, if it does not exist we use the default of the field defined in $field .

With which setvalue will have one of these values:

  • the value set since the last post (if form_validation->run() fails)
  • the value saved in $registro
  • the default value for this field defined in $field
  • This last value is the one that we compare with the value that this particular option would have, if they match the radius will go checked if they do not match is printed without checked .

        
    answered by 06.08.2018 в 16:22