Z
Z
zepy2018-03-28 21:31:38
PHP
zepy, 2018-03-28 21:31:38

Get checkbox values ​​from database?

Good afternoon, I'm doing a test, help me figure it out. You need to get the values ​​of the checkboxes in the test => [numbers of correct answers]. The data is taken from the database. I wrote the code, but the problem is that the next answer overwrites the previous one. Here is the code

if( !$test ) return false;
        $connection = Yii::$app->db;
        $query = $connection->createCommand('
        SELECT q.id AS question_id, a.id AS answer_id,a.parent_question AS quest_answer
        FROM question q
        LEFT JOIN answer a
            ON q.id = a.parent_question
        LEFT JOIN test
            ON test.id = q.parent_test
                WHERE q.parent_test = :test AND a.correct_answer = :currect
        
        ')->bindValue(':test',$test)->bindValue(':currect',1);
        $modelQuery = $query->queryAll();
        $data = null;
        foreach ($modelQuery as $row )
        {
          $data[$row['question_id']] = $row['answer_id'] ;
 
 
        }
 
       return $data;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Nikolaev, 2018-03-28
@gromdron

What do you want? You explicitly overwrite it in the line: Push the array: When folding 1k1 (1 question = 1 answer) - your structure will do, but with 1 question => there are no more answers.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question