views:

65

answers:

3

I am building an application for a client which is a step by step tool that allows the user to enter data for different "sections" that is later output into a html document they can download

Im aiming for this application to be very flexible so that any changes from the client dont require a lot of rebuilding.

I am using the Codeigniter framework.

The way I am currently looking at it is, I have each step defined in a database table called "steps"

On initialisation the application loads this table and starts at step 0, through to 10 - that's all pretty simple, it loads a new view for each step which is essentially a template

Each step might require either the user enters some data into textboxes, or textareas and other steps require they select values from radio buttons.

Different steps require some different processing, ie the text is just stored in the db, the radio buttons are totaled and averaged etc, and also each steps will have different validation rules

I want to build it with enough flexibility so that if the client changes things later, we wont have to rebuild the app, so that is why Ive created the steps as rows in a db table, but im unsure how I would go about handling each different step in terms of processing the data differently and the different validation. I could have a single function with a whole bunch of conditionals, but this seemed to get quite messy and didnt seem right.

I hope this makes sense, so I guess my question is, what is the best approach to this?

A: 

IMHO, you could make life a lot easier just using $_SESSION, you can still display the different views, and save the file at the end... If you really want to use a database (necessary to be able to recover a document) , using a table for each step might get a bit messy. You would have to pull rows from 10 different tables to piece it together. I would suggest a single table. Nothing wrong with width. As far as your actual question, you can have a controller for each step if you want (each step posting to the controller ahead of it). This may make the code a bit more manageable.

Orbit
Using CI's session class is more appropriate than $_SESSION, especially since you need DB storage cause of the large amounts of data.
stef
+1  A: 

I think it depends on assessing, realistically, what is subject to change, and code those sensible parts with enough flexibility, minimizing coupling, so that you don't have to work on a major rewrite.

Without knowing anything of the above, (initially) I would just have 1 controller with an action that takes the step # from a parameter. Then I would have a base business logic class (I usually put all my b. logic in CI libraries and inject dependencies) with several children classes representing each step.

Each of those classes would take care of initialization/setup (loading required libraries, models, helpers, etc.), validation, and processing (storing the submitted data as required by the current step).

Initially, I wouldn't put the steps in the database.

Obviously, the generic Steps controller would do a bit of magic, by setting up views, creating objects, redirecting, etc. based on the step # parameter.

HTH

Favio
+1  A: 

I had a similar project with a very large multi step form. Each step had about 20 - 40 form fields. Once each step is completed and the "next" button is clicked you validate the data, show form validation errors if needed and once no more errors, you store your data in the session.

$my_data['step_1'] = $an_array_with_your_data_1;
$this->session->set_userdata($my_data);

Then on the next page

$my_data['step_2'] = $an_array_with_your_data_2;
$this->session->set_userdata($my_data);

If you need to process data you can do so before inserting it via set_userdata(), by processing $an_array_with_your_data_1; Or, you do it at the end when all the unprocessed data has been collected. You can retrieve each step's data via

$this->session->userdata('step_1');

Make sure you put your sessions in DB since there is a limit on how much you can store in a cookie. So 1 table is all you need, the query to create the table is here, as are the config options you need to set ($config['sess_use_database'] = TRUE;)

This is perfectly feasible and that's what sessions are for.

You can use one controller, named Steps with functions for step_1(), step_2() etc.

yourdomain.com/steps/step_1 yourdomain.com/steps/step_2

Each URL is mapped to the function with the same name as segment_2 in the Steps controller.

stef