skip to Main Content

I am using MySQL and PHP CodeIgniter.

I want to implement something like this:

---------------------------------------
Skill name: Photoshop | Skill level: 4
---------------------------------------

---------------------------------------
Skill name: CSS | Skill level: 3
---------------------------------------

---------------------------------------
Skill name: CorelDraw| Skill level: 4
---------------------------------------

I can easy print this in a single page. But I need it in a form somehow like this:

---------------------------------------
Skill name: [input] | Skill level: [input]
---------------------------------------

---------------------------------------
Skill name: [input] | Skill level: [input]
---------------------------------------

---------------------------------------
Skill name: [input] | Skill level: [input]
---------------------------------------

My database for skills table is like this:

skill_id | user_id | skill_name | skill_level

I cannot figure it out how to insert each row with name and level into one DB row

Can you help me with this?

2

Answers


  1. Not sure how this would work with whatever form/input framework CI provides but i would normall do this by naming the inputs with array notation:

    <label for="skill_0_name">Skill Name</label>
    <input type="text" name="skill[0][name]" id="skill_0_name" />
    
    <label for="skill_0_level">Skill Level</label>
    <input type="text" name="skill[0][level]" id="skill_0_level" />
    
    <label for="skill_1_name">Skill Name</label>
    <input type="text" name="skill[1][name]" id="skill_1_name" />
    
    <label for="skill_1_level">Skill Level</label>
    <input type="text" name="skill[1][level]" id="skill_1_level" />
    

    After posting to PHP you would get an array like the following in $_POST:

    array(
      'skill' => array(
        0 => array(
          'name' => 'name value for index 0'
          'level' => 'level value for index 0'
        ),
        1 => array(
          'name' => 'name value for index 1'
          'level' => 'level value for index 1'
        )
      )
    )
    
    Login or Signup to reply.
  2. You can use []-notation for input fields.
    As I suppose all your skills have ids, which are skill_id in a DB table.
    So, try creating a form like this:

    Skill name: <input type="text" name="skill[23]" /> | Skill level: <input type="text" name="skill_level[23]" />
    
    Skill name: <input type="text" name="skill[25]" /> | Skill level: <input type="text" name="skill_level[25]" />
    
    Skill name: <input type="text" name="skill[27]" /> | Skill level: <input type="text" name="skill_level[27]" />
    

    Here, 23,25,27 are ids of your skills (for example), if you use existing skills in DB.
    For newly created skills you can use @prodigitalson answer with 0,1,2… indexing.

    Next you can iterate through $_POST['skills']

    foreach ($_POST['skill'] as $skill_id => $skill_name) {
        $skill_level = $_POST[skill_level][$skill_id];
        // do your insert here
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search