CodeIgniter Forums
[solved] A shorter way to populate a Radio button - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: [solved] A shorter way to populate a Radio button (/showthread.php?tid=55370)



[solved] A shorter way to populate a Radio button - El Forum - 10-23-2012

[eluser]Juan Velandia[/eluser]
Hello everyone, I came up this code to populate a check box depending on the previosly selected options on a radio button or to leave it empty whem nothing is received.

Perhaps yo could give an idea to make it shorther, thanks
Code:
if($this->form_data->regmotor):
                  if($this->form_data->regmotor == 'true'):
                         $check_si1 = array(
                        'name'        => 'regmotor',
                        'id'          => 'regmotor',
                        'value'       => 'TRUE',
                        'checked'     => TRUE,
                         );

                         $check_no1 = array(
                        'name'        => 'regmotor',
                        'id'          => 'regmotor',
                        'value'       => 'FALSE',
                         );
        
                    endif;  

                    if($this->form_data->regmotor == 'false'):

                         $check_no1 = array(
                        'name'        => 'regmotor',
                        'id'          => 'regmotor',
                        'value'       => 'FALSE',
                        'checked'     => TRUE,
                         );

                         $check_si1 = array(
                        'name'        => 'regmotor',
                        'id'          => 'regmotor',
                        'value'       => 'TRUE',
                         );

                    endif;
                 endif;  

                  if(!$this->form_data->regmotor):
                         $check_si1 = array(
                        'name'        => 'regmotor',
                        'id'          => 'regmotor',
                        'value'       => 'TRUE',
                         );

                         $check_no1 = array(
                        'name'        => 'regmotor',
                        'id'          => 'regmotor',
                        'value'       => 'FALSE',
                         );
        
                    endif;  

               echo 'Si'.form_radio($check_si1).'  '.'No'.form_radio($check_no1);



[solved] A shorter way to populate a Radio button - El Forum - 10-23-2012

[eluser]Lotus[/eluser]
Code:
$check_si1 = array(
    'name'        => 'regmotor',
    'id'          => 'regmotor',
    'value'       => 'TRUE'
);

$check_no1 = array(
    'name'        => 'regmotor',
    'id'          => 'regmotor',
    'value'       => 'FALSE'
);

if($this->form_data->regmotor):

    if($this->form_data->regmotor == 'true'):
        $check_si1['checked'] = TRUE;
    endif;  

    if($this->form_data->regmotor == 'false'):
        $check_no1['checked'] = TRUE;
    endif;
endif;

echo 'Si'.form_radio($check_si1).'  '.'No'.form_radio($check_no1);



[solved] A shorter way to populate a Radio button - El Forum - 10-23-2012

[eluser]benton.snyder[/eluser]
Personally I'd use jquery to save from having to submit the form, but the below is certainly shorter:

Code:
$check_si1 = array(
    'name'        => 'regmotor',
    'id'          => 'regmotor',
    'value'       => 'TRUE',
);

$check_no1 = array(
    'name'        => 'regmotor',
    'id'          => 'regmotor',
    'value'       => 'FALSE',
);

if($this->form_data->regmotor == 'true')
$check_si1['checked'] = TRUE;
elseif($this->form_data->regmotor == 'false')
$check_no1['checked'] = TRUE;

echo 'Si'.form_radio($check_si1).'  No'.form_radio($check_no1);



[solved] A shorter way to populate a Radio button - El Forum - 10-23-2012

[eluser]Juan Velandia[/eluser]
Thanks to both of you!, sometimes these kind of things are a mental mistake when there is a deadline. Best regards!