I thought it would probably be rude to post this on Coding Horror, as it is rediculously long… but here is my attempt at Spartan coding.
So, the first function… changes a users information in the database:
function update_User($user,$id) { if (isset($user['deleteprofile'])) { //array(3, 'live', 'Rick' $this->db->where('user_id', $id); $this->db->delete('users'); $this->db->where('id', $id); $this->db->delete('ez_users'); $this->db->where('user_id', $id); $this->db->delete('ez_auth'); $this->db->where('user_id', $id); $this->db->delete('ez_access_keys'); $this->db->where('user_id', $id); $this->db->delete('faves'); return true; } $user['first_name']= isset ($user['first_name'])? $user['first_name']: ""; $user['last_name']= isset ($user['last_name'])? $user['last_name']: ""; $user['academic_status']= isset ($user['academic_status'])? $user['academic_status']: ""; $user['college']= isset ($user['college'])? $user['college']: ""; $user['department']= isset ($user['department'])? $user['department']: ""; $data = array( 'first_name' => $user['first_name'], 'last_name' => $user['last_name'], 'academic_status' => $user['academic_status'], 'college' => $user['college'], 'department' => $user['department'], 'user_id' => $id ); $this->db->where('user_id', $id); if (!$this->db->update('users', $data)) { return false; } if (!isset($user['access'])) return true; $data = array( 'access' => $user['access'], 'user_id' => $id ); // print_r($data); $this->db->where('user_id', $id); if (!$this->db->update('ez_access_keys', $data)) { //incorrect parameters. maybe one of your form field names is different from the database fields //in the 'users' table? //common mistake: using a name for the submit button. don't do that! //also: you have to have a hidden field in the form called $user_id with //the value of the ID of the user you are playing with. return false; } return true; }
It became this:
function updateProfile($data, $id){ $this->db->where('user_id', $id); if(!$this->db->update('users', $data)){ return false; } return true; }
This is possible because arrays can be passed to the function. All of the functionality that was needed in the function is still there, it is perfectly readable, and it does the same tests… I guess it could become this:
function updateProfile($data, $id){ $this->db->where('user_id', $id); if($this->db->update('users', $data)){ return true; } return false; }
Which is probably better as it will return false automagically if there is a problem…. in fact, I think I will be changing it to that. Yes it does hide a lot of the functionality, but… it allows the functionality to be huge… I can edit the whole table or just one column with this function, unlike the other function where the author apparently wanted to create a function for each of the options that were needed…. sigh… The unfortunate thing is that I have not been able to get rid of this code completely yet because I have not yet looked to find out where else it is used… And yes, I know it would be more complex if it were not for the fact that I was using the code igniter framework, but… not as complex as the one that preceeded it.
Wow, the code for the syntax highlighter needs to be fixed…
<pre lang="php">
works, but
<pre lang = "php">
does not…












~ End Article and Begin Conversation ~
There are no comments yet...
~ Now It's Your Turn ~