データベースにクイズを作成して、次のような基本的なテーブルで回答を保存します。
| question |
|============|
| id (PK) |
| no |
| image_path |
| choice |
|=============|
| id (PK) |
| question_id |
| value |
| answer |
|=====================|
| user_id (PK, FK) |
| question_id (PK, FK)|
| choice_id (PK, FK) |
| user |
|=====================|
| id (PK) |
| session_id or email |
このようなスキーマを使用すると、キーno
を使用して質問を一貫した順序で、またはRAND()
ランダムな順序で簡単に提示できます。選択はオプションですが、クイズを拡張したい場合は、それを使用して選択肢を広げることができます。
特定のユーザーの次の質問を見つけるには、次のようなものがあります
SELECT *
FROM question as q
JOIN answer as a
ON q.id = a.question_id
WHERE q.id NOT IN (SELECT question_id FROM answer WHERE user_id = :user_id)
AND a.user_id = :user_id
ORDER BY q.no -- or ORDER BY RAND()
LIMIT 1;
このようにして、ユーザーがまだ回答していない最初の質問を取得します。それ以外は、ユーザーについてどれだけの情報を保持するかはあなた次第です。
編集 (最初のコメントから追加)
コントローラーには 2 つのアクションが必要です。1 つは表示するものを取得して ID をビューに送信するため、もう 1 つはユーザーからデータを取得して DB に保存するためです。
モデルでは、(少なくとも) 2 つのメソッドが必要になります。1 つは次の質問を取得するためのもので、もう 1 つは回答を保存するためのものです。コードイグナイターの詳細はわかりませんが、主な行は次のとおりです。
Controller:
displayAction()
get user id from user model (through session variable or session id)
If you don't have a reference for the user, you need to create one and return it
get next question from question model (user id as param)
send the question data to the view
saveAction()
get user id (through session variable or session id)
get question id (from get or post param)
if you use a database to store choices, validate that the choice is possible, based on the current question
send user id, question id and the choice made to the answer model for storage
redirect to displayAction()