How to get key for multi-dimension array when doing foreach?
Posted: Fri Jun 05, 2009 1:37 am
pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:
Posting Code in the Forums to learn how to do it too.
I am creating quiz software and want to display BOTH the questions and the answers for each question in a random order. I thought I'd accomplished this, but then realized I didn't know how to deal with my data once I'd gotten it into _POST.
A little bit of background on me and the program.
I am largely self-taught and learning as I go along. Classes confuse the heck out of me, but I'm willing to use them if they are necessary and are explained clearly.
My application is a quiz management program that will allow multiple quizzes to be managed from a central location. Each quiz may have multiple categories and an unlimited number of questions in each category. Scoring will be done on the following bases: 1) Overall Raw Score, 2) Overall Percentage Score, 3) Raw Score by Category, 4) Percentage Score by Category.
Here's snippet of the code:
Each question can have up to 8 answers, but also can have as few as 2. Up to four and as few as one of these answers may be right. They may be entered by the quiz administrators in any order and are marked as correct at that time. So before this snippet I've retrieved all the questions and organized them randomly.
Then with this snippet I am sticking the answers for a given question into a multi-dimensional array and, if the answer is not blank, displaying it with a radio button next to it. The name of the radio button is the ID number of the question as retrieved from the database (primary key). Then I get posted data that looks like this:
In my scoring code I have been just fumbling around, trying to figure out exactly what I am doing. I have created several arrays that I think might be useful--probably far more than I need.
These arrays include:
$category -- an associative array listing all the categories to which each question can belong. Each question can belong to only one category. The key is the id number (primary key) of the category from the category_t table.
$x -- this is an array created repeatedly during a while loop. It contains ALL the fields for each question (but, of course, only during that cycle through the while loop).
$questionlist -- a "regular" array listing all the ID numbers (primary keys) for the questions used in this quiz
$allquestions -- an associative multi-dimensional array with the key being the question id number (as listed in $questionlist) and the second dimension being a list of the correct answers for that question (up to 4). If there are fewer than 4 correct answers, the data is null.
For example, the array looks like this:
[91] is the question number, [0] => 1 is the only correct answer for this question. Sometimes the contents of [0] might be "8" or "6", etc. The contents is the NUMBER of the correct answer, not the answer itself (which may be anything depending on the subject of the quiz). None of the other correct answers have been set, so they are blank. Actually, I haven't decided how to deal with multiple correct answers yet, so ALL of my questions have only one correct answer right now. Eventually I will allow for questions with checkboxes instead of just radio buttons, like "Which of the following are ways to do X?" where between 1 and 8 may be checked, but only between 1 and 4 will be right.
What I want to do is figure out how to do the following:
Get all of the correct answers (by number), compare this data to the answer selected by the quiz-taker, determine if it is correct.
I think that my problem might just be that I am passing the wrong data into my form--or not enough data. The name of the radio button is the id number for the question, but I am not passing the right information for the comparison.
What I think I need to do is take the key from the $answers array (e.g. a1, a2, a3) and somehow pass it to my scoring code to compare it to the correct answer. How can I display the key, though?
If I am on the right track, any information regarding how to do what I am trying to do would be appreciated.
If I am totally off-base, any guidance at all would be appreciated!
pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:
Posting Code in the Forums to learn how to do it too.
I am creating quiz software and want to display BOTH the questions and the answers for each question in a random order. I thought I'd accomplished this, but then realized I didn't know how to deal with my data once I'd gotten it into _POST.
A little bit of background on me and the program.
I am largely self-taught and learning as I go along. Classes confuse the heck out of me, but I'm willing to use them if they are necessary and are explained clearly.
My application is a quiz management program that will allow multiple quizzes to be managed from a central location. Each quiz may have multiple categories and an unlimited number of questions in each category. Scoring will be done on the following bases: 1) Overall Raw Score, 2) Overall Percentage Score, 3) Raw Score by Category, 4) Percentage Score by Category.
Here's snippet of the code:
Code: Select all
$answers = Array(
"a1" => Array($q['quest_ans1'],$q['quest_ans1_img']),
"a2" => Array($q['quest_ans2'],$q['quest_ans2_img']),
"a3" => Array($q['quest_ans3'],$q['quest_ans3_img']),
"a4" => Array($q['quest_ans4'],$q['quest_ans4_img']),
"a5" => Array($q['quest_ans5'],$q['quest_ans5_img']),
"a6" => Array($q['quest_ans6'],$q['quest_ans6_img']),
"a7" => Array($q['quest_ans7'],$q['quest_ans7_img']),
"a8" => Array($q['quest_ans8'],$q['quest_ans8_img'])
);
shuffle($answers);
foreach ($answers as $ans) {
if ($ans[0] != "") {
?>
<input class="answer" type="radio" name="<?php echo $q["quest_id"]; ?>" value="<?php echo $ans[0]; ?>"><?php
echo $ans[0]; ?><br />
Then with this snippet I am sticking the answers for a given question into a multi-dimensional array and, if the answer is not blank, displaying it with a radio button next to it. The name of the radio button is the ID number of the question as retrieved from the database (primary key). Then I get posted data that looks like this:
Code: Select all
Array
(
[101] => 27
[99] => 88
[91] => 125
[108] => 20
[94] => 101
[93] => 61
[95] => 142
[100] => 50
[109] => 17
[96] => 79
[97] => 130
[106] => 17
[107] => 2
[92] => 104
[quiz_id] => 14
[quiz_name] => Math Quiz
[submit_quiz_answers_x] => 21
[submit_quiz_answers_y] => 17
[submit_quiz_answers] => Submit Quiz Answers
)
These arrays include:
$category -- an associative array listing all the categories to which each question can belong. Each question can belong to only one category. The key is the id number (primary key) of the category from the category_t table.
$x -- this is an array created repeatedly during a while loop. It contains ALL the fields for each question (but, of course, only during that cycle through the while loop).
$questionlist -- a "regular" array listing all the ID numbers (primary keys) for the questions used in this quiz
$allquestions -- an associative multi-dimensional array with the key being the question id number (as listed in $questionlist) and the second dimension being a list of the correct answers for that question (up to 4). If there are fewer than 4 correct answers, the data is null.
For example, the array looks like this:
Code: Select all
[91] => Array
(
[0] => 1
[1] =>
[2] =>
[3] =>
)
What I want to do is figure out how to do the following:
Get all of the correct answers (by number), compare this data to the answer selected by the quiz-taker, determine if it is correct.
I think that my problem might just be that I am passing the wrong data into my form--or not enough data. The name of the radio button is the id number for the question, but I am not passing the right information for the comparison.
What I think I need to do is take the key from the $answers array (e.g. a1, a2, a3) and somehow pass it to my scoring code to compare it to the correct answer. How can I display the key, though?
If I am on the right track, any information regarding how to do what I am trying to do would be appreciated.
If I am totally off-base, any guidance at all would be appreciated!
pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: