Jump to content

Writing a Quiz HELP


stephflix

Recommended Posts

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>';
 
 
?>
Link to comment
Share on other sites

<?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>';
 
 
?>

 

 

Link to comment
Share on other sites

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 by Barand
Link to comment
Share on other sites

 

$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 by Psycho
Link to comment
Share on other sites

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 by Barand
Link to comment
Share on other sites

<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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.