View Single Post
Old 01-25-2013, 03:03 AM   PM User | #12
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,542
Thanks: 62
Thanked 4,054 Times in 4,023 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Oooshhh... I had no idea you didn't know how to save the data in a DB. Explains a lot.

Okay...an important question: After the user has completed all 150 pages, do you need to continue to remember all the answers from all pages? (I would kind of assume so, else why collect the answers in the first place, but...)

There are several simplistic approaches, but whether they match your needs I can't tell.

Simplest of all:
Code:
CREATE TABLE questions(
    pageNumber INT,
    questionNumber INT,
    question VARCHAR(2000),
    PRIMARY KEY( pageNumber, questionNumber )
);

CREATE TABLE users(
    userid INT AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(50),
    password VARCHAR(50)
);

CREATE TABLE userAnswers(
    userid INT,
    pageNumber INT,
    questionNumber INT,
    answer VARCHAR(1000),
    CONSTRAINT FOREIGN KEY userid REFERENCES users(userid),
    CONSTRAINT FOREIGN KEY (pageNumber, questionNumber) 
          REFERENCES questions(pageNumber, questionNumber) 
);
That says: Every page gets a page number (could of course be a page name if you preferred). Every question on each page gets a question number.

Every user has an id. When they login, you lookup that id by username and password. (If not found, they can't log in.) When found, you store the userid in a session variable. THAT IS THE ONLY DEPENDENCE on cookies you have: The userid stored in a session variable, on the server.

When they answer the questions on a page, you add all their answers to the answer table. Each answer is identified by the userid along with, of course, its page number and question number.

Simple as that.

At *ANY* time, you can go look in the database to see what answer *IF ANY* the current user has given to any question.

So if they were in the middle of answering questions on page 73 and needed to stop for the day, they hit the SAVE button. You save all their answers on that page. (You might also save their current page number in a separate table, so that they don't have to remember it.)

Tomorrow, they login. They ask for page 73 (or you remember that's where they left off), you bring it up, but in the process you fill in all the answers they had already made yesterday.

What could be simpler?

It's not terribly sophisticated, because it doesn't allow for questions with multiple answers, for example. And there are many other improvements possible. But it's an easy starting point.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
tpeck (01-25-2013)