Question

I have a multistep form which is called "card"

This form must contain sevral information of a person, what person has done, the job of person, tools person used etc.

To solve this and make it more cool I made a multiform step form, so when user finishes filling data for first table (for instace the information of tools he used) user clicks on next step button and then inputs from other table is showed (project person has worked)... this keeps on going till user fills all necessary data. To save work I am using grocerycrud.

<form method="post" action="<?php echo base_url();?>controller/save" name="form"/>
  <label>User Name</label>
  <input type="text" name="Username">   
  <input type="submit" value="Add"/>
  <!--#######################
       Several more data to store in distict tables
   #######################
   -->    
</form>   


in controller:

  function save
    {
      $arrData["Username"] = $this->input->post("Username");
      $this->User_model->Add($arrData); 
    } 


in User_model

function Add($arrData) { 
        if($this->db->insert(tableName, $arrData))
          return true;
        else
          return false;
    } 

When redirecting to controller everything would work if I fill only one table.

The problem is I do not know how to store corresponding data to other tables involved. So if I insert a new registry in tableName a new ID is assigned. Now I would liket to use this recently assigned ID to use it as other requiered field in other tables (the related tables have this ID as foreign key)

How to solve this is there something like:

$last_id = $user->id;

so I keep inserting data into other tables,

will it be useful to use

$crud->set_relation('column','table','column');

maybe a callbackor something, Has anyone have done something like inserting into multiple related tables from one form?

Was it helpful?

Solution

So if I am understanding you correctly you need to hold the userId for insertion into multiple tables after the first insert right? That's easy enough, just save the id to the session (if this person is logged into your application you should be doing this anyway at login).

After the initial insert runs you can call this:

$this->session->set_userdata('userId',$this->db->insert_id());

Then any time you need that id just pull it back from the session, for example.

$array = (
    'tool'=>'hammer',
    'userId'=>$this->session->userdata('userId')
);
$this->db->insert('tool_table',$array);

OTHER TIPS

First of all i recomment you use single table to store all the information. But because its multi-step operation therefore you have created several tables to store them because you are saving the data in chunks.

Here is a quick idea for you.

Instead of storing the data into multiple tables save it in single table. When you insert the first form you will get the id and store this id in session/other_techniques. after this you can just use update query to insert the rest of information in same table.

The benefit is that when you want to fetch information you dont have to do joins and joins do slow down the query.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top