CodeIgniter Forums
Help optimizing form code (checking IDs and radio buttons) - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Help optimizing form code (checking IDs and radio buttons) (/showthread.php?tid=29786)



Help optimizing form code (checking IDs and radio buttons) - El Forum - 04-21-2010

[eluser]Wayne Smallman[/eluser]
Hi guys!

I've had to create a load of messy code to get around problems I'm having with set_radio(). What I'd really appreciate is any advice as to how this code might be tidied up using any pre-build functions to replace my code.

The bulk of the problems I'm having revolve around error checking, which all appears to work fine except for the record ID and radio buttons.

The following is the code I've used to work out which radio values were checked in the previous form submission:
Code:
// define default radio button values on the first pass
    if (!empty($this->arrayClassAttributes['results']['entries'][0]['category'])):

        switch ($this->arrayClassAttributes['results']['entries'][0]['category']) {
    
            case 'cash':
    
                $checkvaluecash = true;
                $checkvaluebank = false;
    
            break;
    
            case 'bank':
    
                $checkvaluecash = false;
                $checkvaluebank = true;
    
            break;

        }

    endif;

    // define default radio button values on any subsequent passes
    switch ($this->input->post('category')) {

        case 'cash':

            $checkvaluecash = true;
            $checkvaluebank = false;

        break;

        case 'bank':

            $checkvaluecash = false;
            $checkvaluebank = true;

        break;

    }

    $input_common['cash'] = array (
        'name' => 'category',
        'id' => 'cash',
        'value' => 'cash',
        'checked' => $checkvaluecash
    );

    $input_common['bank'] = array (
        'name' => 'category',
        'id' => 'bank',
        'value' => 'bank',
        'checked' => $checkvaluebank
    );
The following is the code I've used to work out what the ID of the record is from the previous form submission:
Code:
// if there's no URL parameter for the entry ID...
if (($this->uri->segment(4) === false)

    // ... and there's no post ID value for the entry, then it's a totally new entry
    && ($this->input->post('id') === false)):

    $url = config_item('base_url') . "sales";

    $buttontitle = "Add";

else:

    $url = config_item('base_url') . "sales/edit";

    $id = ($this->uri->segment(4)) ? $this->uri->segment(4) : $this->input->post('id');

    $buttontitle = "Edit";

endif;

Any help would be greatly appreciated!


Help optimizing form code (checking IDs and radio buttons) - El Forum - 04-21-2010

[eluser]vitoco[/eluser]
Code:
// THIS CODE IS SMALLER ========================================================
    $checkvaluecash = ( $this->input->post('category') == 'cash' ) ? true : false ;
    $checkvaluebank = ! $checkvaluecash ;
    // =============================================================================

    // GET BOTH VALUES ONCE...I THINK IT'S BETTER THAN CALL FUNCTIONS OVER AND OVER
    // ALSO THE CONDITIONAL STATEMENTS GET MORE READABLE

    // IT THINK ALSO THAT IDS MUST BE ALWAYS > 0 , SO YOU CAN DO CAST TO CLEAN INPUT
    // $id_seg = (int) $this->uri->post('id');

    $id      = null ;
    $id_seg  = (int) $this->uri->segment(4) ;
    $id_post = (int) $this->input->post('id') ;
    $url     = base_url(); // THE SAME THAT config_item('base_url') I GUESS

    // NO VALUE THROUGH POST OR URI
    if ( $id_seg <= 0 AND $id_post <= 0 )
    {    
        $url .= 'sales';
        //
        $buttontitle = 'Add';
    }
    // ID THROUGH POST OR URI
    else
    {
        $url .= 'sales/edit';
        $id   = ( $id_seg > 0 ) ? $id_seg : $id_post ;
        //
        $buttontitle = "Edit";
    }

Saludos


Help optimizing form code (checking IDs and radio buttons) - El Forum - 04-22-2010

[eluser]Wayne Smallman[/eluser]
Thanks for the feedback. Again, much appreciated.