Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8244561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:53:41+00:00 2026-06-07T21:53:41+00:00

I’ve been using codeIgniter for 5 months now and I want to improve the

  • 0

I’ve been using codeIgniter for 5 months now and I want to improve the way I write my code. Here’s a method from a controller for users:

function create_user(){ 

         $user_data = array(
            'username' => 'Username', 'firstname' => 'Firstname',
            'middlename' => 'Middlename', 'lastname' => 'Lastname',
            'password' => 'Password', 'department' => 'Department', 
            'role' => 'Role'
        );

        foreach ($user_data as $key => $value) {
            $this->form_validation->set_rules($key, $value, 'required|trim');
        }

        if ($this->form_validation->run() == FALSE) {
            $departments = $this->user_model->list_departments();
            $it_roles = $this->user_model->list_roles(1);
            $tc_roles = $this->user_model->list_roles(2);
            $assessor_roles = $this->user_model->list_roles(3);

            $data['data'] = array('departments' => $departments, 'it_roles' => $it_roles, 'tc_roles' => $tc_roles, 'assessor_roles' => $assessor_roles);

            $data['content'] = 'admin/create_user';

            parent::error_alert();
            $this->load->view($this->_at, $data);

        } else {
            $username = $this->input->post('username');
            $salt = $this->bcrypt->getSalt();
            $hashed_password = $this->bcrypt->hash($this->input->post('password'), $salt);
            $fname = $this->input->post('firstname');
            $mname = $this->input->post('middlename');
            $lname = $this->input->post('lastname');
            $department = $this->input->post('department');
            $role = $this->input->post('role');

            $user_login     = array($username, $hashed_password, $salt);
            $user_profile   = array($fname, $mname, $lname);
            $this->user_model->register_user($user_login, $user_profile, $department, $role);

            $data['content'] = 'admin/view_user';

            parent::success_alert(4, 'User Sucessfully Registered!', 'You may now login using your account');


            $data['data'] = array('username' => $username, 'fname' => $fname, 'mname' => $mname, 'lname' => $lname, 'department' => $department, 'role' => $role);
            $this->load->view($this->_at, $data);
        }

    }

I basically cram in fetching of input, input validation, rendering views for errors or success, and other stuff in a single controller. What’s the better way of doing this, how do you suggest I breakdown this function. Someone said that I should add a router/dispatcher between the view and the controller to handle POST, GET, COOKIE and SESSION but I don’t have any idea how to do that.

Any code samples, suggestions that would help me grasp the proper way of doing things will be accepted as an answer.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T21:53:42+00:00Added an answer on June 7, 2026 at 9:53 pm

    I did some minor changes:
    Not setting unnecessary variables, grouping the userdata in an array etc. That’s just the way I would do it. Other than that you’re good on your way, I’d say.

    <?php 
    function create_user()
    { 
         $user_data = array ('username' => 'Username', 'firstname' => 'Firstname', 'middlename' => 'Middlename', 'lastname' => 'Lastname', 'password' => 'Password', 'department' => 'Department', 'role' => 'Role');
    
        foreach ($user_data as $key => $value) {
            $this->form_validation->set_rules($key, $value, 'required|trim');
        }
    
        if ($this->form_validation->run() == FALSE)
        {
    
            $data['data'] = array(  'departments'   => $this->user_model->list_departments(), 
                                    'it_roles'      => $this->user_model->list_roles(1), 
                                    'tc_roles'      => $this->user_model->list_roles(2), 
                                    'assessor_roles'=> this->user_model->list_roles(3)
                            );
    
            $data['content'] = 'admin/create_user';
    
            parent::error_alert();
            $this->load->view($this->_at, $data);
    
        } 
        else 
        {
            $salt    = $this->bcrypt->getSalt();
    
            $user['username']       = $this->input->post('username');
            $user['hashed_password']= $this->bcrypt->hash($this->input->post('password'), $salt);
            $user['fname']          = $this->input->post('firstname');
            $user['mname']          = $this->input->post('middlename');
            $user['lname']          = $this->input->post('lastname');
            $user['department']     = $this->input->post('department');
            $user['role']           = $this->input->post('role');
    
            $user_login     = array($user['username'], $user['hashed_password'], $salt);
            $user_profile   = array($user['fname'], $user['mname'], $user['lname']);
            $this->user_model->register_user($user_login, $user_profile, $department, $role);
    
            $data['content'] = 'admin/view_user';
    
            parent::success_alert(4, 'User Sucessfully Registered!', 'You may now login using your account');
    
            unset($user['hashed_password']); // Just to be sure 
            $this->load->view($this->_at, $user);
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.