#1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,650
    Rep Power
    171

    Checkbox checked. Codeigniter and Bootstrap


    Hi;

    How can I make this code in controller nicer and smaller?

    Controller
    PHP Code:
    $data['washer_dryer_checked'] = $this->input->post('washer_dryer') ? TRUE $data['details'][0]->washer_dryer;
                if(
    $data['washer_dryer_checked']=='on')
                    {
                        
    $data['washer_dryer_checked']  = TRUE;    
                    } 
    View
    PHP Code:
      $data = array(
        
    'name'        => 'washer_dryer',
        
    'value'       => 'on',
        
    'checked'     => $washer_dryer_checked
        
    );

    echo 
    form_checkbox($data); 
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Logical operators

    PHP Code:
    $data['washer_dryer_checked'] =
        
    $this->input->post('washer_dryer') || $data['details'][0]->washer_dryer == 'on'
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,650
    Rep Power
    171
    What about this one, slightly different:
    PHP Code:
    //Checkboxes
    if($_POST)
        {
            if(
    $this->input->post('furnished')=='on')
                {
                    
    $data['furnished_checked'] = TRUE
                }
            else
                {
                    
    $data['furnished_checked'] = FALSE;    
                }
        }
    else
        {
            if(
    $data['details'][0]->furnished=='on')
                {
                    
    $data['furnished_checked'] = TRUE;    
                }
            else
                {
                    
    $data['furnished_checked'] = FALSE;        
                }
        } 
    For 15 checkboxes.
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    The result of a comparison already is either true or false. It doesn't get "truer" or "falser" if put into an if statement.

    So things like

    PHP Code:
    if ($some_boolean_expression)
        
    $var true;
    else
        
    $var false
    don't make a lot of sense, because they're a very cumbersome way of expressing

    PHP Code:
    $var $some_boolean_expression
    Your whole code could be boiled down to

    PHP Code:
    if ($_POST)
        
    $data['furnished_checked'] = $this->input->post('furnished') == 'on';
    else
        
    $data['furnished_checked'] = $data['details'][0]->furnished == 'on'
    This can be reduced further with the logical operators, but then it's probably harder to read.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  8. #5
  9. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,650
    Rep Power
    171
    Originally Posted by Jacques1
    This can be reduced further with the logical operators, but then it's probably harder to read.
    Would you please show me?
  10. #6
  11. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    PHP Code:
    $data['furnished_checked'] =
        (
    $_POST && $this->input->post('furnished') == 'on')
        || (!
    $_POST && $data['details'][0]->furnished == 'on')

    But like I said: This is too obscure. And since we're not doing code golf, go with the previous code.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  12. #7
  13. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,650
    Rep Power
    171
    How about this
    PHP Code:
    if ($_POST)
        {
            
    $data['selected_available_date'] = $this->input->post('available_date');
        }
    else
        {
            if(
    strtotime($data['details'][0]->available_date) < strtotime('Y-m-d'))
                {
                    
    $data['selected_available_date'] = strtotime('Y-m-d');
                }
            else
                {
                    
    $data['selected_available_date'] = strtotime($data['details'][0]->available_date);    
                }
        } 

IMN logo majestic logo threadwatch logo seochat tools logo