0

I know this kind of question has been asked on different topics and I've read so many but none seem to have what I want. If you are familiar with CodeIgniter you probably know you need a method with parameters in your model to insert records to a database from a controller. In my case there are too many records I want to insert and the parameters seems like they've exceed required limits for parameters, I doubt if there's any but regardless it looks like a dirty work and I want it looking better. If you got any suggestion or improvement here's the code below. I would so much appreciate. Thank you.

public function create_cv($created_by, $fullname = '', $mobile = '', $email = '', $summary = '', $career_obj = '', $personal_skill = '',$experience1 = '', $position1 = '',$duration1 = '', $experience2 = '', $position2 = '', $duration2 = '', $experience3 = '', $position3 = '', $duration3 = '', $degree_course1 = '', $institution1 = '', $degree_duration1 = '', $degree_course2 = '', $institution2 = '',$degree_duration2 = '', $degree_course3 = '', $institution3 = '',$degree_duration3 = '', $skils = '', $linguistic_skills = '', $certificates = '', $hobbies = '', $refrees = '') {
    $data = array(
        'created_by' => $created_by,
        'fullname' => $fullname,
        'mobile' => $mobile,
        'email' => $email,
        'summary' => $summary,
        'career_obj' => $career_obj,
        'perosnal_skill' => $personal_skill,
        'experience1' => $experience1,
        'position1' => $position1,
        'duration1' => $duration1,
        'experience2' => $experience2,
        'position2' => $position2,
        'duration2' => $duration2,
        'experience3' => $experience3,
        'position3' => $position3,
        'duration3' => $duration3,
        'degree_course1' => $degree_course1,
        'institution1' => $institution1,
        'degree_duration1' => $degree_duration1,
        'degree_course2' => $degree_course2,
        'institution2' => $institution2,
        'degree_duration2' => $degree_duration2,
        'degree_course3' => $degree_course3,
        'institution3' => $institution3,
        'degree_duration3' => $degree_duration3,
        'skills' => $skils,
        'linguistic_skills' => $linguistic_skills,
        'certificates' => $certificates,
        'hobbies' => $hobbies,
        'refrees' => $refrees,
    );
    $this->db->insert('users', $data);
}

Thanks again in advance as I hope to get positive results. Thanks again :)

UPDATE: I added this $this->db->insert('users', $data); to the code snippet

UPDATE: Thanks guys it got it handled now. Your answers helped a lot.

Rhyno_xD
  • 13
  • 1
  • 7
  • 1
    just pass the complete dataset as Array!? – Jeff Feb 24 '17 at 13:37
  • [You should probably make a Domain Object or Data Transfer Object](http://stackoverflow.com/questions/3853749/what-is-the-difference-between-an-mvc-model-object-a-domain-object-and-a-dto) to pass around. [Where the validation goes is up to you.](http://avenir.ro/how-to-handle-security-in-post-or-get-using-codeigniter/) – ourmandave Feb 24 '17 at 14:15
  • just some design thoughts: i would have these as a nested array: ('experience', 'position', 'duration'). It will be easier to process in database and in objects. Similar for any any items that are really repeated structures. Easier to add extra tuples (rows) without having to modify the data structures? – Ryan Vincent Feb 24 '17 at 14:39
  • @RyanVincent I will take note of that. thanks – Rhyno_xD Feb 25 '17 at 10:26

3 Answers3

2

You should try like that.

public function create_cv($cvs) {
    $data = [
        'created_by' => $cvs['created_by'],
        'fullname'   => $cvs['fullname'],
        'mobile'     => $cvs['mobile']
    ];
}

Like that for all record.

Ajay Korat
  • 716
  • 4
  • 14
1

Why can't you pass it as an object?

public function create_cv($obj) {
    $data = array('email' => $obj->email,
   ...);
}

where the method is called something like:

$obj = new stdClass();
// Assign properties (parameters) 
$obj->email = "test@mail.com"
// Call function
create_cv($obj);
Erick Boshoff
  • 1,443
  • 2
  • 18
  • 30
  • If you create object then data should get like $data = array('email' => $obj->email, insted $data = array('email' => $obj['email'] – Ajay Korat Feb 24 '17 at 13:48
0

One way to do it would be

public function create_cv ( $args ) {
    $defaults = array (
         // your default values go here
        );
    $data = array_merge( $defaults, $args);
    // if you need single variables
    extract($data, EXTR_SKIP);
}
RST
  • 3,899
  • 2
  • 20
  • 33