stephflix Posted March 1, 2013 Share Posted March 1, 2013 Hey Everyone, I'm trying to make a quiz in PHP for a class. THis is what I have so far. I just can't get the answers to show up. Any help will be wonderful! <?php $quiz_questions = array(); $quiz_questions['q1'] = array( 'question' => 'What is Rebel Wilsons Character Name?', 'choices' => array( 'a' => 'Fat Amy', 'b' => 'Rebelicious', 'c' => 'Fat Samy', 'd' => 'Pitch Slapper'), 'answer' => 'a'); $quiz_questions['q2'] = array( 'question' => 'What are Nodes?', 'choices' => array( 'a' => 'An STD', 'b' => 'Vocal cords rubbing together at an above average rate w/o proper lubrication', 'c' => 'A point at which lines or pathways intersect', 'd' => 'A high note'), 'answer' => 'b'); $quiz_questions['q3'] = array( 'question' => 'What song did Beca audition with?', 'choices' => array( 'a' => 'Cups', 'b' => 'Call me Maybe', 'c' => 'Titanium', 'd' => 'No Diggity'), 'answer' => 'a'); $quiz_questions['q4'] = array( 'question' => 'Where does Beca work?', 'choices' => array( 'a' => 'Jamba Juice', 'b' => 'Vocal coach at the university', 'c' => 'University radio station', 'd' => 'McDonalds'), 'answer' => 'c'); $quiz_questions['q5'] = array( 'question' => 'What happened at last years finals?', 'choices' => array( 'a' => 'They Won', 'b' => 'Chloe had surgery on her vocal chords', 'c' => 'Bumper sabotaged the Barden Belles', 'd' => 'Aubrey threw up on stage'), 'answer' => 'd'); if($user_answers[$qid] ==' '){ //user did not answer the question }elseif($user_answers[$qid] == $question['answer']){ //answer is correct $total_answered++; $total_correct++; }else{ //answer is wrong $total_answered++; } foreach($quiz_questions as $qid => $question){ //output the question text echo '<p>'.$question['question']. '<p>'; } foreach($question['choices'] as $cid => $choice){ $lid = $qid.'_'.$cid; //default message $msg = ' '; $checked = ($user_answers[$qid]== $cid) ? ' checked ' : ' '; //if all questions have been answered, display correct and wrong answers if($total_answered > 0 and $total_answered == $total_questions){ if ($checked){ if($user_answers[$qid]== $question['answer']){ $msg = echo '<p>CORRECT!</p>'; }else{ $msg = echo '<p>INCORRECT</p>'; } } } echo ' <input type="radio" name=" ' . $qid . ' " value= " ' .$cid. ' " id= " ' .$lid. ' " ' . $checked . '/> <label for =" '. $lid . ' ">' . $choice . '</label>'; ?> Quote Link to comment Share on other sites More sharing options...
Jessica Posted March 1, 2013 Share Posted March 1, 2013 Please put your code in the code tags. Quote Link to comment Share on other sites More sharing options...
stephflix Posted March 1, 2013 Author Share Posted March 1, 2013 How do you do that? Quote Link to comment Share on other sites More sharing options...
stephflix Posted March 1, 2013 Author Share Posted March 1, 2013 <?php $quiz_questions = array(); $quiz_questions['q1'] = array( 'question' => 'What is Rebel Wilsons Character Name?', 'choices' => array( 'a' => 'Fat Amy', 'b' => 'Rebelicious', 'c' => 'Fat Samy', 'd' => 'Pitch Slapper'), 'answer' => 'a'); $quiz_questions['q2'] = array( 'question' => 'What are Nodes?', 'choices' => array( 'a' => 'An STD', 'b' => 'Vocal cords rubbing together at an above average rate w/o proper lubrication', 'c' => 'A point at which lines or pathways intersect', 'd' => 'A high note'), 'answer' => 'b'); $quiz_questions['q3'] = array( 'question' => 'What song did Beca audition with?', 'choices' => array( 'a' => 'Cups', 'b' => 'Call me Maybe', 'c' => 'Titanium', 'd' => 'No Diggity'), 'answer' => 'a'); $quiz_questions['q4'] = array( 'question' => 'Where does Beca work?', 'choices' => array( 'a' => 'Jamba Juice', 'b' => 'Vocal coach at the university', 'c' => 'University radio station', 'd' => 'McDonalds'), 'answer' => 'c'); $quiz_questions['q5'] = array( 'question' => 'What happened at last years finals?', 'choices' => array( 'a' => 'They Won', 'b' => 'Chloe had surgery on her vocal chords', 'c' => 'Bumper sabotaged the Barden Belles', 'd' => 'Aubrey threw up on stage'), 'answer' => 'd'); if($user_answers[$qid] ==' '){ //user did not answer the question }elseif($user_answers[$qid] == $question['answer']){ //answer is correct $total_answered++; $total_correct++; }else{ //answer is wrong $total_answered++; } foreach($quiz_questions as $qid => $question){ //output the question text echo '<p>'.$question['question']. '<p>'; } foreach($question['choices'] as $cid => $choice){ $lid = $qid.'_'.$cid; //default message $msg = ' '; $checked = ($user_answers[$qid]== $cid) ? ' checked ' : ' '; //if all questions have been answered, display correct and wrong answers if($total_answered > 0 and $total_answered == $total_questions){ if ($checked){ if($user_answers[$qid]== $question['answer']){ $msg = echo '<p>CORRECT!</p>'; }else{ $msg = echo '<p>INCORRECT</p>'; } } } echo ' <input type="radio" name=" ' . $qid . ' " value= " ' .$cid. ' " id= " ' .$lid. ' " ' . $checked . '/> <label for =" '. $lid . ' ">' . $choice . '</label>'; ?> Quote Link to comment Share on other sites More sharing options...
Barand Posted March 1, 2013 Share Posted March 1, 2013 (edited) Enclose in [ code][ /code] tags or use "<>" button in toolbar You need something like foreach ($quiz_questions as $qid => $q_data) { echo $q_data['question'] . '<br>'; foreach ($q_data['choices'] as $cid => $choice) { echo "$cid - $choice<br>"; } } Edited March 1, 2013 by Barand Quote Link to comment Share on other sites More sharing options...
stephflix Posted March 1, 2013 Author Share Posted March 1, 2013 That worked! But how do I make it display radio buttons? Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 1, 2013 Share Posted March 1, 2013 (edited) $output = ''; if(!isset($_POST['choices'])) { //User did not answer the questions, show form $output .= "<form method='post' action=''>\n"; $qNo = 0; foreach($quiz_questions as $qid => $question) { //output the question text $qNo++; $output .= "<p>{$qNo}. {$question['question']}<p>\n"; foreach($question['choices'] as $choiceValue => $choiceText) { $output .= "<input type='radio' name='choices[{$qid}]' value='{$choiceValue}'> {$choiceText}<br>\n"; } } $output .= "<br><br>\n"; $output .= "<button type='submit'>Submit</button>\n"; $output .= "</form>\n";; } Edited March 1, 2013 by Psycho Quote Link to comment Share on other sites More sharing options...
Barand Posted March 1, 2013 Share Posted March 1, 2013 (edited) like this You will need a couple of hidden fields. One for the answer and another as a dummy radio button in case no selection is made foreach ($quiz_questions as $qid => $q_data) { echo '<strong>' . $q_data['question'] . '</strong><br>'; echo "<input type='hidden' name='answer[$qid]' value='{$q_data['answer']}'>"; echo "<input type='hidden' name='choice[$qid]' value='' />"; //in case no selection foreach ($q_data['choices'] as $cid => $choice) { echo "<input type='radio' name='choice[$qid]' value='$cid' /> $choice<br>"; } } Edited March 1, 2013 by Barand Quote Link to comment Share on other sites More sharing options...
stephflix Posted March 1, 2013 Author Share Posted March 1, 2013 Thank you so much Barand! You've been a HUGE help! Quote Link to comment Share on other sites More sharing options...
stephflix Posted March 3, 2013 Author Share Posted March 3, 2013 <link href="Style/style.css" rel="stylesheet" type="text/css" /> <?php $page_title = "Pitch Perfect"; ?> <?php include_once('includes/header.php'); ?> <div class="content"> <?php echo '<h1>Are you Aca-Awesome?</h1>'; $quiz_questions = array(); $quiz_questions['q1'] = array( 'question' => 'What is Rebel Wilsons Character Name?', 'choices' => array( 'a' => 'Fat Amy', 'b' => 'Rebelicious', 'c' => 'Fat Samy', 'd' => 'Pitch Slapper'), 'answer' => 'a'); $quiz_questions['q2'] = array( 'question' => 'What are Nodes?', 'choices' => array( 'a' => 'An STD', 'b' => 'Vocal cords rubbing together at an above average rate w/o proper lubrication', 'c' => 'A point at which lines or pathways intersect', 'd' => 'A high note'), 'answer' => 'b'); $quiz_questions['q3'] = array( 'question' => 'What song did Beca audition with?', 'choices' => array( 'a' => 'Cups', 'b' => 'Call me Maybe', 'c' => 'Titanium', 'd' => 'No Diggity'), 'answer' => 'a'); $quiz_questions['q4'] = array( 'question' => 'Where does Beca work?', 'choices' => array( 'a' => 'Jamba Juice', 'b' => 'Vocal coach at the university', 'c' => 'University radio station', 'd' => 'McDonalds'), 'answer' => 'c'); $quiz_questions['q5'] = array( 'question' => 'What happened at last years finals?', 'choices' => array( 'a' => 'They Won', 'b' => 'Chloe had surgery on her vocal chords', 'c' => 'Bumper sabotaged the Barden Belles', 'd' => 'Aubrey threw up on stage'), 'answer' => 'd'); $total_answered = 0; $total_correct = 0; if($user_answers[$qid] ==' '){ //user did not answer the question }elseif($user_answers[$qid] == $question['answer']){ //answer is correct $total_answered++; $total_correct++; }else{ //answer is wrong $total_answered++; } foreach ($quiz_questions as $qid => $q_data) { echo '<br><strong>' . $q_data['question'] . '</strong><br>'; echo "<input type='hidden' name='answer[$qid]' value='{$q_data['answer']}'>"; echo "<input type='hidden' name='choice[$qid' value='' />"; //in case no selection foreach ($q_data['choices'] as $cid => $choice) { echo "<input type='radio' name='choice[$qid]' value='$cid' /> $choice<br>"; } } echo '<p align="center"><input type="submit" name="submit" value="Submit" /></p>'; ?> </div> <?php include_once('includes/footer.php'); ?> Does anyone know why I'm getting the following errors and how I can fix them? Undefined variable: qid Undefined variable: user_answers Line 61 & 63 Quote Link to comment Share on other sites More sharing options...
Jessica Posted March 3, 2013 Share Posted March 3, 2013 Yes. At that point in the script $qid is not defined. Quote Link to comment Share on other sites More sharing options...
stephflix Posted March 3, 2013 Author Share Posted March 3, 2013 How do I define it? Quote Link to comment Share on other sites More sharing options...
Jessica Posted March 3, 2013 Share Posted March 3, 2013 What is it supposed to be? Look at your code, you define it later. Why are you trying to use it BEFORE you defined it? Quote Link to comment Share on other sites More sharing options...
stephflix Posted March 3, 2013 Author Share Posted March 3, 2013 Ugh, i see it now. Thanks. I wish I could figure this stuff out...grrrr. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.