0

私はCodeigniterで簡単なクイズアプリを作成しています。このアプリでは、ユーザーに画像が表示され、それが正しいか間違っているかを推測します。「ホットかどうか」に似ています。

25の質問があり、すべて「はい」または「いいえ」の回答です。25ページを作成し、以前の結果を非表示のフィールドに引き継ぐという簡単なオプションを選択することもできますが、これは少し時間がかかり、ばかげているようです。

変更されるのは画像だけであるビューファイルがあります。したがって、画像パスを変数として渡して動的に変更できると思いますが、各回答の結果を記録し、最後に結果を表示してデータベースに挿入する方法を、より抽象的なレベルで理解することはできません。 。

私が抱えている問題は、シーケンス内の次の画像をロードして、その結果を記録する方法です。

Ps

画像は常に同じ順序になります。それらがランダムであるかどうかは実際には問題ではありませんが、それは素晴らしいことです!

4

3 に答える 3

0

データベースにクイズを作成して、次のような基本的なテーブルで回答を保存します。

| 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()
于 2013-02-25T15:23:55.017 に答える
0

次のフィールドを使用して、データベースに質問を挿入することで、これを実現できます。

ID - 質問の ID。画像 - この質問の画像の名前、たとえば「question1Image.jpg」 正解 - この質問の正解。答えがすべて YES/NO の場合は、INT を使用できます。はいの場合は 1、いいえの場合は 0。

データベースに関する質問があるので、コントローラーで次のようなものを作成できます。

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Quizz extends CI_Controller {
    public function questions($idquestion){
        $this->load->model('questions_model');
        //fetch the questions from the database
        $questions = $this->questions_model->get_questions();
        //if it's a post we'll check if the answer is correct and pass the next question to the view
        if($this->input->post()){
            $answered_question = $this->questions_model->get_question($idquestion-1);
            $question = $this->questions_model->get_question($idquestion);

            if($answered_question['correct_answer']==$this->input->post('answer')){
                //if the user answered correctly do something here
            }
            $data['question'] = $question;
            //the id of the next question you'll use it for the next post
            $data['next_question'] = $idquestion+1;
            $this->load->view('your_view', $data);
        }
        //it's the first time the user uses this so we'll load the first answer
        else{
            $question = $this->questions_model->get_question(1);

            $data['question'] = $question;
            //the id of the next question you'll use it for the next post
            $data['next_question'] = 2;
            $this->load->view('your_view', $data);
        }
    }
}

あなたの見解では、次のようなものを持つことができます:

    <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Question page</title>
</head>
<body>
    <?php //we'll use the codeigniter form helper to generate manually the form
    echo form_open('quizz/questions/'.$next_question); ?>
    <!-- This will load the image dynamically-->
    <img src="<?=base_url('public/images/'.$question['image'])?>">

    <label><input type="radio" name="answer" value="1">YES</label>
    <label><input type="radio" name="answer" value="0">NO</label>
    <button type="submit" >Submit</button>
    <?=form_close();?>
</body>
</html>

モデル:

<?php
class questions_model extends CI_Model {

    public function __construct()
    {
        $this->load->database();
    }
    public function get_questions()
    {   
        return $this->db->get('questions_table')->result_array();
    }
    public function get_question($idquestion)
    {   
        $this->db->select('questions_table.*');
        $this->db->from('questions_table');
        $this->db->where('questions_table.id', $idquestion);
        return $this->db->get()->result_array();
    }
}
于 2013-02-25T15:45:53.030 に答える
0

私の意見では、Ajax を使用するか、Ajax を使用しないかの 2 つのオプションがあります。

ajaxなしで最初に簡単なもの:

各質問から他の質問にデータを渡す最良の方法は、セッション変数を使用して並べ替えることです。たとえば、次のように $answers という配列で回答を並べ替えます。

$answers = $this->session->userdata('answers');
$answers[] = ** NEW ANSWER HERE **
$this->session->set_userdata('answers', $answers);

画像を取得するには、コントローラー/モデルの配列内のすべての質問 (画像) を並べ替え、セッションの $answers に含まれるアイテムの数を確認して、次に表示する画像を決定します。そのような単純な !:-)

現在、ajax を使用すると、すべてがほぼ同じになります。唯一の変更点は、ページを更新せずに画像だけを変更するために、jquery/js を少し使いこなす必要があることです (質問)。

それが役に立ったことを願っています。

于 2013-02-25T15:21:15.643 に答える