tags:

views:

51

answers:

2

I'm building my first real CI app. I have to build a survey system--If the specifics are important, I'll elaborate.

What is the best way to post the info to the db from the user? In a single row, comma separated? I'm a noob here, so detail would be appreciated! :)

I should add: the user needs to have the ability to try multiple times for the test and have each test's score charted. Payment is required to take each test.

Here's where I'm at. This code works, but I'm sure there's a more elegant way to do things.

$this->db->select('credits')->from('be_user_profiles')->where('user_id', $this->session->userdata('id'));
$query = $this->db->get();
foreach ($query->result() as $row)
{
    echo $row->credits;
}

What about this?

A: 

It depends on how the data is structured, but your best bet would be to have a survey table, and store the answer to each question in a different column.

From an MVC point of view, you would have a form in a view, have the controller pass the data from that form the model when it is submitted, then have the model clean the data and insert it into the database.

GSto
Could you elaborate? What do you mean "clean the data"? :)
Kevin Brown
using function like mysql_real_escape_string() and such to make sure the data is safe. NEVER trust user input.
GSto
I'm not sure I'm following. Is this synonymous with validation?
Kevin Brown
A: 

That's more like a code organization / code structure question than a problem.

You can fetch your input data "by hand" or use Active Record CI library to do that (http://codeigniter.com/user_guide/database/active_record.html#insert)

If CI has ORM implemented, the job should be even easier than this.

Just create a controller and check the input data on it (or inside the model) and create a model to apply the necessary cleanups, data verification and proper data assign to one or mode tables and you're set.

edit : more elegant than the example you've showed would be with ORM, but I'm not sure CI has that implemented yet.

yoda
Check my update. :)
Kevin Brown
Yea, CI has a library for ORM.
Kevin Brown