0

New to MVC/Codeigniter and still don't completely understand all the concepts. I hope my question was somewhat clear but here is what I'm trying to do:

My application is basically a multipage form that the user fills out and at the end has a report he can view based on all of his inputs. Report flow is basically like this

'Create New Report' -> 'fill customer info' -> 'fill user info' -> 'Enter Data' -> 'Preview Report'

All steps above are essentially views and have corresponding controllers and models.

  1. Every time a new report is created I insert a row with new report id in my db using my reports controller:

    class Reports extends CI_Controller {
    
     public function index() {
      //list all current existing reports
    
    //model controller simple get to return all reports in db
    $data['results']  = $this->report_model->get_reports();
    
    $this->load->view('header');
    $this->load->view('reports_view', $data);
    $this->load->view('footer');
    
     }
    
    
     public function createReport() {
    //create new id for report when user initiates new report
    
    //Enter placeholders in reports db until further info recieved
    $data = array(
        'customer_id'   => 1,
        'user_id'       => 1,
        'data_id'       => 1
        );
    
    //will be done in model just temporarily in controller
    $this->db->insert('reports', $data);
    
      }
     }
    
  2. When selected 'Create New Report' will take you to the 'fill customer info' view which is a simple form with name, contact etc. The customer controller and model will store information to customer db and create new id for the customer.

Customer Controller:

class Customer extends CI_Controller {

public  $customerData;


public function index() 
{

    $this->load->view('header');
    $this->load->view('customer_view');
    $this->load->view('footer');

}


 public function saveCustomerInfo() {


     $customerData = array(

    'customerName'       => $this->input->post('customer_name'),
    'customerPhone'      => $this->input->post('customer_phne'),
    'customerEmail'      => $this->input->post('customer_email'),
    'customerAddress'    => $this->input->post('customer_addr'),
    'customerCity'       => $this->input->post('customer_city'),
    'customerstate'      => $this->input->post('customer_state')

   );

        $output = $this->customer_model->save_customers($customerData);
 }
}
  1. I have done the same thing with the next two view as well.

All the info from customer, user, and data is linked to my Report table through primary key in the other tables to Reports table looks essentially like this.

*---------------------------------------------------------*
| report_id | (fk)customer_id | (fk)user_id | (fd)data_id |
*---------------------------------------------------------*
|   1       |     4           |      7      |     3       | 
|   2       |     2           |      9      |     1       |
|   3       |     1           |      1      |     6       |

I want to know how do I store the $report_id for the current report in progress so that I can pass it to customer/user/data controller so as those parts of the form are filled out and being written to the db I can update the reports table and replace the placeholders I had initially with the current customer, user and data information.

I don't quite want to sue sessions for this so is what is the best way to do this. Might be a really simple answer but I'm a newb so help would be much appreciated.

4
  • mm.. maybe $_SESSION ? Commented Mar 3, 2016 at 15:33
  • It could be the way to go, but I want to know if there was another solution to the problem without using sessions. Commented Mar 3, 2016 at 15:35
  • hidden field with the id? Commented Mar 3, 2016 at 15:38
  • @bos570 set a cookie? Same solution and session really. Storing some key / value pair of data as a pivot bit to recall the related data once all the steps of the form are completed. Commented Mar 3, 2016 at 15:54

2 Answers 2

1

In the model once the $this->db->insert(...); is done, you can get the ID of the last record and return that, essentially doing return $this->db->insert_id();

if you call your save function from your controller as:

$report_id = $this->Reports_model->save(...);

And your model code does

$this->db->insert(...);
return $this->db->insert_id();

Your controller will then have the ID last inserted. You can then pass it through to your view from the controller by doing:

$this->load->view('path/to/view', ['report_id' => $report_id]);

which will then let you use the variable $report_id in the view where you can add it to a hidden field.

You could also use the $report_id from the controller to add it to the session so people can't inspect the HTML and tamper with your ID.

Sign up to request clarification or add additional context in comments.

2 Comments

Is it a better practice to do one or the other. If I just want to use sessions and store the report_id is that better?
It is better to use the session so that the ID can't be modified as easily. If you do go down the route of putting it into the form, then you should look into form token generation which will allow you to detect a change in any of the data which have been set by the system in the form. That's one for another question though.
0

I don't know your exact requirements, but this should work. (this is insecure, and plain out wrong. But it works) Once you generate the report_id, pass that as a get parameter to the rest of the pages. For example:

Assuming report_id 99 has been generated, access the rest of the create_pages as

fillCustomerForm/99
or
fillCustomerForm?report_id=99

This way you can track to which report_id the current information belongs (this parameter should also be passed to your form submission script, ie saveCustomerInfo etc)

As I said, this will work assuming that the user does not change the url. But if he does, you will have to implement some security measures, which in this case will be done using session. So anyway you look at it, you need sessions.

tl;dr Long story short, it can be done. But don't.


PS: Avoid CI's built in sessions, they are broken.

3 Comments

Can you state how CI sessions are broken or provide a link to somewhere which states how? Stating they are broken isn't particularly helpful to anyone coming into this from the outside
Thanks for the info. It's something that people should know about if they are going down that route. I didn't know about the issues and I use CI for a few projects (though only 1 uses sessions)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.