Welcome Guest, Not a member yet? Register   Sign In
What's the best way to handle a function with multiple parameters...
#1

[eluser]dallen33[/eluser]
I have a model with a function called get_user. It looks like this:
Code:
function get_user($create_dropdown = FALSE, $form_name = FALSE, $order_field = FALSE, $order_by = FALSE, $role_id)
    {
        if ($order_field && $order_by):
            $this->db_common->order_by($order_field, $order_by);
        endif;
        
        $result = '';
        $query = $this->db_common->get_where('users', array('role_id' => $role_id));    
        
        if ($query->num_rows > 0):
            if ($create_dropdown):
                $result .= '<select name="'.$form_name.'" id="'.$form_name.'">';
                $result .= '<option></option>';
                
                foreach ($query->result() as $row):    
                    $result .= '<option value="'.$row->id.'">'.$row->first_name.' '.$row->last_name.'</option>';
                endforeach;
                
                $result .= '</select>';
                return $result;
            endif;

        else:
            return FALSE;
        endif;
    }
Is this a bad way of doing things? I basically wanted to keep the model complex while keeping my view code simple, so if I have to build a <select> box with users, I just do this:
Code:
echo $this->database->get_user($create_dropdown = TRUE, $form_name = $name, 'first_name', 'asc', $role_id = '4');
But what if I want to add a parameter to the function itself (like $table maybe), then I would have to go back to ALL instances of $this->database->get_user() and either add a true or false for building a table.

I feel like I'm going about this all wrong.
#2

[eluser]Buso[/eluser]
what I usually do is to send an array instead. Then check if isset() for each key, OR set a default value for it.

eg:

Code:
isset($options['create_dropdown']) OR $options['create_dropwdown'] = FALSE;
#3

[eluser]dallen33[/eluser]
Great idea! That's exactly what I needed. Thanks!




Theme © iAndrew 2016 - Forum software by © MyBB