I'm writing a quiz application in PHP and am querying the DB for questions and their associated answers. I then wrangle the result set into a usable array for my front end. However, it always seems like a wrestling match and I'm wondering if I could have got to my desired array structure more efficiently.
What I wanted was an associative array / dictionary which was structured so that the questions were at the top level with their associated answers underneath. I also wanted a numeric index above the question level so that I can later use that to match my current_step
session variable for stepping through the form.
function get_question_data( $quiz_id)
{
global $wpdb;
if ( !isset($quiz_id)) {
return FALSE;
}
$sql = $wpdb->prepare("SELECT q.ID AS 'question_id', question, explanation, q.sort_order, qa.ID AS 'answer_id', qa.answer, qa.correct, qa.hint
FROM {$wpdb->prefix}my_quiz_questions q
LEFT JOIN wp_nmsi_quiz_answers AS qa ON q.ID = qa.question_id
WHERE quiz_id=%d ORDER BY q.ID", $quiz_id);
$quiz_data = $wpdb->get_results($sql, ARRAY_A);
fb($quiz_data,'DATABASE RESULTS');
//build into a user-friendly array which we can use to manage quiz steps and questions later.
$question_array = array();
foreach ($quiz_data as $key=>$value) {
foreach ($value as $k => $v) {
if ($k == 'question' ) {
if (!array_key_exists('question_'.$value['question_id'], $question_array)) {
$question_array['question_'.$value['question_id']]['question_text'] = $v;
$question_array['question_'.$value['question_id']]['question_id'] = $value['question_id'];
}
}
if ($k == 'answer'){
$question_array['question_'.$value['question_id']]['answers'][$value['answer_id']]['text'] = $v;
}
if ($k == 'hint') {
$question_array['question_'.$value['question_id']]['answers'][$value['answer_id']]['hint'] = $v;
}
if ($k == 'correct') {
$question_array['question_'.$value['question_id']]['answers'][$value['answer_id']]['correct'] = $v;
}
if ($k == 'explanation' ) {
$question_array['question_'.$value['question_id']]['explanation'] = $v;
}
}
}
//echo $wpdb->last_query;
return array_values($question_array);
}
I ended up with this solution.
Update: Query Results
array(6) {
[0]=>
array(8) {
["question_id"]=>
string(1) "1"
["question"]=>
string(34) "Question 1. What is the question ?"
["explanation"]=>
string(38) "This is the explanation for question 1"
["sort_order"]=>
string(1) "0"
["answer_id"]=>
string(2) "20"
["answer"]=>
string(16) "this is answer 4"
["correct"]=>
string(1) "0"
["hint"]=>
string(29) "this is the hint for answer 4"
}
[1]=>
array(8) {
["question_id"]=>
string(1) "1"
["question"]=>
string(34) "Question 1. What is the question ?"
["explanation"]=>
string(38) "This is the explanation for question 1"
["sort_order"]=>
string(1) "0"
["answer_id"]=>
string(2) "19"
["answer"]=>
string(16) "this is answer 3"
["correct"]=>
string(1) "0"
["hint"]=>
string(29) "this is the hint for answer 3"
}
[2]=>
array(8) {
["question_id"]=>
string(1) "1"
["question"]=>
string(34) "Question 1. What is the question ?"
["explanation"]=>
string(38) "This is the explanation for question 1"
["sort_order"]=>
string(1) "0"
["answer_id"]=>
string(2) "18"
["answer"]=>
string(16) "this is answer 2"
["correct"]=>
string(1) "0"
["hint"]=>
string(29) "this is the hint for answer 2"
}
[3]=>
array(8) {
["question_id"]=>
string(1) "1"
["question"]=>
string(34) "Question 1. What is the question ?"
["explanation"]=>
string(38) "This is the explanation for question 1"
["sort_order"]=>
string(1) "0"
["answer_id"]=>
string(2) "17"
["answer"]=>
string(16) "this is answer 1"
["correct"]=>
string(1) "1"
["hint"]=>
string(29) "this is the hint for answer 1"
}
[4]=>
array(8) {
["question_id"]=>
string(1) "2"
["question"]=>
string(10) "Question 2"
["explanation"]=>
string(26) "Explanation for question 2"
["sort_order"]=>
string(1) "0"
["answer_id"]=>
string(2) "24"
["answer"]=>
string(28) "test answer 2 for question 2"
["correct"]=>
string(1) "0"
["hint"]=>
string(13) "answer 2 hint"
}
[5]=>
array(8) {
["question_id"]=>
string(1) "2"
["question"]=>
string(10) "Question 2"
["explanation"]=>
string(26) "Explanation for question 2"
["sort_order"]=>
string(1) "0"
["answer_id"]=>
string(2) "23"
["answer"]=>
string(28) "test answer 1 for question 2"
["correct"]=>
string(1) "1"
["hint"]=>
string(13) "answer 1 hint"
}
}